simCheckCollision(objectHandle, sim_handle_all) not working

Report crashes, strange behaviour, or apparent bugs
Post Reply
FrankVeen
Posts: 7
Joined: 15 Mar 2016, 16:01

simCheckCollision(objectHandle, sim_handle_all) not working

Post by FrankVeen » 26 May 2016, 13:20

Hi Coppelia Robotics,

Through a C++ plugin I am trying to detect collisions between objects and I noticed that it does not always work. When I know that two objects are colliding simCheckCollision(objectHandle1, objectHandle2) returns true but simCheckCollision(objectHandle1, sim_handle_all) returns false.

The plugin operates similarly to the bubblerob plugin. When the simulation starts, objects are created, set to a position and orientation, and finally checked for collision. Here is some relevant code:

if (message == sim_message_eventcallback_simulationabouttostart) {
float size1[3] = { 0.1,0.1,0.1 };
float position1[3] = { 0,-0.4,0.2 };
float orientation1[3] = { -0.3,0.8,-0.5 } ;

float size2[3] = { 0.1,0.1,0.1 };
float position2[3] = { 0,-0.45,0.25 };
float orientation2[3] = { 0.3, 0.2, 0 };

int cube1 = simCreatePureShape(0, 16, size1, 1, NULL);
simSetObjectPosition(cube1, -1, position1);
simSetObjectOrientation(cube1, -1, orientation1);

int cube2 = simCreatePureShape(0, 16, size2, 1, NULL);
simSetObjectPosition(cube2, -1, position2);
simSetObjectOrientation(cube2, -1, orientation2);

std::cout << "cube 1 to cube 2 collision = " << simCheckCollision(cube1, cube2) << std::endl; // prints 1 (prints 0 if cube 1 and 2 are in the same position and orientation)
std::cout << "cube 1 to all_except_self collision = " << simCheckCollision(cube1, sim_handle_all_except_self) << std::endl; // prints -1
std::cout << "cube 1 to all collision = " << simCheckCollision(cube1, sim_handle_all) << std::endl; // prints 0
std::cout << "cube 2 to all collision = " << simCheckCollision(cube2, sim_handle_all) << std::endl; // prints 0

if (simCheckCollision(cube1, sim_handle_all) == true) {
std::cout << "objects collided" << std::endl; // never gets here...
}
}

I am not sure what is going wrong.

Cheers, Frank

- edit: just a small update, when the objects cube1 and cube2 are in the exact same position and orientation simCheckObjectCollision(cube1, cube2) also doesn't work anymore.

coppelia
Site Admin
Posts: 6683
Joined: 14 Dec 2012, 00:25

Re: simCheckCollision(objectHandle, sim_handle_all) not work

Post by coppelia » 26 May 2016, 14:12

Hello,

so following might be happening:

When your identical objects are perfectly aligned and at the same position, then it will probably not detect collision: the collision checking for shapes is based on triangular mesh vs triangular mesh intersection testing. The triangle thickness is infinitely thin, so two aligned triangles won't be detected for collision.

When you call simCheckCollision between two specific objects, then their collidable flag is overridden. This is not the case when you use simHandleCollision on a collision object that contains the same object pair, since with the second function, the collidable flag is always taken into account (if you manually call simHandleCollision, the collision object should be flagged as explicit handling, so that the main script doesn't implicitely handle it).

Now, when you call simCheckCollision between one object and all other objects in the scene, the collidable flag of the first object is overridden, but not the collidable flag of all other objects in the scene. So you should specifically enable the collidable flag for the objects that are meant to be tested (it most of the time doesn't make sense to test all scene objects, since many will never collidable, or are not meant to be colliding (or at least detected for collision).

Another option would be to create a collection that contains all scene objects, and check in the collection dialog the Collection overrides the collidable, measurable, renderable, cuttable and detectable properties item. Then you can call simCheckCollision(object1,collectionHandle) and all involved objects will have their collidable flag overridden.

Cheers

FrankVeen
Posts: 7
Joined: 15 Mar 2016, 16:01

Re: simCheckCollision(objectHandle, sim_handle_all) not work

Post by FrankVeen » 26 May 2016, 14:26

Thanks for the clarification.
coppelia wrote:Another option would be to create a collection that contains all scene objects, and check in the collection dialog the Collection overrides the collidable, measurable, renderable, cuttable and detectable properties item. Then you can call simCheckCollision(object1,collectionHandle) and all involved objects will have their collidable flag overridden.
I will try to add all the objects to a collection as you suggested and add a manual function to make sure that the objects are not in the same position.

Cheers, Frank

Post Reply