Callbacks on Collisions between Objects in SIO2

Let’s examine the following collision detection task:

1. Spot a collision between 2 predefined physic objects in our scene.

2. Ignore the collision handling in physics engine (repulsion of the 2 colliding objects).

3. On collision destroy one of the 2 objects (“object1″).

Certainly, “Bullet Physics” С++ engine, embedded in SIO2, supports hooking to collision callbacks. For this purpose Bullet Physics has a global function pointer gContactAddedCallback of the following type:

typedef bool (*ContactAddedCallback)(btManifoldPoint& cp,

const btCollisionObject* colObj0,

int partId0,int index0,

const btCollisionObject* colObj1,

int partId1,int index1);

If we set it to our function, the function will be called at the very start of the collision.

Along with this support of collision callbacks in Bullet Physics, in SIO2 itself there is an easy-to-use wrapper – objects of type SIO2sensor.

It is SIO2sensor objects that we will use for our task.

Firstly, once in our window render callback (function of type SIO2windowrender) we must initialize our custom collision callback:


object1, object2, collisionCallback);

where “object1_object2_collision”is the name of our collision,

object1 и object2 are pointers to our colliding SIO2object objects,

collisionCallback – is a pointer to our function, this function will be called at the start of the collision.

In the same place, in our window render callback, we must turn off the collision handling in Bullet Physics for our object object1:


object1->_SIO2objectphysic->_btRigidBody->getCollisionFlags() |

btCollisionObject::CF_NO_CONTACT_RESPONSE |


Now let’s pass on to our custom collision callback function:

void collisionCallback(void *_ptr)


SIO2sensor *sender = (SIO2sensor *)_ptr;

if (!sio2StringCmp(sender->name, “object1_object2_collision”))


//mark sensor as the sensor to be removed

sensorToRemove = sender;



The function receives SIO2sensor object as its argument: it’s the sensor that has detected collision.

Having detected our collision, we must destroy one of the colliding objects.

Unfortunately, SIO2 won’t let you destroy the object in our callback, that’s why so far we just save the activated sensor and will destroy it in the next iteration of our physics engine.

For this purpose in our SIO2 rendering callback we have to add the following code:






sensorToRemove->_SIO2object0 = NULL;

sensorToRemove->_SIO2object1 = NULL;


//reset the pointer

sensorToRemove = NULL;


As you can see, it is precisely this place where we destroy our object and the activated sensor.

This is it! The 2 objects have collided and one of the colliding objects has been destroyed.