casting help when contact collision detected

toglia
Posts: 12
Joined: Mon May 10, 2010 1:59 am

casting help when contact collision detected

Post by toglia »

Hi, I'm very new to bullet, and although I know this problem is not necessarily bullet specific maybe you guys can still give me a hand.

I'm trying to implement "triggers" in my game, so I basically I copy/pasted the btGhostObject example from the wiki and got the thing working perfectly.
http://bulletphysics.org/mediawiki-1.5. ... d_Triggers

Later, to know which game object hit the "trigger" I inherited my GameObject class to the btRigidBody, so I could cast the manifold body this way:

Code: Select all

GameObject * auxGameObject = static_cast<GameObject*>(manifold->getBody1());
That went very well too. And I was able to know literally the name of the object who touched the trigger.

Now, do you guys know if there is a way of guessing which class is the body really coming from? Say you have a classes like balls, players, cars, etc, ect. They all inherit from a btCollsionObject but you would like to do different things when they touch this trigger, like: if its a ball emit some force, if its a player upgrade his health, etc. Note this is an imaginary situation.

I usually do this with dynamics casts, but its not possible here, nor do I know if its efficient enough. So, have you ever wanted something like this? In that case how do you guys do it?
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: casting help when contact collision detected

Post by Flix »

I usually do this with dynamics casts, but its not possible here, nor do I know if its efficient enough. So, have you ever wanted something like this? In that case how do you guys do it?
Well, you should do some time testing to be sure that dynamic_cast is too slow for your project.
I believe that in many applications it's good enough, and that the main reason it's not used by the "official" Bullet programmer's guidelines is compatibility of the source code (like STL and smart pointers).

Anyway, to answer to your question, you can follow an approach similiar to the following:

1) Create a base class for your bodies with a property that can give you the specialized class (through an enum or an int) you'll be using (basically a virtual int getMyClassType() const method). Add to it a virtual destructor.
2) Inherit from this class all your derived classes, overriding the getMyClassType() method.
3) In each derived class implement some casting static methods that take a btCollisionObject*, extract the user ptr, cast it to your base class, get it the specialized class type index, and if it matches the current derived class, it performs a static cast to it and returns its casted poiter, otherwise zero.
4) Put the ptrs to these classes in the user ptr of the btRigidBody (or btCollisionObject).
5) When you have a btCollisionObject*, call all the "cast" static methods of your derived classes, until you find the correct one.
(As an alternative you can cast the user ptr directly to your base class once and manually code the rest of the process.)

Note that it's useful to have similiar static methods (step 3), when you inherit directly from btRigidBody* too: this way you can minimize a bit the code changes if you switch from a HAS-IT to a IS-IT approach.

Hope it helps.