[Solved] How to destroy body on collision

Topics: Developer Forum, User Forum
Jul 23, 2012 at 5:35 PM

Im trying to make something like destroyable terrain made of small square boxes, then I need to add some extra action when my bodys collides. I have something like this: 


public void checkCollisiom()       


for (int i = 0; i < this.fix.Count;i++ )                this.fix[i].OnCollision += MyOnCollision;       


public bool MyOnCollision(Fixture f1, Fixture f2, FarseerPhysics.Dynamics.Contacts.Contact contact)       


if(f2.Body.BodyType == BodyType.Static)               

f2.Body.Position = new Vector2(0, 0);



checkCollision is in update.

Its not the best way to achive my goal... i was planning to remove bodys but i dont know how. I think there is a better way to do this, please help!

Jul 24, 2012 at 12:29 AM

Why not just use the World.RemoveBody(Body body) method when you detect the collision? (You will also need to disable/dispose the texture for that body too, I assume)

Jul 24, 2012 at 8:28 AM

Its good idea but I cant use my World inside function MyOnCollision because its impossible to add it to argument list MyOnCollision function.

Jul 24, 2012 at 12:55 PM

Just keep a reference to the world object somewhere. :)

Jul 25, 2012 at 1:22 AM

Body.Dispose() actually call World.RemoveBody() on the current body. You don't need a reference to World, you just need to call Dispose() on the body object.

Remember that it is better to return false in the OnCollision event when removing a body, this way the collision gets ignored and the engine don't have to calculate anything on a body that is going to be removed on the next update.

Jul 25, 2012 at 9:36 AM

Thanks Genbox!! It was very helpful! It is funny but I wasn't able to get reference to the world :)