Detecting collisions

Topics: User Forum
Nov 9, 2009 at 8:56 AM


I'm trying to use Farseer for detecting collisions. I mean I don't want to deal with gravity or anything.


I wrote code:


        public void Do()
            PhysicsSimulator physicsSimulator;
            physicsSimulator = new PhysicsSimulator();

            Body boxBody1;
            Geom boxGeom1;

            boxBody1 = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 100, 100, 1);

            boxBody1.Position = new FarseerGames.FarseerPhysics.Mathematics.Vector2(50, 50);
            boxBody1.IsStatic = true;

            boxGeom1 = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, boxBody1, 100, 100);
            boxGeom1.IsSensor = true;
            boxGeom1.OnCollision = new CollisionEventHandler(Collided);
            boxGeom1.CollisionResponseEnabled = false;

            Body boxBody2;
            Geom boxGeom2;

            boxBody2 = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, 100, 100, 1);
            boxBody2.Position = new FarseerGames.FarseerPhysics.Mathematics.Vector2(0, 0);

            boxGeom2 = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, boxBody2, 100, 100);
            Body polyBody1;
            Geom polyGeom1;
            Vertices vertices = new Vertices(new Vector2[] { 
                new Vector2(-50f, -50f), 
                new Vector2(-50f, 50f), 
                new Vector2(50f, 50f), 
                new Vector2(50f, -50f)});

            polyBody1 = BodyFactory.Instance.CreatePolygonBody(physicsSimulator, vertices, 1f);
            polyGeom1 = GeomFactory.Instance.CreatePolygonGeom(polyBody1, vertices, 0f);


        public bool Collided(Geom geometry1, Geom geometry2, ContactList contactList)
            return true;



It works with two rectangles (I have commented that section). But when I try to use polygon, it never goes to Collided-method.
Nov 9, 2009 at 1:49 PM

Change the line that says

polyGeom1 = GeomFactory.Instance.CreatePolygonGeom(polyBody1, vertices, 0f);


polyGeom1 = GeomFactory.Instance.CreatePolygonGeom(physicsSimulator, polyBody1, vertices, 0f);

Nov 9, 2009 at 3:23 PM

Ok, thank you for your response.

Mine problems is that I need to collide geometries to specified direction. With collide I mean moving until collision with another geometry. 

I've been trying to move geometry by setting Position-property of body-object and also tried setting LinearVelocity-property for geom. I'm sure Farseer can solve my problem. But I just can't figure it how would I do it..

Here's a picture of my problem:



Free Image Hosting at

Nov 10, 2009 at 9:40 AM

To move bodies around, you should use methods such as ApplyForce() or ApplyImpulse() rather than changing the position manually. After a force is applied, Farseer will then continue the motion for subsequent frames.