Vizard 8 » Tutorials & Examples » Viewpoints & windows » Viewpoint control » Tutorial: Head and body motion
8.0

Tutorial: Head and body motion

Next we want the user to use the mouse to look around the car while they are driving it. To do this we need to handle mouse move events. Add the following code:

def mousemove(e):
    pass

viz.callback(viz.MOUSE_MOVE_EVENT,mousemove)

viz.mouse(viz.OFF)
viz.mouse.setVisible(False)

The mousemove function will be called every time the mouse moves. The last two lines of code simply turn off the built-in mouse navigation and hide the mouse cursor.

 

Whenever the mouse moves we want to turn the HEAD_ORI of the viewpoint in that direction. What we will do is get the current Euler angle rotation of the HEAD_ORI. We will use the x movement of the mouse and add that to the current yaw. We will then use the y movement of the mouse and add that to the pitch. Replace the pass statement with the following code in your mousemove function:

    euler = view.getEuler(viz.HEAD_ORI)
    euler[0] += e.dx*0.1
    euler[1] += -e.dy*0.1
    euler[1] = viz.clamp(euler[1],-85.0,85.0)
    view.setEuler(euler,viz.HEAD_ORI)

The first line simply gets the Euler angle rotation of the HEAD_ORI. The second line is adding the x movement of the mouse to the current yaw. Notice that we multiply the x movement by 0.1.  This is to decrease the sensitivity of the movement.  The third line adds the y movement of the mouse to the current pitch. We need to negate the value since a positive pitch value means rotating down, but moving the mouse down means having a negative y movement. The fourth line is clamping the pitch value between -90 and 90, since we don't want to let the user look down or up so much that they end up upside down. The last line is simply rotating the HEAD_ORI of the viewpoint by the new euler angle rotation.

 

Run your script again.  Now you can look around the car and move at the same time. Since the HEAD_ORI is attached to the BODY_ORI, it will always rotate with the car. Let's add one more feature to your script. When the user clicks the left mouse button, we want to reset their HEAD_ORI so that they are looking out the front window of the car. When the user clicks the right mouse button, we want to reset the BODY_ORI and position of the viewpoint back to the origin.  To do this we need to create callbacks for mouse button events. Add the following code:

vizact.onmousedown(viz.MOUSEBUTTON_LEFT,view.reset,viz.HEAD_ORI)
vizact.onmousedown(viz.MOUSEBUTTON_RIGHT,view.reset, viz.BODY_ORI |viz.HEAD_POS)
vizact.onmousedown(viz.MOUSEBUTTON_RIGHT,updatecar)

If the left button was pressed then we simply reset the HEAD_ORI of the view. If the right button was pressed we reset the BODY_ORI and head position of the view. We also need to call the updatecar function since viewpoint position has changed. Run your script again to test it out. That's it, now you know how to manually control the viewpoint using the keyboard and mouse.

Setting up a vehicle

Forward/back motion

Left/right turning

Getting viewpoint information

Head and body motion