1
\$\begingroup\$

I have many sensors(coins) in each level and I want to remove the sensors(coins) when the player rectangle(Mario) touches them. I check in OnCollision if Mario touches a coin, if he touches it, it should be removed. But I don't know how to remove a body.

What is the best way to remove a body?

How can I remove a coin in this example?

bool Player_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
    {
      if (fixtureB.CollisionCategories == Category.Cat10)
        {

        }
        return true;
    }

 coinrect = BodyFactory.CreateRectangle(world, 0.64f, 0.64f, 1.0f);
 coinrect.BodyType = BodyType.Static;
 coinrect.Position = new Vector2(1.60f, 4.16f);
 coinrect.CollisionCategories = Category.Cat10;

 coinrect2 = BodyFactory.CreateRectangle(world, 0.64f, 0.64f, 1.0f);
 coinrect2.BodyType = BodyType.Static;
 coinrect2.Position = new Vector2(2.60f, 3.16f);
 coinrect2.CollisionCategories = Category.Cat10;

 playerrect = BodyFactory.CreateRectangle(world, 0.64f, 0.64f, 1.0f);
 playerrect.BodyType = BodyType.Dynamic;
 playerrect.Position = new Vector2(0.22f, 0f);
 playerrect.FixedRotation = true;

 playerrect.OnCollision += Player_OnCollision; 
\$\endgroup\$
1
\$\begingroup\$

Body.Dispose(bodyhere) is what you are looking for. It removes the body as well as all attached features. If you do not want to move Body.Dispose you can use World.RemoveBody(bodyhere) which removes the body after the current step. I would recommend you use the latter though.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.