Please help with some basic problems!

mohican
Posts: 17
Joined: Mon Aug 25, 2008 5:12 pm

Please help with some basic problems!

Post by mohican »

Hi all!
I have been searching on the forum for answers to these questions, but could not find them.
I feel a bit silly, because I know these are basic questions... anyway here goes:


(Question 1)

I keep reading everywhere that btRigidBody is derived from btCollisionObject.
There should be an easy way to know which btRigidBody is linked to which btCollisionObject, but I cant find it.
I create my bodies using:

Code: Select all

Bullet_Objects[id].rigidBody = btRigidBody(mass, defaultmotionState, colShape, local_inertia);
m_dynamicsWorld->addRigidBody(&Bullet_Objects[id].rigidBody); 
I then use the contactManifold to find out which objects are currently in contact.

Code: Select all

int numManifolds = m_dynamicsWorld->getDispatcher()->getNumManifolds();
for (int i=0; i<numManifolds; i++)
{
    btPersistentManifold* contactManifold = m_dynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i);
    btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
    btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());
}
The contactManifold return pointers to btCollisionObjects, not rigid bodies.
So, in the code above how can I find out the "id", in my array Bullet_Objects[id].rigidBody, of obA and obB?


(Question 2)

I know how to step through the simulation using m_dynamicsWorld->stepSimulation.
But how can I make instant collision detections using m_dynamicsWorld?
My reason for this is that I want to add objects, but I want to make sure they do not collide with existing objects, otherwise I create a "mini" explosion.


(Question 3)

The contact manifold tells me which object are currently in contact with each other, and at which points.
But how do I find out if 2 objects collided at speed?
This is because i want to play sound effects when I get collisions between say 2 wooden crates.


Thanks in advance for your help!
User avatar
mirat
Posts: 16
Joined: Thu May 29, 2008 10:47 am

Re: Please help with some basic problems!

Post by mirat »

The contactManifold return pointers to btCollisionObjects, not rigid bodies.
Just cast pointer returned by the contactManifold->getBody0() to btRigidBody * type. It will be valid, if you added only btRigidBodies to world and there are no btCollisionObjects in the world. More safe way is to store some additional information in all objects by setting setUserPointer() pointing to some kind of object descriptor and getting it by getUserPointer().
(Question 2)

I know how to step through the simulation using m_dynamicsWorld->stepSimulation.
But how can I make instant collision detections using m_dynamicsWorld?
My reason for this is that I want to add objects, but I want to make sure they do not collide with existing objects, otherwise I create a "mini" explosion.
Add your object to the world by addCollisionObject(), then call world->performDiscreteCollisionDetection(). After that you can get manifold points from dispatcher. and if one exists, remove object or make explosion.
(Question 3)

The contact manifold tells me which object are currently in contact with each other, and at which points.
But how do I find out if 2 objects collided at speed?
This is because i want to play sound effects when I get collisions between say 2 wooden crates.
As I understand, you need to play sound only when collision occured first time. Use gContactAddedCallback.

That is my imho.
mohican
Posts: 17
Joined: Mon Aug 25, 2008 5:12 pm

Re: Please help with some basic problems!

Post by mohican »

You answered all my questions, that's fantastic!
I will get onto my code straight away, and let you know how this gets on!
mohican
Posts: 17
Joined: Mon Aug 25, 2008 5:12 pm

Re: Please help with some basic problems!

Post by mohican »

I was able to implement your suggestions for (1) and (2)

Unfortunately, my application does not fit well with the use of callbacks for (3).
Question: Is there any way for me to retrieve the indexes of the Manifolds that were added in the last simulation step?
I could then analyse those manifolds using m_dynamicsWorld->getDispatcher()->getManifoldByIndexInternal().
User avatar
mirat
Posts: 16
Joined: Thu May 29, 2008 10:47 am

Re: Please help with some basic problems!

Post by mirat »

One of the ways is to implement btOverlappingPairCallback interface by your own class, e.g MyCallback. That interface has method addOverlappingPair. You need to overload it and handle all added pairs. btGhostPairCallback is an example of implemeting btOverlappingPairCallback.

btAxisSweep3 has method setOverlappingPairUserCallback which can be used to make broadphase use your MyCallback. In that case btAxisSweep3 will dublicate all pair related events to its internal pair cache and to your callback.

If you are using other broadphase implementation (setOverlappingPairUserCallback defined only in btAxisSweep3), other way is to call broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(), but in that case you will not be able to use btGhostObject's.

There are also many pervese ways :) such as btDispatcher subclassing/reimplementing or others.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Please help with some basic problems!

Post by Erwin Coumans »

But how do I find out if 2 objects collided at speed?
A btManifoldPoint contact point has a life-time (m_lifeTime), and an applied impulse (m_appliedImpulse) member. This information could be helpful for sound playback.

Hope this helps,
Erwin
cobolt_dink
Posts: 72
Joined: Fri Apr 04, 2008 6:07 pm

Re: Please help with some basic problems!

Post by cobolt_dink »

Erwin Coumans wrote:
But how do I find out if 2 objects collided at speed?
A btManifoldPoint contact point has a life-time (m_lifeTime), and an applied impulse (m_appliedImpulse) member. This information could be helpful for sound playback.

Hope this helps,
Erwin
m_lifeTime is always zero for me in either the tick callback or the contact added callback.

Something I've noticed is the system of using the applied impulse and the collision distance breaks down with large amounts of blocks. When they fall into each other from a good height they are 'crushing' each other on the way down. Seems to be no way of having a system of playing sounds that works with both small distances and large ones. A threshold that works for a small drop is not nearly high enough for a object dropped from a great height. Even dropping one block one unit on to another block generates a bunch of contact added callbacks. Would be nice if it was truely called only once with two objects touch and not again until they were seperated.
mohican
Posts: 17
Joined: Mon Aug 25, 2008 5:12 pm

Re: Please help with some basic problems!

Post by mohican »

I still cannot figure out what you guys mean with the callbacks... :(

It is a shame 'btPersistentManifold' does not contain a field that records "when" specific contact manifolds were generated during the simulation process, or at least something telling you whether it was generated during the last call to stepSimulation.
mickey
Posts: 107
Joined: Fri Sep 19, 2008 6:08 pm

Re: Please help with some basic problems!

Post by mickey »

Hi

You can use among the 3 various ways the collision callback is triggered during a collision - as described here:

http://www.bulletphysics.com/mediawiki- ... d_Triggers

Callbacks are function pointers in c/c++ - or a pointer to a function.

Basically you give Bullet's collision callback your function (making sure you follow the function signature/prototype), so when a collision happens, Bullet would report that to your supplied function.

mirat's suggestion was to override an existing callback that Bullet calls naturally in its system, that's another way too for your app to be notified.

Hope that helps.