How to use vcCollisionDetector.OnCollision event

Hello

I am using Visual Components 4.1.
I would like to do a process to detect collision with Python script.
I want to use the OnCollision event of vcCollisionDetector, but I do not know how to use it.
Could you suggest a simple sample?

Thank you.

Hello

In Python, You can compare two lists to each other. Here is an example:

 

from vcScript import *

comp = getComponent()
sim = getSimulation()
app = getApplication()

def OnRun():

list_A = [ (comp, VC_NODELIST_INCLUDE, VC_NODELIST_NODE ) ]
list_B = [ (sim.World, VC_NODELIST_INCLUDE, VC_NODELIST_TREE )]
detector = sim.newCollisionDetector()
detector.NodeListA = list_A
detector.NodeListB = list_B

print detector.testOneCollision(0)

This will print True, if boxes is collading, False otherwise.

To keep simulation performance high, it is not recommended to test collision to sim.World (=All objects in World), if You can limit the samples.

See more from python script help “vcCollisionDetector”.

Hope this helps.

I don’t remember, but I think you need to manually call the test in script to trigger the OnCollision event. Here is an old snippet from 2014 community that is similar to the one in pp, which adds a detector dynamically to parts entering a path.

01.from vcScript import *
02. 
03.app = getApplication()
04.robot = app.findComponent("ArticulatedRobot_v2 #2")
05.gripper = app.findComponent("SimpleGripper")
06.sim = getSimulation()
07.comp = getComponent()
08.path = comp.findBehaviour("One-WayPath__HIDE__")
09.detectors = []
10. 
11.def createPathPartDetector(part, isArriving):
12.  '''creates collision detector for part moving into path'''
13.  if isArriving == True:
14.    detector = sim.newCollisionDetector()
15.    detector.NodeListA = [(part, VC_NODELIST_INCLUDE, VC_NODELIST_COMPONENT)]
16.     
17.    testAgainst = []
18.    testAgainst.append((robot, VC_NODELIST_INCLUDE, VC_NODELIST_COMPONENT))
19.    testAgainst.append((gripper, VC_NODELIST_INCLUDE, VC_NODELIST_COMPONENT))
20.    detector.NodeListB = testAgainst
21.     
22.    detector.Active = True
23.    detectors.append(detector)
24. 
25.     
26.path.OnTransition = createPathPartDetector
27. 
28.def OnRun():
29.   
30.  ##create control loop for testing rate
31.  while True:   
32.    for detector in detectors:
33.      hit = detector.testAllCollisions()
34.      if hit == True:
35.        print detector.getHitNodeB(0).Name
36.        #sim.halt()
37.    delay(0.1)

Thank you for helpful information.

The Python code you taught explicitly calls the following method to detect collision.

detector.test *** Collisions ()

If I registered an event handler in vcCollisionDetector.OnCollision without calling the method like the above, I expected that the handler will be automatically called when collision is detected.

Is vcCollisionDetector.OnCollision not like that?

1 Like

Hello, could you please clarify what you mean with “manually call the test in script to trigger the OnCollision event”?

Thanks in advance!

Hello,

It has been four years and lots of techno, so I drink some coffee and see if I can help.

The layout in 3D world has one collision detector called “Selection vs World” and you do not need to tell it to do anything. It will run during simulation if enabled as shown in the following image and trigger the event.

If you make your own collision detector using API, you must tell it when to test and how often. The following image illustrates this using a sampling rate during simulation.

I hope this helps. You’ll do fine.

3 Likes