2
votes

I am trying to visualise a box in VPython The problem is: I know it's roll pitch and yaw, but Vpython's box has attributes "axis" and "up". How could I convert my angles to those two needed vectors?

Here is a short code that shows 3 axes and one box. Function setOrientation should change the boxes attributes "up" and "axis" provided roll, pitch and yaw.

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)

The axes and directions should match the ones when describing orientation of an aircraft

X - points forward

Y - points to the right

Z - points down

roll - positive direction is clockwise

pitch - positive is up

yaw - positive is clockwise

the closest thing i have found is code from 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))

The problem with this solution is that it's axes don't match with my problem and I am unable to modify it to suit my needs.

1

1 Answers

0
votes

You are doing a rotation from a body quardinate system to a global coordinatye system. You can use a quaternion.

You can create a quaternion from roll pitch and yaw with this.

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