Vizard 8 » Tutorials & Examples » Getting your feet wet » Inserting User Interaction
8.0

Tutorial: Inserting User Interaction

Next we're going to learn how to control events in our world. We'll do this by adding keyboard interaction. The vizact library has commands to handle a variety of events, including keypresses and timer events.

 

We'll do this by using vizact.onkeydown() to listen for a specific keypress and call the appropriate function when it occurs.

 

First, we define a walkAvatars function that gets called when the 'w' key is pressed. This will add walking actions to the human avatars using the vizact.walkTo command. This command takes a [x,y,z] target location as an argument. The locations specified here are on the other side of the piazza through the arch. The female's walk action is applied 0.5 seconds after the male's walk action. This accounts for the time it takes the male to turn toward the arch so they walk out side by side. Add and run the following code to see this in action.

def walkAvatars():
    walk1 = vizact.walkTo([4.5, 0,-40])
    vizact.ontimer2(0.5,0,female.addAction,walk1)

    walk2 = vizact.walkTo([3.5,0,-40])
    male.addAction(walk2)

vizact.onkeydown('w',walkAvatars)

Next, we define a pigeonsFeed function that gets called when the 'p' key is pressed. When the event occurs a repeating sequence of actions (walk, change animation, wait) is applied to each pigeon to replicate walking and feeding behaviour. By generating random parameters for each action the result is movement that looks quite natural. This code is introduced to demonstrate the power of actions so you don't need to worry about the details at this time. Add the following code and run the script:

def pigeonsFeed():

    random_speed = vizact.method.setAnimationSpeed(0,vizact.randfloat(0.7,1.5))
    random_walk = vizact.walkTo(pos=[vizact.randfloat(-4,4),0,vizact.randfloat(3,7)])
    random_animation = vizact.method.state(vizact.choice([1,3],vizact.RANDOM))
    random_wait = vizact.waittime(vizact.randfloat(5.0,10.0))
    pigeon_idle = vizact.sequence( random_speed, random_walk, random_animation, random_wait, viz.FOREVER)

    for pigeon in pigeons:
        pigeon.addAction(pigeon_idle)

vizact.onkeydown('p',pigeonsFeed)

 

This concludes the Vizard Introduction. From here, you can either follow through the tutorials in order or pick and choose the ones that focus on your areas of interest.

 

Have fun!

 

The WorldViz team

Launch into a new world

Creating a new Script

Setting the Scene

Moving the Viewpoint

Manipulating 3D Models

Animating 3D Models

Timer Events

Adding Avatars

Inserting User Interaction