Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)

Help is greatly appreciated

Thanks, Jason

share|improve this question
In the C++ version, there exist contact callbacks for this exact purpose. Are those not in the Flash port? – Anne Quinn Sep 17 '11 at 2:43
Its a Java port and as far as I can tell there is only one general contact listener where you get all contacts that happened – Jason Sep 18 '11 at 5:37
possible duplicate of how to ignore physics collision of some objects in box2d – Sean Middleditch Mar 8 '13 at 4:32
up vote 3 down vote accepted

I think ultimately you will have to use user data but you can greatly reduce the number of collisions to check with collision filtering.

share|improve this answer
Collision filtering has some other use, it just asks if collision between two objects should be checked or not. if you filter collisions they don't collide at all, not that box2d doesn't give a report for some collisions – Ali.S Sep 16 '11 at 19:26
Yea I guess this is the only way - Thanks for the tip on filtering :) – Jason Sep 16 '11 at 20:27

Add a trigger either to your moving ball or to the ground. A trigger is an volume causing some custom game action. Like reaching the end of the level or if a switch can be activated etc.

Your trigger class should know which objects caused the trigger to fire and can do the actual bouncing. Not sure how to do this with box2D though.

share|improve this answer
Yea the box2d part is the problem - never mind ill use UserData I'll just try to make it nice with filtering and better code writing then in the demo. Thanks – Jason Sep 16 '11 at 20:26

Have you tried this editor and its FixtureAtlas? If not, try it out. It automates several steps to make this work.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.