Get RigidBody from rayTest() in Bullet Physics

Started by
2 comments, last by rocklobster 11 years, 4 months ago
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.
Advertisement
You could try storing the rigid body in the user pointer.


collisionObject->setUserPointer((void*)rigidBody);

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


EDIT: Sorry but I've only used it in c++ so I don't know about the java port
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 provided by the basic collision-only world 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.

Previously "Krohm"


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 provided by the basic collision-only world 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.


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

This topic is closed to new replies.

Advertisement