Collision detection: knowing who hit who

User avatar
chaosavy
Posts: 29
Joined: Wed Nov 11, 2009 9:09 pm

Collision detection: knowing who hit who

Post by chaosavy »

I'm using the collision only aspect of Bullet along with Ogre. Its been a great asset and fairly easy to integrate. Thank you.

I have a question:

I took this code from the examples and integrated it into my project:

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



And I don't understand how bullet decides how to define obA and obB, I went with the assumption that obA is the hitter/mover and obB is the hittee. But currently I find that this assumption is incorrect, for example I have object1 and object2, object 1 is standing stationary, object2 is heading towards object1 and hits it. But obA is object1 (the stationary object) and obB is the moving object.

My code then goes and calculates the momentum of the two objects and decides how to respond (slow down the moving object, and speed up the hit object). But all of that depends on this assumption that obA is the mover and obB isn't, and that assumption is wrong.

Could I get some clarification? How can I determine who is the mover and who is the hit? In some cases my assumption is correct and in others it isn't.

thank you
User avatar
chaosavy
Posts: 29
Joined: Wed Nov 11, 2009 9:09 pm

Re: Collision detection: knowing who hit who

Post by chaosavy »

It looks like this is the path I need to go on:

http://www.bulletphysics.org/Bullet/php ... f=9&t=4136

The scalar returned is 0.00 (is this because I'm using collision only and not setting anything up except the mesh collision object). Either way I wouldn't know what to do with the scalar.
Jasonrun
Posts: 11
Joined: Fri Oct 30, 2009 7:44 pm

Re: Collision detection: knowing who hit who

Post by Jasonrun »

I don't think it should matter, as long as you are doing your response based on simple Newtonian physics... you just need to know both objects velocities and masses before the collision and then you can calculate them both for after... conservation of momentum. For better responses you'd obviously need to take more into consideration such as rotation and shape...

If you really want to know which one was moving the fastest (the hitter) just compare their linear velocities.
I'm still learning Bullet myself, but these should help.

Code: Select all

obA->getInterpolationLinearVelocity();
I have only worked with dynamic worlds so far though... I haven't fully figured out the full meaning of the interpolation features.
I'm not sure if it would work in a collision world, but if you upcast it as a btRigidBody you get another linear velocity option.

Code: Select all

btRigidBody* body = btRigidBody::upcast(obA);
body->getLinearVelocity();
User avatar
chaosavy
Posts: 29
Joined: Wed Nov 11, 2009 9:09 pm

Re: Collision detection: knowing who hit who

Post by chaosavy »

so far what I've been doing is:

assuming that obA is the mover/hitter and obB is the hittee, also the game takes place in space

First method I call bouncyness method:
I take the momentum of obA reverse it, then apply it (corrected for orientation) to obB
what happens is a neat bounce of obA and obB from eachother, not very realistic

Next method I did was take the size of obA and obB (using bounding boxes x*y*z)
compare them and get the % difference between them

then I decelerate obA by this percent and accelerate obB by this percent
this gave me a neat little push effect

however this all falls apart when obA is the hittee and obB is the mover/hitter

As you can probably tell I'm not too concerned about realism, rather I'm more concerned about what feels correct in my terms/mind, for example I don't necessarily want the space ships to immediatly explode on impact, I'd rather them bounce off/decelerate.

It sounds like I need to dig up the formulas for what you describe, and then I don't have to worry about obA/obB and their individual roles, rather I take them as partners. If you have some formulas handy for what you described I would appreciate the help.
Jasonrun
Posts: 11
Joined: Fri Oct 30, 2009 7:44 pm

Re: Collision detection: knowing who hit who

Post by Jasonrun »

You sort of just have to combine the two things you did.
http://en.wikipedia.org/wiki/Momentum#C ... r_momentum has a simple enough description of the formula and process that you should be able to implement it easily. If you don't really use masses you can just assume equal densities and use your volumes (x*y*z) as the masses.
User avatar
chaosavy
Posts: 29
Joined: Wed Nov 11, 2009 9:09 pm

Re: Collision detection: knowing who hit who

Post by chaosavy »

thanks... its taking a bit of head scratching, but I think I'm starting to understand, and yeah the formulas are half way similar to what I was doing, I didn't have the 2nd portion of the formula that follows this text: "In one dimension
When the initial velocities are known, the final velocities for a head-on collision are given by"

but I was doing something like the math in the first part, except not having the 2nd part I had to compensate for when the objects had equal mass (which occurs quite often) since I was using the bounding box method with exactly the same size meshes.