Sign in to follow this  

Get RigidBody from rayTest() in Bullet Physics

This topic is 1857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've recently started implementing physics in my game. I'm using the java port of the bullet physics engine. I'm trying to interact with rigid bodies in my scene using rayTest() to select what rigid body to interact with. I'm confused on the correct way of using the rayTest(start, end, callback) function. From my callback I can retrieve a CollisionBody but I need a RigidBody. I'm wondering If I'm about the this the wrong way. Thanks.

Share this post


Link to post
Share on other sites
You could try storing the rigid body in the user pointer.

[CODE]
collisionObject->setUserPointer((void*)rigidBody);

// then when you ray cast you can get the user pointer from the collision object
btRigidBody* body = (btRigidBody*)rayHitObject->getUserPointer();
[/CODE]

EDIT: Sorry but I've only used it in c++ so I don't know about the java port Edited by rocklobster

Share this post


Link to post
Share on other sites
There's likely nothing wrong in your understanding.
Let's start from the difference between [font=courier new,courier,monospace]btCollisionObject [/font]and [font=courier new,courier,monospace]btRigidBody[/font].
[font=courier new,courier,monospace]btRigidBody [/font]is a [font=courier new,courier,monospace]btCollisionObject [/font]with extra state added to manage dynamics.
Unfortunately, dynamics are optional in Bullet ([font=courier new,courier,monospace]btCollisionWorld[/font]) and the dynamics-enabled world is a derived class.
Ray casting (and other tests) are [url="http://bulletphysics.com/Bullet/BulletFull/classbtCollisionWorld.html"]provided by the basic collision-only world[/url] and they know only about [font=courier new,courier,monospace]btCollisionObject [/font]so yes, there's a information loss.

So, what we do if we need a [font=courier new,courier,monospace]btRigidBody[/font]?
Here's the deal. If you use the dynamic-enabled world, you just create [font=courier new,courier,monospace]btRigidBody [/font]only. Therefore you can just blindly cast the pointer. Note the user-pointer involves a cast anyway.

Caveat: if you use bullet serialization files you must be extra special sure they don't inject [font=courier new,courier,monospace]btCollisionObject[/font], otherwise, everything will run amok.

Share this post


Link to post
Share on other sites
[quote name='Krohm' timestamp='1355730978' post='5011582']
There's likely nothing wrong in your understanding.
Let's start from the difference between [font=courier new,courier,monospace]btCollisionObject [/font]and [font=courier new,courier,monospace]btRigidBody[/font].
[font=courier new,courier,monospace]btRigidBody [/font]is a [font=courier new,courier,monospace]btCollisionObject [/font]with extra state added to manage dynamics.
Unfortunately, dynamics are optional in Bullet ([font=courier new,courier,monospace]btCollisionWorld[/font]) and the dynamics-enabled world is a derived class.
Ray casting (and other tests) are [url="http://bulletphysics.com/Bullet/BulletFull/classbtCollisionWorld.html"]provided by the basic collision-only world[/url] and they know only about [font=courier new,courier,monospace]btCollisionObject [/font]so yes, there's a information loss.

So, what we do if we need a [font=courier new,courier,monospace]btRigidBody[/font]?
Here's the deal. If you use the dynamic-enabled world, you just create [font=courier new,courier,monospace]btRigidBody [/font]only. Therefore you can just blindly cast the pointer. Note the user-pointer involves a cast anyway.

Caveat: if you use bullet serialization files you must be extra special sure they don't inject [font=courier new,courier,monospace]btCollisionObject[/font], otherwise, everything will run amok.
[/quote]

Good point, I wasn't sure if you could just blindly cast it to rigid body or not.

Share this post


Link to post
Share on other sites
Sign in to follow this