2012-08-17 60 views
1

我想弄清楚如何旋轉一個框架對象相對於它的當前位置。vpython「相對」旋轉一幀

例如一個物體指向北方,我說繞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) 

回答

0

創建一個自定義類,它是飛機而不是框架。通過這種方式,您可以爲飛機課程提供一種方法,以相對於飛機的位置和方向旋轉課程中的每個項目(機翼,尾部等)。所以這個類將包含一個向量,它的位置以及它的方向向量。然後使用旋轉數學關於飛機的方向矢量分別旋轉每個項目。

對不起,我會在評論中發佈這個,但我沒有足夠的聲望:)