Get intersected polygon from rayTest?

pwr00
Posts: 5
Joined: Tue Sep 10, 2013 8:01 pm

Get intersected polygon from rayTest?

Post by pwr00 »

Hi!
I'm using Ogre and Bullet together in my game and wondering if someone could point me in the right direction. I need to do pick selection on a mesh and find the polygon in the mesh that is collided with the pick ray.
Currlently I'm using btCollisionWorld::rayTest to find the collided object. The result from that technique is the collisionpoint.
But is it possible to get the collided polygon in the same convenient way somehow?
My other option would be to iterate through every polygon in the hit mesh and find the polygon that has the closest hit with the picking ray. But that seems to be wasteful if I already have the answer somewhere from my call to btCollisionWorld::rayTest.

thanks
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Get intersected polygon from rayTest?

Post by Erwin Coumans »

The hit triangle index is available through the callback object that you are passing in the rayTest method.

Just derive your own version of the callback and store the hit index.
pwr00
Posts: 5
Joined: Tue Sep 10, 2013 8:01 pm

Re: Get intersected polygon from rayTest?

Post by pwr00 »

Ok, so I should derive my own type of btCollsionWord::RayResultCallback.

I guess that it's this function your are refering to:
RayResultCallback::addSingleResult( LocalRayResult & rayResult, bool normalInWorldSpace )

The LocalRayResult contains the LocalShapeInfo which has a triangleIndex. But if I have the triangle index, how do I get the corresponding triangle? LocalShapeInfo contains a pointer to btCollisionObject. But the btCollisionObject interface doesn't seem to have any way of fetching the corresponding triangle.
Do one normally get many calls to addSignleResult? Or is it only the actual hit that is added in this function callback?

pwr
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Get intersected polygon from rayTest?

Post by Erwin Coumans »

virtual btScalar addSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace) = 0;
LocalRayResult has int m_shapePart; and int m_triangleIndex;

You can get to the collision shape from a btCollisionObject:
btCollisionObject* obj = ....
btCollisionShape* shape = obj->getCollisionShape();
then based on the shape type, you can cast to the right type.
And if you have a btBvhTriangleMeshShape, you can get access to the btStridingMeshInterface using getMeshInterface,
and a btStridingMeshInterface allows you to query for triangles.
pwr00
Posts: 5
Joined: Tue Sep 10, 2013 8:01 pm

Re: Get intersected polygon from rayTest?

Post by pwr00 »

Ok, I my meshes are not btBvhTriangleMeshShape. They are btTriMesh. But according to the Bullet documentation, they inherit from btStridingMeshInterface. So it should work to query the mesh for the triangle index that I have. The only problem is that I can't see any functions in the btStridingMeshInterface that seems to be usable for accessing specific triangles. Have I misunderstood something here?