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

OnCollision not being called for two bodies when using XNA 4.0 and FarSeer 3.3

Topics: User Forum
Sep 29, 2013 at 10:48 PM
I admit, I'm very new to FarSeer physics. I plan on using it for it's collision detection system in an XNA project. I've looked around quite a bit, tried many things and haven't had any luck solving my problem, I hope someone with more experience can help me out.

Essentially, I've created two bodies using BodyFactory.CreateRectangle. In the project I update their positions manually (no gravity or velocity is used), then place the Sprite/XNA rectangle onto the position of the FarSeer bodies. This works fine, using the ConvertUnits functions the positions match up exactly. The trouble comes when I want the two bodies to collide.

During LoadContent I assign the player body collision a function to call (Player_Body.OnCollision += FarSeer_Collision)
However, when the two bodies collide, this function is never called.

I do not know what I'm doing wrong and I would love some help. Below is how I've created the bodies for the Player and the other object, as well as the FarSeer_Collision that I would like to call when the two objects collide.
Player_Body = BodyFactory.CreateRectangle(world, FarseerPhysics.SamplesFramework.ConvertUnits.ToSimUnits(Players[0].Rect.Width), FarseerPhysics.SamplesFramework.ConvertUnits.ToSimUnits(Players[0].Rect.Height), 1.0f, FarseerPhysics.SamplesFramework.ConvertUnits.ToSimUnits(Players[0].Position));
            Player_Body.BodyType = BodyType.Static;
            Player_Body.CollidesWith = Category.All;
            Player_Body.CollisionCategories = Category.All;

public bool FarSeer_Collision(Fixture f1, Fixture f2, Contact contact)
            return true;
Is there something else I have to do before the two objects will "collide"? I apologize if this is a simple mistake and would greatly appreciate any advice. I also apologize if this has been asked and solved before, I have searched but haven't had any luck. I am also more than happy to provide more information if needed.
Sep 29, 2013 at 11:31 PM
I think you need to set at least one of your bodies to BodyType.Dynamic for the engine to check for collisions. Also make sure you're calling World.Step() in your update method.

Also, moving bodies my setting the Position vector isn't good practice. Use Body.ApplyForce() or at least set Body.LinearVelocity to get your body moving around e.g. Player_Body.ApplyForce(new Vector2(0.1, 0)) would start moving your body to the right.

Have fun
Sep 30, 2013 at 1:36 AM
Ah! Setting one to Dynamic completely fixed that problem! Thank you so much, I feel a bit stupid now that it was such a simple fix... I had tried setting both to Dynamic, but never one at a time...

I know setting Position isn't a great idea, I was using it until I can figure out a good way to use the methods you mentioned in the way I would like.

Again, thanks for the quick and helpful response!