Vizard 8 » Tutorials & Examples » Example scripts » Input Devices » Navigation
8.0

Navigation

This script demonstrates joystick navigation.

import viz

viz.setMultiSample(4)
viz.fov(60)
viz.go()

#Add a maze
maze = viz.addChild('maze.osgb')

#Add a joystick
dinput = viz.add('DirectInput.dle')
joy = dinput.addJoystick()
joy.setDeadZone(0.2)

def UpdateJoystick():

    elapsed = viz.elapsed()

    #Get the joystick position
    x,y,= joy.getPosition()

    #Get the twist of the joystick
    twist = joy.getTwist()

    #Move the viewpoint based on xy-axis value
    move_amount = 5 * elapsed
    viz.MainView.move([x*move_amount,0,y*move_amount], viz.BODY_ORI)

    #Turn the viewpoint left/right based on twist value
    turn_amount = 90 * elapsed
    viz.MainView.setEuler([twist*turn_amount,0,0], viz.BODY_ORI, viz.REL_PARENT)

#UpdateJoystick every frame
vizact.ontimer(0,UpdateJoystick)

#Register a callback for joystick button events
def onJoyButton(e):
    if e.object is joy:
        print('Joystick button',e.button,'pressed')
viz.callback(viz.SENSOR_DOWN_EVENT, onJoyButton)