2
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ In the C++ version, there exist contact callbacks for this exact purpose. Are those not in the Flash port? \$\endgroup\$
    – Anne Quinn
    Commented Sep 17, 2011 at 2:43
  • \$\begingroup\$ 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 \$\endgroup\$
    – Jason
    Commented Sep 18, 2011 at 5:37
  • \$\begingroup\$ possible duplicate of how to ignore physics collision of some objects in box2d \$\endgroup\$ Commented Mar 8, 2013 at 4:32

3 Answers 3

3
\$\begingroup\$

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

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – Ali1S232
    Commented Sep 16, 2011 at 19:26
  • \$\begingroup\$ Yea I guess this is the only way - Thanks for the tip on filtering :) \$\endgroup\$
    – Jason
    Commented Sep 16, 2011 at 20:27
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – Jason
    Commented Sep 16, 2011 at 20:26
1
\$\begingroup\$

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

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .