This project has moved and is read-only. For the latest updates, please go here.

Collision Count with circles

Topics: Developer Forum, User Forum
Jan 19, 2011 at 6:22 PM

I have been making a little game that allows the user to set up all the fixtures they want with the mouse without physics activated. When they are done placing they activate the physics. I'm currently attempting to make it so the user cannot place fixtures over eachother. I thought about oncollision, but i need this to work for an ever changing enviroment. For time sake i just had 

if (contact count on picking up fixtures != contact count on droping fixture)

move fixture back to it's original location

The issue i'm having is with circles. I can drag the circles next to each other or on top one another really close and keep contact count to 0, but if they are diagonal from each other without touching the contact count goes up.

The left side has 0 collision count, but the right has 1. It's almost like there is a square around the circles. Is there a way to get around this? broad phase, late phase? 

Jan 19, 2011 at 6:35 PM

Contacts are only created when the circles touch each other. There are indeed a rectangle around the circles (AABB) that is used in the broad phase, but it does not create contacts.

The easiest for you is probably to not use the engine while in design mode. You should not add the bodies to the engine before it is needed.

Instead you should manually check for collisions between the shapes. You can use the static Collision class for that: CollideCircles.CollideCircles(...). The Manifold reference you give the CollideCircles will be populated with the collision data. You then simply check Manifold.PointCount to check if the circles collided.

Jan 19, 2011 at 7:00 PM

Currently i'm using the fixtures and world.TestPoint(...) to find the fixture i'm selecting so removing the bodies would prevent that, right? Also can you provide a sample or pseudocode for the CollideCircles class? Also, should i create a manifold or is there one i should just plug in from somewhere else.

Jan 19, 2011 at 9:05 PM

You can't use World.TestPoint then because it uses queries inside the broadphase to look for fixtures in an efficient manner.

There is a Test in the Testbed called PolyCollisionTest that shows you how to use the Collision class. You simply supply it with a Manifold object, the two polygons and their transforms (position and rotation).

Jan 20, 2011 at 6:36 PM

Is there a way i could possibly take a fixture's shape and turn it into a polygonshape? Is there something i can use to extract the vertices of the circle?

Jan 20, 2011 at 8:59 PM

You can always cast the shape to a polygon shape (if it is a of type PolygonShape):

PolygonShape shape = myShape as PolygonShape;

if (shape != null)
   Vertices v = shape.Vertices);

You can't get vertices from a circle as it is not a polygon - it is a circle. You can however approximate the circle using PolygonTool.CreateCircle().