WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Collision notifications (https://forum.worldviz.com/showthread.php?t=2748)

CarlosRamos 05-24-2010 01:13 AM

Collision notifications
 
I'm using the collision notification mechanism in Vizard, and I want to detect if one object collides with another and apply a force if that happens. But the problem is I can't identify the object. For example, let's say I have an object called "ball", and a object called "racket" already defined and loaded previously on the script, and with the viz.COLLIDE_NOTIFY flag enabled. Then I define the collide notification function:

Code:

def collideNot(e):
    if e.obj1 == ball or e.obj2 == ball:
        print "collision"
    else:
        print "no collision"

It always shows "no collision". Am I doing something wrong? Thanks in advance.

Jeff 05-24-2010 11:48 AM

I'm not sure what's wrong without seeing more of the script. In the following example viz.COLLIDE_NOTIFY is enabled on the ball. When the ball collides with another object it will generate the collision event and show up as obj1 in the callback function. The object it collides with will be obj2.

Code:

import viz
viz.go()

viz.phys.enable()

ground = viz.add('tut_ground.wrl')
ground.collidePlane() 

ball = viz.add('ball.wrl',pos=(0,1.8,6))
ballPhys = ball.collideSphere(bounce=1.5)
ball.enable(viz.COLLIDE_NOTIFY)

def onCollide(e):
        if e.obj1 == ball:
                print 'ball collision'
        if e.obj2 == ground:
                print 'ground collision'
               
viz.callback( viz.COLLIDE_BEGIN_EVENT, onCollide )


CarlosRamos 05-25-2010 01:02 AM

I didn't post more of the script because it's a mess right now :p Anyway, I think it was my fault... The thing I'm trying to do is to detect the collision with only a child of a model. I didn't mention that in my original post for the sake of simplicity, but after checking it, it seems that's the problem, the object I get on the collision event is the parent, not the child.

Code:

child = model.getChild("Plane06")
child.enable(viz.COLLIDE_NOTIFY)

...

def CollideNot(e):
    if e.obj2 == child:
        print "child"
    if e.obj2 == model:
        print "model"

It will print "model", but never "child". Is there any way to do this?

Jeff 05-25-2010 02:33 PM

Collision shapes work with nodes that are childen of viz.WORLD, otherwise they will not be positioned properly.

You could use the method described in the following article:
http://kb.worldviz.com/articles/350


All times are GMT -7. The time now is 04:31 AM.

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