0

So, I have some circles which I want to bounce from the edge of the map (i.e. rectangle) but they should not bounce from each other.

Problem is, I still need to capture circle-circle collisions and perform some task on such events.

Can anyone point me the direction I should go?

For clarification:

I am using libGDX and Box2D, circles are DynamicBodies. In the middle of the map there might be some static bodies, which circles should also normally colide with (bounce from them).

What I tried is to set restitution to 0 for both circles at the beginning of contact and restore restitution at the end of the contact (in my ContactListener subclass). Also tried temporary set circles to be sensors. Am not sure why it didn't work, though.

lukaszb
  • 694
  • 1
  • 6
  • 15

3 Answers3

3

You can set filters to your circles, so they will collide with edge of map, but do not collide with themselves. How to do this, you can read at box2d manual http://www.box2d.org/manual.html, at section "Filtering".

To capture circle-circle collisions, you can do next:

  1. At each world step foreach your circles one by one.
  2. When looking at some circle, make AABB query by it's AABB. What is AABB query and how to make it look at box2d manual at section "AABB Queries". To get AABB of circle, call b2Fixture::GetAABB.
  3. After AABB query, you will get count of fixtures, wich possibly are collide with your circle. To test certain, use b2TestOverlap function, or simple calculate distance between centers.

Good luck!

Pavel
  • 2,602
  • 1
  • 27
  • 34
0

From a high-level point of view, you would program something like that in your application logic:

Bouncing from a map is done using some if-conditions: check e.g. if the x-value of the circle center minus its radius is smaller than the x-component of the map; if this is the case, invert the x-direction of the circle movement vector.

Collision detection of two circles is just a comparison of the length of the connection vector between both centers against the sum of both radius; if it is smaller or equal, they are intersecting each other.

Edit: Oh I'm sorry, I did not see that you use some kind of physics library. I haven't used libgdx, but in other frameworks, you could define collision groups that specify, which objects can collide with each other and then handle collisions of objects in these groups accordingly. Perhaps libgdx offers a similar concept as well?

Sven Viehmeier
  • 418
  • 4
  • 11
  • Sven, thanks for your feedback however that's not actually the answer for my question (however I might not be precise enough in the first place - I started using Box2D in order not to reinvent the wheel: had struggled with own, extremely simple physics already but it soon started to be annoying to add features [and the performance was pretty bad too]). – lukaszb Jul 24 '12 at 22:37
0

thanks to iforce2d's comment I could find proper answer in the following thread: How to detect collision but do not collide in box2d?

As in the question: I wanted to react on collision events but make circles not collide with each other at the same time. So, sensor wasn't the answer, changing physics attributes of the bodies neither. Proper answer was to disable contact in a preSolve method of the CollisionListener (contact.setEnabled(false)). Simple as that.

Community
  • 1
  • 1
lukaszb
  • 694
  • 1
  • 6
  • 15