Sunday, June 10, 2018

xna - Farseer: Only allow the collision one time with the object



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



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...