1
votes

I have been looking into box2d (in java with libgdx) lately and have been trying to create my own custom bounce effect (I increase the Restitution after the first bounce)

To do this as a test I simply checked the location of the object and waited for the first bounce.

But now I wanted to actually implement this and came across a problem: How to detect the collision of 2 specific object in box2d?

I found this tutorial: box2d collision detection - but I am very reluctant to use that code. There must be a simpler and cleaner way to detect a collision between 2 objects (without having to set user data and checking all collisions with giant if() conditions...)

Can anyone help me out? (assuming I am not just hopeful and there actually is a better way)

2

2 Answers

2
votes

Subclass b2ContactListener class to handle the collision and reimplement collision callbacks. Then just:

MyContactListener *listener = new MyContactListener();
myB2World->SetContactListener(listener);
0
votes

And take note, some solutions don't have shortcuts as always but you can find the right solution and there is possibly a hundred ways to detect a collision and you know it, don't you? This is like in the solution of box2d collision detection program for example.

http://blog.allanbishop.com/box2d-2-1a-tutorial-%E2%80%93-part-4-collision-detection/