Vizard 8 » Tutorials & Examples » Proximity Sensors » Tutorial: Proximity Events
8.0

Tutorial: Proximity Events

Proximity events are generated when a target enters/exits a sensor region and both sensor and target are registered with the same proximity manager. These events can either be handled using callback functions or within a task function. Both methods will be used in this tutorial.

 

First a task function is created to control program flow. After the participant is instructed to walk to a location in the courtyard the task function will yield until a proximity enter event occurs at that location's sensor. Add the following code and run the script:

#The following task directs the user where to go and waits until the user reaches each destination.
def destinationsTask():

    # Action for hiding/showing text
    DelayHide = vizact.sequence( vizact.waittime(8), vizact.method.visible(False) )
    Show = vizact.method.visible(True)

    yield viztask.waitTime(12)
    instructions.setText("Walk to the potted plant directly ahead on the opposite side of the courtyard.")
    instructions.runAction(DelayHide)
    yield vizproximity.waitEnter(plantSensor)
    instructions.runAction(Show)
    instructions.setText("Face the arch side of the courtyard. Walk to the piles of crates directly ahead.")
    instructions.runAction(DelayHide)
    yield vizproximity.waitEnter(cratesSensor)
    instructions.runAction(Show)
    instructions.setText("Walk into the cafe on the opposite side of the courtyard.")
    instructions.runAction(DelayHide)
    yield vizproximity.waitEnter(cafeSensor)
    instructions.runAction(Show)
    instructions.setText("Thank you for your participation.")
    #Show results of experiment
    print('Avoided sitting avatar:',avoidSitting)
    print('Avoided standing avatar:',avoidStanding)
    print('Avoided dancing avatar:',avoidDancing)

viztask.schedule( destinationsTask() )

In the code above proximity events related to destination sensors are handled. We also want to know which avatar sensors get triggered and handle those events. If an avatar sensor gets triggered it means the participant walked the most direct path to the destination without going out of their way to avoid the avatar. Each avatar has a boolean variable associated with it that tells us whether or not the participant avoided it. These boolean variable were initially set to True in the first section of this tutorial (don't add this code again):

#Boolean variables to store trial results
avoidSitting = True
avoidStanding = True
avoidDancing = True

Within a callback function for proximity enter events, we check to see which sensor was activated and if it matches one of the avatars, the boolean variable is set to False:

#Proximity callback function that records if the user has entered the proximity of an avatar.
#Entering avatar proximity indicates the user did not avoid the avatar and sets the corresponding
#trial variable to False  
def EnterProximity(e):
    """@args vizproximity.ProximityEvent()"""

    global avoidSitting,avoidStanding,avoidDancing
    if e.sensor == sensorAvatar1:
        avoidSitting = False
    elif e.sensor == sensorAvatar2:
        avoidStanding = False
    elif e.sensor == sensorAvatar3:
        avoidDancing = False

manager.onEnter(None,EnterProximity)

Run the script and complete the experiment. Once you enter the cafe the trial results will be shown in the input/output pane.

Proximity Sensors

Sensors and Targets

Proximity Events