1

我试图弄清楚如何相对于当前位置旋转框架对象。

例如,一个物体指向北方,我说绕 y 轴旋转 180 度。在那之后我说:倾斜 90 度它从“世界”的角度倾斜它。所以它指向下方而不是向上。(很抱歉这个令人困惑的故事,如果你尝试下面的代码并使用你的上下左右键,你就会明白我的意思)。

任何帮助将不胜感激。

    import time
    import numpy
    from visual import *

    # initialize variables
    pitch_degrees = 0
    roll_degrees = 0
    yaw_degrees = 0

    xangle = 0.0
    yangle = 0.0
    zangle = 0.0

    # create the airplane frame. this will be our working object.
    airplane = frame(make_trail=True)
    # below are the elements that create the airplane
    body = cone(frame=airplane, pos=(50,0,0), axis=(-150,0,0), radius=10)
    body2 = cone(frame=airplane, pos=(50,0,0), axis=(50,0,0), radius=10)
    wing = box(frame=airplane, pos=(35,0,0), size=(30,3,180))
    tail = box(frame=airplane, pos=(-75,0,0), size=(20,3,50))
    aileron = box(frame=airplane, pos=(-75,12,0), size=(20,24,3))
    cabin = ellipsoid(frame=airplane, pos=(30,5,0), axis=(1,0,0),size=(45,24,12))
    #painting
    for obj in airplane.objects:
        obj.color = color.red

    body.color = color.white
    cabin.color = (0.5, 0.5, 0.5)
    cabin.opacity = 0.8

    # I'm experiencing some jitter in my screen when starting up (yay ati....)
    time.sleep(2)

    # loop forever
    while True:
            # are there any keys pressed? if so, act on them.
            if scene.kb.keys: # event waiting to be processed?
                    s = scene.kb.getkey() # get keyboard info
                    if (s == 'up'):
                            pitch_degrees = pitch_degrees - 1
                    if (s == 'down'):
                            pitch_degrees = pitch_degrees + 1
                    if (s == 'left'):
                            roll_degrees = roll_degrees - 1
                    if (s == 'right'):
                            roll_degrees = roll_degrees + 1

            # convert degrees to radians
            zangle = numpy.radians(pitch_degrees)
            xangle = numpy.radians(roll_degrees)

            # execute the actual rotation.
            # but this should be relative to its current rotation :(
            airplane.rotate(angle=zangle,axis=(0,0,1))
            airplane.rotate(angle=xangle,axis=(1,0,0))

            # some delay because i'm a dirty boy
            time.sleep(0.005)
4

1 回答 1

0

创建一个自定义类,它是飞机而不是框架。这样,您可以为飞机类提供一个方法,该方法可以相对于飞机的位置和方向旋转类中的每个项目(机翼、机尾等)。因此,该类将包含一个表示其位置的向量以及一个表示其方向的向量。然后使用旋转数学分别围绕飞机的方向向量旋转每个项目。

抱歉,我会在评论中发布此内容,但我没有足够的声誉:)

于 2013-08-09T21:34:31.673 回答