Search code examples
pythonvpython

Rotating an object by the own axis?


I want to create an arrow, that can be controlled with the keyboard! Rotations on the xz-plain work great, but i can´t let it rotate by it´s own z-axis.Check This...

So i dont want to use the system axis, i want a axis relativ to the arrow!

from visual import *
from threading import Thread



class Pfeil(frame, Thread):
     "modelliert einen Pfeil"
def __init__(self, pos=(0,0,0), axis=(1,0,0)):
    frame.__init__(self, pos=pos, axis=axis)
    Thread.__init__(self)
    selfpointer = arrow(frame=self, pos=(0,2,1), axis=(5,0,0), shaftwidth=1)

def tasten(self):
    "Methode"
    if scene.kb.keys:
        taste=scene.kb.getkey()
        if taste=='left':
            self.rotate(angle=radians(5), axis=(0,1,0), origin=self.pos)
            print(self.axis)

        if taste=='right':
            self.rotate(angle=radians(-5), axis=(0,1,0), origin=self.pos)
            print(self.axis)

        if taste=='up':
            self.rotate(angle=radians(5), axis=(0,0,1), origin=self.pos)
            print(self.axis)


def run(self):
    while True:
        self.tasten()

Sorry for being too dumb to paste my code in here, so here´s a upload... Upload

Thanks for help, if you dont understand my problem, just comment pls!


Solution

  • You're just doing your transformations out of order.

    You want to do a "local" transformation, which is very straightforward. Move the arrow back to the origin, do your rotation about the z axis, and then move it back to its original position.

    This is easier done if you keep a local coordinate system for the arrow but this may be overkill for your purpose.