Vizard 8 » Tutorials & Examples » Viewpoints & windows » Viewpoint control » Tutorial: Forward/back motion
8.0

Tutorial: Forward/Back motion

Since you will be manually controlling the viewpoint in your script, you will need to get the main viewpoint object:

view = viz.MainView

Next, let's setup the keyboard interaction for moving the viewpoint around. Usually when you want to use the keyboard for input you would create a callback for keyboard events. This would get called every time a key is pressed. However, we want the viewpoint to move while a key is down. In cases like this, it is better to setup a timer that constantly checks if a key is down.  So, we'll use the vizact.ontimer() command to start a perpetual timer and register a function to call when the timer expires.

def updatecar():
    pass

vizact.ontimer(0,updatecar)

Using "0" as the timer interval means that the function updatecar() will be called every frame.  

 

Next we need to replace the pass statement in your updatecar() function with some code that will move the viewpoint around.  Vizard has a command which will let you know if a key is currently down. We will use this command to check if any of the arrow keys are down, and move the viewpoint if they are. First, let's check if either the up or down arrow key is being pressed. If it is being pressed we will move the view forward or backward. Add the following code to your updatecar() function:

    if viz.key.isDown(viz.KEY_UP):
        view.move([0,0,MOVE_SPEED*viz.elapsed()],viz.BODY_ORI)
    elif viz.key.isDown(viz.KEY_DOWN):
        view.move([0,0,-MOVE_SPEED*viz.elapsed()],viz.BODY_ORI)

Note: When replacing the pass statement with the above code, make sure that the indentation for the copy-pasted code is at the same indentation level as the pass statement. Python uses indentation as a structural element of the language. Make sure that the indentation is consistently either tabs or spaces, but not mixed. You can make the tabs/spaces visible in Vizard under the View menu.

First we check if the up arrow is being pressed. If it is then we call the viewpoint move command. This command takes 2 arguments. The first is a list with X,Y,Z values.  These tell how much we want to move sideways (along the X-Axis), vertically (along the Y-Axis), and forward/backward (along the Z-Axis). The second argument is which orientation we want to move relative to.

 

The viewpoint is made up of two orientations, the BODY_ORI and HEAD_ORI. The HEAD_ORI is attached to the BODY_ORI. So whenever the BODY_ORI is rotated, the HEAD_ORI will be rotated also. However, if the HEAD_ORI is rotated, the BODY_ORI will remain the same. So, when we specify BODY_ORI for the second argument we are telling Vizard to move forward in the direction of the BODY_ORI. Since we only want to move forward if the up arrow is pressed, we set the first two elements in the list to 0. The third element is the MOVE_SPEED multiplied by the elapsed time. We set the MOVE_SPEED to 5, which means when the up arrow is pressed, the viewpoint will move forward at 5 meters per second in the direction of the BODY_ORI.

Note: The head and body reference frames are a hierarchical system. You can think of the HEAD_ORI as a child of the BODY_ORI system. Any motions applied to the body are passed down to the HEAD, but motions applied to the HEAD don't affect the BODY.

If the up arrow is not being pressed, then we check if the down arrow is being pressed. If it is then we move backward using the viewpoint move command. Notice that the elements in the list are are the same as when we move forward except that we place the negative sign in front of the move speed. A positive value means we move forward and a negative value means we move backward. This also applies to moving sideways and vertically. Try running your script now. Use the up and down arrow keys to move forward and backward.

Setting up a vehicle

Forward/back motion

Left/right turning

Getting viewpoint information

Head and body motion