How to respond to collisions

wasp
Posts: 7
Joined: Tue May 04, 2010 6:19 am

How to respond to collisions

Post by wasp »

I am a hobbyist writing a Tetris clone with physics. I am writing it in Java (don't hate) using the JBullet port. It began as a class assignment, but has grown far beyond the requirements (obviously).

The way I have Tetris set up, the currently dropping piece is given a force to counter gravity and an initial downward velocity. When it touches another object (besides the walls), or the next piece is created, it is released from the counter-gravity force and left to settle naturally. Right now I am having difficulty getting JBullet to tell my application when this collision happens. Creating the next piece successfully releases the old one.

I have read this page in the wiki

http://bulletphysics.org/mediawiki-1.5. ... d_Triggers

and I have tried both a ContactAddedCallback and a ContactProcessedCallback, but debugging show that neither is ever called. I have registered this callback with BulletGlobals and every new piece is given the CUSTOM_MATERIAL_CALLBACK CollisionFlag.

Could someone push me in the right direction? Am I missing a step to getting the callbacks to work? Is there a better way to respond to this type of collision?

Oh, and it might be better to communicate in the abstract, rather than code, so we can avoid the Java/C++ language barrier.

Thanks, all.
yinono
Posts: 2
Joined: Thu Apr 29, 2010 2:15 pm

Re: How to respond to collisions

Post by yinono »

Just starting with Bullet myself.
I couldn't make the callback stuff works so I opt to iterating through the contactManifolds and if I have a manifold with the 2 btCollisionObject's I'm interested in and the number of contact point is bigger then 0, I know I have a collision then I call myself to my collision callback.
I also implemented a mechanism to throw CollisionOn and CollisionOff events, using two sets where I keep all the pairs of colliding objects from the previous frame and compare to the set of all pairs in the current frame. This way I know which pairs just collided now and which just got separated this frame. I can post a source sample if you're interested (In c++).
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: How to respond to collisions

Post by Flix »

yinono wrote:I also implemented a mechanism to throw CollisionOn and CollisionOff events, using two sets where I keep all the pairs of colliding objects from the previous frame and compare to the set of all pairs in the current frame. This way I know which pairs just collided now and which just got separated this frame. I can post a source sample if you're interested (In c++).
This solution looks similiar to the one I posted some time ago in a modified "Basic Demo" (with source code) that forwards collision detection events (here: http://www.bulletphysics.org/Bullet/php ... php?id=343, in this forum thread: http://www.bulletphysics.org/Bullet/php ... 91&start=0).

@ wasp:
It's very easy to integrate my code into existing applications, but unluckily it's C++, not Java!
You may take a look at it just in case you want to port it.
wasp wrote:Oh, and it might be better to communicate in the abstract, rather than code, so we can avoid the Java/C++ language barrier.
Well, the explanation of yinono is good enough, in addition I calculate the "average" contact point before sending it to the user (it depends if you want callbacks for each contact point or callbacks for each collision object pair, and if you really need the contact point info (many times you just need the ptrs to the two bodies)).

Hope it helps, but I think that all this could be a bit difficult to be done in Java...
(It was not too easy to be done in C++...)
Good luck!
wasp
Posts: 7
Joined: Tue May 04, 2010 6:19 am

Re: How to respond to collisions

Post by wasp »

Ok, I've got it working. I used a variation on the code in the third post in the topic linked by Flix. Obviously I switched up the second for loop. I won't go into more details unless someone is curious.

This solution is not ideal, but given how few objects I have, it shouldn't be a problem.

Thanks, all!