[solved] Weird behavior when retrieving stored user pointer

Post Reply
User avatar
jubei
Posts: 11
Joined: Sun Jul 24, 2011 9:29 am

[solved] Weird behavior when retrieving stored user pointer

Post by jubei »

I have a collisionworld with only 2 collision objects coCursor and coHeart. I am storing a pointer to my graphics asset pho::Asset heart in its associated collision object, coHeart. During the manifolds loop, if my spherical cursor collides with coHeart then I do get the stored asset with a static cast:

Code: Select all

 for (int i=0;i<numManifolds;i++)
        {
            btPersistentManifold* contactManifold =  collisionWorld->getDispatcher()->getManifoldByIndexInternal(i);
            btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
            btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());

            pho::Asset* a = static_cast<pho::Asset*>(obA->getUserPointer());
It works like a charm. I get the stored pointer and I can access and use the asset fine. However, from another function (of the same class) part of my code, when I do rayTest in the collisionWorld the collisionObject retrieved from the ResultCallback refuses to give me the user pointer stored. I get 0x0.

Any ideas how I can debug it? gdb shows me that my ray has hit the correct object (coHeart)

https://dl.dropboxusercontent.com/u/147 ... er-bug.png

but for some reason coHeart does not give me the pointer to the heart asset. They are both private member variables in my class so there's no chance of the heart object being destroyed or going out of scope etc.

Any ideas on what to try to debug it?
Last edited by jubei on Mon Aug 26, 2013 7:51 am, edited 1 time in total.
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Weird behavior when retrieving stored user pointer

Post by xexuxjy »

Not sure if it's relevant and I can't see it in the current code but I know that bullet makes use of temporary collision shapes for some bits of the collision testing which may not be correctly maintaining the userPointer, thing to search for / breakpoint on would be setTemporaryCollisionShape in btCollisionObject.
User avatar
jubei
Posts: 11
Joined: Sun Jul 24, 2011 9:29 am

Re: Weird behavior when retrieving stored user pointer

Post by jubei »

xexuxjy wrote:Not sure if it's relevant and I can't see it in the current code but I know that bullet makes use of temporary collision shapes for some bits of the collision testing which may not be correctly maintaining the userPointer, thing to search for / breakpoint on would be setTemporaryCollisionShape in btCollisionObject.
Thank you for the reply. I'm guessing that's not the problem because it's not the collisionShape that stores the userpointer it's the collisionObject. Thanks though.

The funny thing is that I stopped using a collisionWorld only, I tried to redo the rayTest with a btDynamicsWorld and the exact same thing happens :/

Niko
MaxDZ8
Posts: 149
Joined: Fri Jun 24, 2011 8:53 am

Re: Weird behavior when retrieving stored user pointer

Post by MaxDZ8 »

If memory serves, user pointers are not serialized in any way using the default world loader. How are you working around that issue? How do you set up user pointers for world objects?
Personally I've given up that, I couldn't consider it robust. I do a lookup in std::map instead.
User avatar
jubei
Posts: 11
Joined: Sun Jul 24, 2011 9:29 am

Re: Weird behavior when retrieving stored user pointer

Post by jubei »

MaxDZ8 thank you for your reply. I sorted the problem out. It was a very embarassing issue with my c++ function, not with bullet. I was doing the rayTest in a function that was being passed a ...

Code: Select all

 myAsset* intersectedAsset) { .. }
So I was passing the pointer by copy, not by reference, and when the function returned the pointer was gone.

I was also considering std::map as a solution (actually I had already implemented it). But bullet user setUserPointer() with static_cast<myAsset*>(collisionObject->getUserPointer()); works fine.

I don't quite understand what you mean by "user pointers are not serialized in any way using the default world loader". My graphics engine has an asset.. let's say class myAsset that's always paired with the bullet ribgidbody (or collisionObject if you're only checking for collisions). So I have

Code: Select all

myAsset box;
btRigidBody* rbBox;
I store the pointer to myAsset as such:

Code: Select all

rbBox->setUserPointer(&box);
dynamicsWorld->addRigidBody(rbBox);
So whenever I do collision checks,manifold loops,raytests etc., bullet tells me what collisionObject has been hit. I then use that collisionObject and static cast to a pointer of type myMesh as such:

Code: Select all

myAsset* intersectedAsset;
intersectedAsset = static_cast<myMesh*>intersectedCollisionObject->getUserPointer();
intersectedAsset->modelMatrix = whatever; or
intersectedAsset->beingIntersected = true; 
MaxDZ8
Posts: 149
Joined: Fri Jun 24, 2011 8:53 am

Re: Weird behavior when retrieving stored user pointer

Post by MaxDZ8 »

jubei wrote:

Code: Select all

 myAsset* intersectedAsset) { .. }
So I was passing the pointer by copy, not by reference, and when the function returned the pointer was gone.
What's in the function body is not irrelevant when dealing with pointers. Even passing out the object by reference does not solve the problem: if the pointed object goes out of scope, it will still go out of scope and using a reference does not fix the underlying problem. Maybe you're really using smart pointers?
jubei wrote: I don't quite understand what you mean by "user pointers are not serialized in any way using the default world loader". My graphics engine has an asset.. let's say class myAsset that's always paired with the bullet ribgidbody (or collisionObject if you're only checking for collisions)
What I mean to say is that if you load a world using the default loader and expect the pointers to be re-populated, this won't happen. But it appears that's not the problem as you're building them at runtime.
Post Reply