How to receive notification on collisions

TDI
Posts: 2
Joined: Wed Sep 17, 2008 12:17 pm

How to receive notification on collisions

Post by TDI »

Hi there,

I'm new to Bullet Physics and I'm planning to use the Java JBullet port in one of my applications.

I'd like to know if there is a way to receive a notification when a collision happens, so that I may perform a custom action instead of having Bullet apply it's physics calculations. For example, I want to know when a RigidBody of my custom type "Balloon" collides with a RigidBody of the type "Needle" so that instead of having Bullet bounce these objects off another, I can make the "Baloon" disappear.

I'm thinking about something like this (pseudocode):

Code: Select all

CollisionObserver myObserver = new CustomCollisionObserver();
dynamicsWorld.addCollisionObserver(myObserver)

interface CollisionObserver {
    // return true if Bullet should perform its collision calculations, otherwise false
    boolean collision(RigidBody collider1, RigidBody collider2);
}

class CustomCollisionObserver implements CollisionObserver{
    boolean collision(RigidBody collider1, RigidBody collider2){
        ... [perform custom collision action]...
        
        // tell Bullet not to perform any collision actions
        return false;
    }
} 
In this example, I can register CollisionObservers with a DynamicsWorld to receive collision "events" and tell the DynamicsWorld if it should go on with the physics calculation or ignore it, because it was already handled.

So for some RigidBodies, I'm only interested in when they collide and not in the physics calculations resulting from that collision.

Is there any way to perform this in Bullet?
chunky
Posts: 145
Joined: Tue Oct 30, 2007 9:23 pm

Re: How to receive notification on collisions

Post by chunky »

TDI
Posts: 2
Joined: Wed Sep 17, 2008 12:17 pm

Re: How to receive notification on collisions

Post by TDI »

Thanks for your reply... yes, that looks like something I can work with. Guess I should have clicked myself through the Wiki first... :roll: sorry