Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
add comment

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;
        }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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