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

[SOLVED] Circle collision without having an actual farseer circle?

Topics: Developer Forum
Jul 21, 2014 at 9:51 AM
I have an explosion going off and I want to detect all fixtures overlapping a circle (the explosion) for just one update. I don't really want a sensor as I only need to check this very occasionally. I know you can do some stuff outside of the normal flow with QueryAABB and TestPoint but can you test for collision against a circle?

I don't want a sensor because then I will have to wait until the spell hits, and instead of being able to do stuff right away I will have to move and setup the sensor and then store all the collisions in a list and then operate on them in the next game update which seems like a lot of work.
Jul 23, 2014 at 7:22 AM
Hello all I dug into how sensors work and have come up with a fix that seems to work:
        internal static List<Fixture> GetFixturesCollidingWithCircle(Vector2 position, float radius)
            var vectorRadius = new Vector2(radius, radius);
            FarseerPhysics.Collision.AABB queryRegion = new FarseerPhysics.Collision.AABB(position - vectorRadius, position + vectorRadius);
            List<Fixture> broadPhaseHits = Factories.PhysicsComponents.World.QueryAABB(ref queryRegion);

            FarseerPhysics.Collision.Shapes.Shape testShape = new FarseerPhysics.Collision.Shapes.CircleShape(radius, 1f);
            var testRotation = new FarseerPhysics.Common.Rot(0f);
            var testTransform = new FarseerPhysics.Common.Transform(ref position, ref testRotation);
            // since our test shape is a circle there will always be exactly 1 Child
            int testChildIndex = 0;

            var hitFixtures = new List<Fixture>();
            foreach (var broadPhaseFixture in broadPhaseHits)
                for (int childIndex = 0; childIndex < broadPhaseFixture.Shape.ChildCount; childIndex++)
                    FarseerPhysics.Common.Transform transform;
                    broadPhaseFixture.Body.GetTransform(out transform);
                    if (FarseerPhysics.Collision.Collision.TestOverlap(testShape, testChildIndex, broadPhaseFixture.Shape, childIndex, ref testTransform, ref transform))
            return hitFixtures;