How to use vcCollisionDetector.OnCollision event


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.


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():

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. = 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 = []
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)]
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
22.    detector.Active = True
23.    detectors.append(detector)
26.path.OnTransition = createPathPartDetector
28.def OnRun():
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?

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

Thanks in advance!


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.