Detection of child shape indices in a collision for a compound shape

Post Reply
aFerreira
Posts: 9
Joined: Fri Dec 04, 2015 10:30 pm

Detection of child shape indices in a collision for a compound shape

Post by aFerreira »

Hello!

In my engine i need to detect which body part is hit by an object to get different damage values per body parts. This would be easily done if i could detect which child shape (or child shape index) was hit inside the compound shape, but i am not finding an easy way to do it.

The collision detection is done via iterating the manifold and get the correct pair:

Code: Select all


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

	int numContacts = contactManifold->getNumContacts();

	if (numContacts > 0)
	{
		// If object B is an humanoid
		if (obB->getUserIndex() == 1)
		{
			for (int j = 0; j < numContacts; j++)
			{
				btManifoldPoint pt = contactManifold->getContactPoint(j);
				
				std::cout << "Index0: " << pt.m_index0 << std::endl;
				std::cout << "Index1: " << pt.m_index1 << std::endl;
				std::cout << "Index0: " << pt.m_partId0 << std::endl;
				std::cout << "Index1: " << pt.m_partId1 << std::endl;

			}
			
		}
	}
}
I have read in the previous threads in this forums that you could use a customcallback to do it, but my question is that why cant i use m_index0 and m_index1 members directly?

If i print these numbers it gives me random/gibberish results but shouldnt this be the direct indices as explained here?
https://www.bulletphysics.org/Bullet/ph ... =9&t=10617

Many thanks,
André
Last edited by aFerreira on Wed May 09, 2018 3:27 pm, edited 2 times in total.
aFerreira
Posts: 9
Joined: Fri Dec 04, 2015 10:30 pm

Re: Detection of child shape indices in a collision pair

Post by aFerreira »

As an example of output when run with some extra information:

Code: Select all

Is obA Compound: 0                                                                                                    
Is obB Compound: 1                                                                                                      
Index0: 32766                                                                                                          
Index1: -92442352                                                                                                      
Part0: 204                                                                                                              
Part1: 530204274  
Clearly the indices are not correct ...
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Detection of child shape indices in a collision for a compound shape

Post by drleviathan »

I looked through the code and AFAICT it should Just Work.

I tried printing out btManifoldPoint::m_index0 and m_index1 in my own project and discovered:

(1) When I collide a single convex shape against a btCompoundShape: one of the indices is valid and the other is some wacky value: it appears to be uninitialized.

(2) When I collide two convex shapes together both indices appear to be uninitialized.

(3) When I collide two btCompoundShape's together both indices appear to be correct.

Looking back at the code more carefully this makes sense: btManifoldPoint::m_index0 and m_index1 are indeed uninitialized data members.
hyyou
Posts: 96
Joined: Wed Mar 16, 2016 10:11 am

Re: Detection of child shape indices in a collision for a compound shape

Post by hyyou »

^ Because of the uninitialized value, I have to remember type of the shape in my own code.

Great answer, drleviathan! ... It is also consistent with what I noticed.

To reply OP's email, yes, I believe query the "index" is the best way. It is probably the only way.
AFAIK, Bullet's API doesn't provide access to child btShape* directly.
aFerreira
Posts: 9
Joined: Fri Dec 04, 2015 10:30 pm

Re: Detection of child shape indices in a collision for a compound shape

Post by aFerreira »

hyyou wrote: Thu May 10, 2018 2:54 am ^ Because of the uninitialized value, I have to remember type of the shape in my own code.

Great answer, drleviathan! ... It is also consistent with what I noticed.

To reply OP's email, yes, I believe query the "index" is the best way. It is probably the only way.
AFAIK, Bullet's API doesn't provide access to child btShape* directly.
drleviathan wrote: Wed May 09, 2018 5:42 pm I looked through the code and AFAICT it should Just Work.

I tried printing out btManifoldPoint::m_index0 and m_index1 in my own project and discovered:

(1) When I collide a single convex shape against a btCompoundShape: one of the indices is valid and the other is some wacky value: it appears to be uninitialized:

(2) When I collide two convex shapes together both indices appear to be uninitialized.

(3) When I collide two btCompoundShape's together both indices appear to be correct.

Looking back at the code more carefully this makes sense: btManifoldPoint::m_index0 and m_index1 are indeed uninitialized data members.
Thank you very much for your responses!

So drleviathan, your findings are exactly as i would expect it to work because it makes perfect sense. However it doesn't seem to line up exactly to what i am seeing:

(1) In the prints i sent shape A was a simple box shape (btBoxShape) and the collision detection worked perfectly, however as you saw both indices appear initialized instead of just one as shape B is a compound:

Code: Select all

Is obA Compound: 0                                                                                                    
Is obB Compound: 1                                                                                                      
Index0: 32766                                                                                                          
Index1: -92442352                                                                                                      
Part0: 204                                                                                                              
Part1: 530204274
 
(2) If i change shape A to be a compound (using the exact same box shape as a child) the collision detection almost stops working (detects 1 in 50)
but now it gives the correct indices:

Code: Select all

Is obA Compound: 1
Is obB Compound: 1
Index0: 14
Index1: 0
Part0: -1
Part1: -1
(3) if i change shape A to be a triangle convex shape (a box) then one of them gives a correct index but again the collision detection almost stops working:

Code: Select all

Is obA Compound: 1                                                                                                      
Is obB Compound: 0                                                                                                     
Index0: 16                                                                                                             
Index1: 9                                                                                                               
Part0: -1                                                                                                               
Part1: 0 
The reason for the collision almost stopping to work could be because they are moderately high speed "projectiles". However it should also work using one of bullet primitive shape as these are the fastest and more reliable to use.

So this all appears very strange to me, perhaps some property not correctly set in my code or maybe a bullet issue?
aFerreira
Posts: 9
Joined: Fri Dec 04, 2015 10:30 pm

Re: Detection of child shape indices in a collision for a compound shape

Post by aFerreira »

So i discovered something very interesting, after some testing i got this using (1), a native btBoxShape:

Hit1:

Code: Select all

Is obA Compound: 0
Is obB Compound: 1
Index0: 32760
Index1: -697672656
Part0: 254
Part1: 897533554
Hit 2:

Code: Select all

Is obA Compound: 0
Is obB Compound: 1
Index0: 32760
Index1: -697672656
Part0: 254
Part1: 897533554
Hit 3:

Code: Select all

Is obA Compound: 0
Is obB Compound: 1
Index0: 32760
Index1: -697672656
Part0: 254
Part1: 897533554
Hit 4:

Code: Select all

Is obA Compound: 1
Is obB Compound: 0
Index0: 16
Index1: 0
Part0: -1
Part1: 615
So as you can see it only shows a correct indice if obA is the compound shape and not obB. The problem is that with only detecting when obA is a compound shape, not all of the collisions are detected (about 1 in 5).

This does not seem like intended behavior, can anyone confirm?
aFerreira
Posts: 9
Joined: Fri Dec 04, 2015 10:30 pm

Re: Detection of child shape indices in a collision for a compound shape

Post by aFerreira »

Still no easy solution to this at sight, bump :|
Post Reply