Page 1 of 1

Simple callback question

Posted: Mon Jan 21, 2013 12:33 pm
by crucio
i just started to learn bullet physics So i Saw some tutorials as well as read wiki but i now am stuck at something now well lets say i have sphere and a box i want the box to turn red when sphere hits it how can i do it ?
gcontactadded doesnt work for me (i already have the box and the Sphere both are rigidbody and are running properly) there are some wiki which i cannot understand and do can someone please Show me a simple code example or something ?
Thank you i will appreciate it!

Re: Simple callback question

Posted: Tue Jan 22, 2013 6:48 pm
by crucio
Bump Please Help Anyone :)

Re: Simple callback question

Posted: Tue Jan 22, 2013 6:52 pm
by majestik666
Why isn't gContactAdded not working for you ?

It's called when a new contact is added to the world,
check that either of the rbd pointer is your sphere,
and that should be it ?

Re: Simple callback question

Posted: Tue Jan 22, 2013 7:42 pm
by crucio
i followed this guy's tutorialand pretty much everything else is the same hers the Code
Well here's the main/relevant code

Code: Select all

bool callbackFunc(btManifoldPoint& cp,const btCollisionObject* obj1,int id1,int index1,const btCollisionObject* obj2,int id2,int index2)
{
        ((bulletObject*)obj1->getUserPointer())->hit=true;
       
        ((bulletObject*)obj2->getUserPointer())->hit=true;
        return false;
}
 
int main()
{
   //main stuff
     gContactAddedCallback=callbackFunc;
//continue main stuff
  
this is the error i get
on this line
gContactAddedCallback=callbackFunc;
error C2440: '=' : cannot convert from 'bool (__cdecl *)(btManifoldPoint &,const btCollisionObject *,int,int,const btCollisionObject *,int,int)' to 'ContactAddedCallback'
1> This conversion requires a reinterpret_cast, a C-style cast or function-style cast

i wondered that gContactAddedCallback doesn't work in new i tried some casting i am not that good into that So Any help will be appreciated

Re: Simple callback question

Posted: Tue Jan 22, 2013 9:15 pm
by jlanisdev
There's a better way to do what I think you're trying to do. Note: This is a little off-topic, I admit, but it's related to the OP's original question so I thought I would share. It's a simple way to receive callbacks (or notifications) when two objects have:

a) just collided
b) are currently in contact with one another
c) have just separated

I believe that's essentially what most people are looking for anyway, because I've come across a lot of threads about the same issue. I'm posting a solution here that's easy to implement and doesn't require any modification to the bullet source code. Instead of using the pre-existing bullet callbacks, I iterate over all the contact manifolds and keep track of the collision state that way. The only requirement is to have each instance of your game object / bullet object (or whatever your user pointer is set to) to have a unique ID associated with it. I wouldn't be posting this unless I felt that this would be useful for a lot of people, not just the OP.

The code is pretty self-explanatory. Just call checkCollisions() each frame, and replace the existing callback/event mechanism in that function with your own implementation. Then make sure you have a getUniqueID() function, or something similar, in your base object class.

Re: Simple callback question

Posted: Thu Jan 24, 2013 4:25 am
by majestik666
for the casting issue, bullet has recently switched from btCollisionObject to btCollisionObjectWrapper,
casting you current function will likely crash when accessing the collision object

if you use a recent cut then your callback function should be of this prototype :

bool contactAddedCallbackBullet(
btManifoldPoint& cp,
const btCollisionObjectWrapper * colObj0,
int partId0,
int index0,
const btCollisionObjectWrapper * colObj1,
int partId1,
int index1)

Re: Simple callback question

Posted: Fri Jan 25, 2013 10:58 am
by crucio
Thanks guys (Both of you)that did it xD