I have there serveral platforms. I would like to allow the player to collide only one time with the object! Hope you can help me
2 Answers
Farseer Bodies have a method like so:
body1.IgnoreCollisionWith(body2);
What you are asking for is so that a body will only collide with another body once, so once those two bodies are separated from each other, they will no longer collide. Luckily, Farseer bodies come with an OnSeparation
event.
body.OnSeparation += OnSeparation;
private void OnSeparation(Fixture fixtureA, Fixture fixtureB)
{
fixtureA.Body.IgnoreCollisionWith(fixtureB.Body);
}
This will allow those two bodies to collide once, but after they separate they will no longer collide.
Depending on how your game works though, the OnSeparation
event might be too sensitive, so you could handle it differently by keeping track of bodies that are colliding in the OnCollision
event and then triggering something after a certain period of time, for example. The principle will remain the same however, still using the IgnoreCollisionWith(body)
method.
You might also want to look into Collision Categories. You can define a Body's collision category and also which categories it collides with. This way, a body can avoid collisions with a whole group of other bodies, without having to use IgnoreCollisionWith
against every single one of those bodies.
Well you can always attach an event to the bodies and when they collide with something check their ID's and only allow the collision if that was the first time (could do that with a bool)
bool thingy=true;
Body1.OnCollision += new OnCollisionEventHandler(Body_OnCollision);
bool Body_OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact)
{
if (fixtureA.FixtureId == 1 && fixtureB.FixtureId == 2 && thingy == false)
{
thingy = true;
return true;
}
else
return false;
}