在VPython中根据滚转、俯仰和偏航显示框
我正在尝试在VPython中可视化一个盒子。问题是:我知道盒子的滚转、俯仰和偏航角,但VPython的box有“轴”和“上”的属性。我该如何把我的角度转换成这两个需要的向量呢?
这里有一段简短的代码,展示了三个坐标轴和一个盒子。函数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轴 - 指向下方
滚转 - 正方向是顺时针
俯仰 - 正方向是向上
偏航 - 正方向是顺时针
我找到的最接近的东西是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))
这个解决方案的问题在于,它的坐标轴和我的问题不匹配,我无法修改它以满足我的需求。
1 个回答
0
你正在把一个物体的坐标系统转换到一个全球坐标系统。你可以使用一种叫做四元数的东西。
你可以通过滚转、俯仰和偏航来创建一个四元数,使用下面的代码。
def QuaternionFromEuler(Roll, Pitch, Yaw):
cRoll = math.cos(Roll/2)
cPitch = math.cos(Pitch/2)
cYaw = math.cos(Yaw/2)
sRoll = math.sin(Roll/2)
sPitch = math.sin(Pitch/2)
sYaw = math.sin(Yaw/2)
Quaternion = numpy.empty(4)
Quaternion[0] = cRoll*cPitch*cYaw + sYaw*sPitch*sYaw
Quaternion[1] = sRoll*cPitch*cYaw - cRoll*sPitch*sYaw
Quaternion[2] = cRoll*sPitch*cYaw + cYaw*cPitch*sYaw
Quaternion[3] = cRoll*cPitch*sYaw - sRoll*sPitch*cYaw
return Quaternion