How to continue moving in the same direction after collision

Topics: Developer Forum
Oct 24, 2013 at 10:13 AM
Hello again!

Is there some way to detect collision without the bounce so the first body flies through the second body?

I have a game where user shots the ball trough the air for collecting coins, but now when the ball hits the coin it bounces. Is there a way to detect the collision but ignore the bounce so the ball continues on its track.

Thanks in advance.
Oct 24, 2013 at 10:58 AM
Edited Oct 24, 2013 at 11:00 AM
Hi again!

Is the ball the only thing colliding with the coins?

I think there are two ways to solve this problem:
  1. You create an OnCollision event for the ball if you havent already and when the ball collides with the coin you return false after doing your logic.
  2. You make the coins sensors. These also have an OnCollision and collide but wont change the direction of your ball.
I would go with solution two, as in my eyes that is more elegant.

Solution 1:
        private bool OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact)
        {
            // Whichever detection you prefer to see what the colliding object is
            if (fixtureB.CollisionCategories == Category.Cat1 
               || fixtureB.UserData == "coin")
            {
                //give player cash and stuff
               return false;
            }
            return true;
        }
Solution 2:
        coin.Body.IsSensor = true;
        coin.Body.OnCollision += OnCollision;

        private bool OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact)
        {
            if (fixtureB.CollisionCategories == Category.Cat2 
                || fixtureB.UserData == "coin")
            {
               //give player cash moneys and stuff
            }
            return true;
        }
Oct 24, 2013 at 11:59 AM
Thank you, that works like a charm!
Oct 24, 2013 at 12:05 PM
No problem. If you have any other questions just ask and I will answer when I can.