[SOLVED]Get specific btCollisionShape from RayResultCallback

Post Reply
tomhhh
Posts: 17
Joined: Tue Jan 19, 2016 10:44 pm

[SOLVED]Get specific btCollisionShape from RayResultCallback

Post by tomhhh »

Hi there,

If I have a btRigidBody composed of several btCollisionShapes, is it possible to find which btCollisionShape a ray test hit?

At the moment I get back a btCollisionObject from the ray test (btCollisionWorld::RayResultCallback) which I can upcast to a btRigidyBody, which has a btCompoundShape (see below)

Code: Select all

btCompoundShape* cs = (btCompoundShape*)result.m_collisionObject->getCollisionShape();
I can then iterate over the child shapes like so

Code: Select all

int child_count = cs->getNumChildShapes();
for ( int c = 0; c < child_count; ++c )
{
    btCollisionShape* shape = cs->getChildShape( c );
    // something interesting
}
But at the moment I have no way of knowing which one of the collision shapes the ray test hit. Is there a way to do this?

Apologies if I'm missing something incredibly obvious!

Thanks very much for your time!


Tom
tomhhh
Posts: 17
Joined: Tue Jan 19, 2016 10:44 pm

Re: Find specific btCollisionShape from RayResultCallback

Post by tomhhh »

Bumping this in case anyone has any ideas - is there a duplicate question/answer similar to this topic someone could point me to?

Thanks again!


Tom
Xammond
Posts: 15
Joined: Sun Jan 03, 2016 4:22 pm

Re: Find specific btCollisionShape from RayResultCallback

Post by Xammond »

I will also need that much detail sooner or later. Plan would be to have a unique btCollisionShape for all child shapes, then somehow cast the ray/rigid-collsion result to btCollisionShape and extracting the userData/userIndex. It might involve modding the library's source or perhaps there is another way, or even a child btCollisionShape result might already be the one available when they're all uniquely created (not shared).
tomhhh
Posts: 17
Joined: Tue Jan 19, 2016 10:44 pm

Re: Find specific btCollisionShape from RayResultCallback

Post by tomhhh »

Yeah it seems this isn't currently exposed. There is something called LocalShapeInfo in btCollisionWorld which looks like it might have been used along the lines I was enquiring about, but in the source there is a comment stating it is only valid for btTriangleMeshShape. I was hoping not to have to change source if I could help it but do you see this as the only option? Ideally I would want LocalShapeInfo to return the index of the child shape in childShapes or something....

Thanks!


Tom
Xammond
Posts: 15
Joined: Sun Jan 03, 2016 4:22 pm

Re: Find specific btCollisionShape from RayResultCallback

Post by Xammond »

I too was distracted by localShapeInfo in btCollisionWorld, and it turns out that it does contain what we need.
I've modded the ClosestRayResultCallback to store the local ID of a a hit to a compound shape.

In the struct, add
int m_childShapeID;

In addSingleResult(), add
if (rayResult.m_loclShapeInfo) // test for nullptr
m_childShapeID = rayResult.m_localShapeInfo->m_triangleIndex; // <--- hm strange name for this!

Now when you test a ray with btCollisionWorld::ClosestRayResultCallback closestResults(from, to);
if (closestResults.m_collisionObject->getCollisionShape()->getShapeType() == COMPOUND_SHAPE_PROXYTYPE) {
s32 ID = closestResults.m_childShapeID;
...

I still need to do similar for the custom CollisionStarted/Ended callbacks, though I haven't looked in to that yet because I'm working on editing tools (the above saves me a lot of work:)


[edited to include nullptr test]
Last edited by Xammond on Sun May 15, 2016 2:17 pm, edited 1 time in total.
tomhhh
Posts: 17
Joined: Tue Jan 19, 2016 10:44 pm

Re: Find specific btCollisionShape from RayResultCallback

Post by tomhhh »

Xammond you are a total legend! :D

That worked perfectly for me - I added the new info you mentioned to our existing derived implementation of RayResultCallback and everything worked as you described! Thank you very much indeed!!

Much appreciated! :)
haalefbeth
Posts: 1
Joined: Sat Oct 22, 2016 10:38 pm

Re: Find specific btCollisionShape from RayResultCallback

Post by haalefbeth »

Xammond... you're the man! that mod is working like a charm
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Find specific btCollisionShape from RayResultCallback

Post by benelot »

I case this mod works on a general basis, you could consider contributing it to the original code. I will mark this thread as solved for others in need of the solution.
Post Reply