我试图在 VPython 中可视化一个盒子问题是:我知道它是滚动俯仰和偏航,但 Vpython 的盒子 有属性“轴”和“向上”。如何将我的角度转换为这两个所需的向量?
这是一个显示 3 个轴和一个框的短代码。函数 setOrientation 应该改变盒子属性“向上”和“轴”提供的滚动、俯仰和偏航。
import vis
def setOrientation(element, roll, pitch, yaw):
element.axis = ???
element.up = ???
vis.display(
title='Board orientation',
x=0, y=200,
width=600, height=600,
center=(0, 0, 0),
forward=(1, 0.4, 1),
up = (0,0,-1),
lights =[
vis.distant_light(direction=(0.22, 0.44, -0.88), color=vis.color.gray(0.8)),
vis.distant_light(direction=(-0.88, -0.22, 0.44), color=vis.color.gray(0.3))],
range = 5
)
# Draw all axes
startingpoint = vis.sphere(pos=vis.vector(0, 0, 0), radius=0.2, color=vis.color.yellow)
vis.arrow(pos=startingpoint.pos, axis=vis.vector(3, 0, 0), shaftwidth=0.1, color=vis.color.red)
vis.arrow(pos=startingpoint.pos, axis=vis.vector(0, 3, 0), shaftwidth=0.1, color=vis.color.green)
vis.arrow(pos=startingpoint.pos, axis=vis.vector(0, 0, 3), shaftwidth=0.1, color=vis.color.blue)
#Make a box
mybox = vis.box(pos=(0,0,0), length=6, height=2, width=0.1, color=vis.color.red)
#Orient it by proviging roll, pitch and yaw
setOrientation(mybox, 0, 0, 0)
描述飞机的方向时,轴和方向应该匹配
X - 指向前方
Y - 指向右边
Z - 向下
roll - 正方向是顺时针
pitch - 积极向上
yaw - 正是顺时针
我发现的最接近的是Mike Smorto的代码
axis=(cos(pitch)*cos(yaw),-cos(pitch)*sin(yaw),sin(pitch))
up=(sin(roll)*sin(yaw)+cos(roll)*sin(pitch)*cos(yaw),sin(roll)*cos(yaw)-cos(roll)*sin(pitch)*sin(yaw),-cos(roll)*cos(pitch))
这个解决方案的问题是它的轴与我的问题不匹配,我无法修改它以满足我的需要。