Vizard 8 » Tutorials & Examples » Viewpoints & windows » Viewpoint control » Tutorial: Left/right turning
8.0

Tutorial: Left/Right Turning

 

Next, let's use the right/left arrow keys to turn the viewpoint right and left. Add the following code to your updatecar() function:

    elif viz.key.isDown(viz.KEY_RIGHT):
        view.setEuler([TURN_SPEED*viz.elapsed(),0,0],viz.BODY_ORI,viz.REL_PARENT)
    elif viz.key.isDown(viz.KEY_LEFT):
        view.setEuler([-TURN_SPEED*viz.elapsed(),0,0],viz.BODY_ORI,viz.REL_PARENT)

This is very similar to the previous code, except that we check if the right/left arrow keys are being pressed.  If they are, we use the setEuler command to turn the viewpoint. The first argument is a list that specifies how much we want to rotate the view. Since we only want to turn left and right we just need to change the first element in the list, corresponding to the yaw value.  

 

The amount we turn is the TURN_SPEED (which we set to 60 degrees per second at the top of your script) multiplied by the elapsed time. The second argument is which orientation we want to rotate. Just like when we moved forward, we want to only turn the BODY_ORI.

 

The last argument is very important. This tells Vizard to apply the rotation relative to the existing rotation. So when the right arrow is pressed, we want to turn right relative to the current orientation of the body. The same is done when the left arrow is pressed, except that a negative sign is placed in front of the TURN_SPEED in order to turn the opposite way.

 

Run your script again. Now you can move and turn at the same time.

Setting up a vehicle

Forward/back motion

Left/right turning

Getting viewpoint information

Head and body motion