WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Proximity event when avatar enters (https://forum.worldviz.com/showthread.php?t=5328)

BSUGeek 04-06-2015 10:47 AM

Proximity event when avatar enters
 
Hi I need help trying to figure out how can you tell when an avatar enters a proximity sensor. Once the avatar enters the proximity its health is supposed to deplete. I'm not really sure where to start. Is there a way to tell when a sensor enters another sensor? :confused:

Erikvdb 04-07-2015 08:05 AM

Just make your avatar a vizproximity Target ;)

Code:

target = vizproximity.Target(avatar)
manager.addTarget(target)

def EnterProximity(e):
    if e.target == target:
        print "Avatar is losing health"

manager.onEnter(None, EnterProximity)

*Code assumes you have already set up your proximity manager, avatar and sensor(s).

BSUGeek 04-07-2015 08:07 AM

Oh! Wow thanks! I had no idea we could do that!

sportykourty 04-14-2015 09:43 AM

Proximity Sensor Event
 
So I was trying to incorporate the previous code but I'm stuck. does my sensor and target have to be in the same manager? I'm not really sure what I'm doing wrong but the function never executes. :confused:

Code:

sensor = vizproximity.Sensor(vizproximity.Sphere(15,center = [72,-.57,2]), source = avatar1)
manager.addSensor(sensor)

Avatar2Target = vizproximity.Target(avatar2)
Avatar2Manager.addTarget(Avatar2Target)

def HealthLoss(e):
        if e.target == Avatar2Target:
                print "Sensor Entered"
Avatar2Manager.onEnter(sensor1, HealthLoss)


Erikvdb 04-15-2015 01:54 AM

Quote:

Originally Posted by sportykourty (Post 17107)
So I was trying to incorporate the previous code but I'm stuck. does my sensor and target have to be in the same manager? I'm not really sure what I'm doing wrong but the function never executes. :confused:

Yes, all targets and sensors that interact with each other have to be assigned to the same manager.

Here's a complete code example:
Code:

import viz
import vizact
import vizproximity

# Physical Objects
ball = viz.addChild('beachball.osgb',pos=[-1,0.2,0])
avatar = viz.addAvatar('vcc_male2.cfg',pos=[1,0,0])
avatar.setEuler([180,0,0])
avatar.state(5)

# Proximity Management
manager = vizproximity.Manager()
manager.setDebug(viz.ON)

sensor = vizproximity.Sensor(vizproximity.Sphere(0.5, center=[0,0,0]), ball)
manager.addSensor(sensor)

target = vizproximity.Target(avatar)
manager.addTarget(target)

def healthLoss(e):
        if e.target == target:
                print "Sensor Entered"
               
manager.onEnter(sensor, healthLoss)

# Demo
def update():
        if viz.key.isDown(viz.KEY_LEFT):
                avatar.setPosition([-2*viz.elapsed(),0,0],viz.REL_GLOBAL)
        elif viz.key.isDown(viz.KEY_RIGHT):
                avatar.setPosition([2*viz.elapsed(),0,0],viz.REL_GLOBAL)
               
vizact.ontimer(0,update)

viz.MainView.setPosition([0,1,-4])
viz.go()

Use arrow keys to move the avatar to the ball.
Created in Vizard 4.

sportykourty 04-16-2015 08:32 AM

Wow thanks! that's really helpful. So if I want the health to continue to drop until I exit the proximity would i just do a While(true) loop?

Erikvdb 04-21-2015 12:53 AM

Quote:

Originally Posted by sportykourty (Post 17113)
Wow thanks! that's really helpful. So if I want the health to continue to drop until I exit the proximity would i just do a While(true) loop?

No, entering and exiting a proximity are two separate events. Use them to toggle a state that you can check for in the general update loop.

Code:

health = 100
healthloss = False

def onEnter(e):
        if e.target == target:
                healthloss = True
               
def onExit(e):
        if e.target == target:
                healthloss = False
               
manager.onEnter(sensor, onEnter)
manager.onExit(sensor, onExit)

# Demo
def update():
        if healthloss:
                health -= 2 * viz.elapsed()



All times are GMT -7. The time now is 06:30 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC