[Python] Display box in VPython knowing roll pitch and yaw

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página