Collision detection
CoppeliaSim can detect collisions between two collidable entities in a flexible way. The calculation is an exact interference calculation. Collision detection, as its name states, only detect collisions; it does however not directly react to them (for collision response, refer to the dynamics module).
[Collision detection between two manipulators]
Using the API function sim.checkCollision, one can easily detect collision between entities, for instance collision detection between a robot and its environment, from within a child script, in each simulation step:
function sysCall_init()
local robotBase=sim.getObject('/robotModelAlias')
robotCollection=sim.createCollection(0)
sim.addItemToCollection(robotCollection,sim.handle_tree,robotBase,0)
end
function sysCall_sensing()
local result,pairHandles=sim.checkCollision(robotCollection,sim.handle_all)
if result>0 then
print('Robot is colliding. Colliding pair is '..getAsString(pairHandles))
end
end
One can also temporarily modify the color of objects or whole collections, in order to visually indicate a collision:
function changePairColor(entityPair,colorPair)
originalColorData={}
originalColorData[1]=sim.changeEntityColor(entityPair[1],colorPair[1])
originalColorData[2]=sim.changeEntityColor(entityPair[2],colorPair[2])
end
function restorePairColor()
if originalColorData then
sim.restoreEntityColor(originalColorData[1])
sim.restoreEntityColor(originalColorData[2])
originalColorData=nil
end
end
function sysCall_init()
local robotBase=sim.getObject('/irb360')
robotCollection=sim.createCollection(0)
sim.addItemToCollection(robotCollection,sim.handle_tree,robotBase,0)
collisionColors={{1,0,0},{1,0,1}} -- collider and collidee
end
function sysCall_sensing()
local result,pairHandles=sim.checkCollision(robotCollection,sim.handle_all)
restorePairColor()
if result>0 then
-- Change color of the collection and the collidee:
changePairColor({robotCollection,pairHandles[2]},collisionColors)
-- Change color of the collider and collidee objects:
-- changePairColor({pairHandles[1],pairHandles[2]},collisionColors)
end
end
function sysCall_cleanup()
restorePairColor()
end
CoppeliaSim's collision detection functionality is also available as stand-alone routines via the Coppelia geometric routines.
See also the add-on Collision check that allows to quickly check for self-collision, collision with the environment, or collision between two entities.
|