How do i make game objects bounce after collision?

Topics: User Forum
Feb 26, 2013 at 12:25 AM
Hello,

i am making a game where you can control cars and collide into eachother. the collision works perfectly but now after the collision i would like to make the other car bounce off the players car after a collision.

(my game is a top down 2D game)

I have been messing around with this code: http://www.mediafire.com/?a1w242q9sna54j4

which was posted on this disscusion: http://farseerphysics.codeplex.com/discussions/406680

the code has a nice after collision bounce effect that i would like to use in my code.

here is my code using what i have learned from here and there

        public bool Player_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
        {           
            if (fixtureA.Body.UserData == "Car"
                && fixtureB.Body.UserData == "Car"
                ||  fixtureA.Body.UserData == "Car"
                && fixtureB.Body.UserData == "AI_Car"
                || fixtureA.Body.UserData == "AI_Car"
                && fixtureB.Body.UserData == "Car"
                || fixtureA.Body.UserData == "AI_Car"
                && fixtureB.Body.UserData == "AI_Car")
            {
                if (Cars_Collided == false)
                {
                    Cars_Collided = true;
                    numCarsHit += 2;
                    CarManager.Car_List[fixtureA.Body.BodyId].Wreck_Counter += 1;
                    CarManager.Car_List[fixtureB.Body.BodyId].Wreck_Counter += 1;
                }
                return true;
            }
            return false;   // i return false because there are other objects using this event               //handler that i dont want to physically collide with but rather use it as a zone collision
}

        public void AfterCollision(Fixture fixtureA, Fixture fixtureB, Contact contact)
        {
            if (fixtureA.Body.UserData == "Car"
                && fixtureB.Body.UserData == "Car"
                || fixtureA.Body.UserData == "Car"
                && fixtureB.Body.UserData == "AI_Car"
                || fixtureA.Body.UserData == "AI_Car"
                && fixtureB.Body.UserData == "Car"
                ||  fixtureA.Body.UserData == "AI_Car"
                && fixtureB.Body.UserData == "AI_Car")
            {
                float maxImpulse = 0f;
                for (int i = 0; i < contact.Manifold.PointCount; i++)
                    maxImpulse = Math.Max(maxImpulse, contact.Manifold.Points[i].NormalImpulse);

                currentImpulse = Math.Max(currentImpulse, maxImpulse);
            }
        }

        public void Player_OnSeparation(Fixture fixtureA, Fixture fixtureB)
        {
            if ( fixtureA.Body.UserData == "Car"
                && fixtureB.Body.UserData == "Car"
                || fixtureA.Body.UserData == "Car"
                && fixtureB.Body.UserData == "AI_Car"
                ||  fixtureA.Body.UserData == "AI_Car"
                && fixtureB.Body.UserData == "Car"
                ||  fixtureA.Body.UserData == "AI_Car"
                && fixtureB.Body.UserData == "AI_Car")
            {
                if (Cars_Collided == true)
                {
                    Cars_Collided = false;
                }
            }
        }
the cars do bounce off eachother but not every time. would anyone know why?

also my numCarsHit counter just keeps adding while i am colliding when it shouldnt be.

sorry i am still new to this physics engine but i still love it very much.

Thank you for the help.
Mar 1, 2013 at 2:48 PM
Just a quick idea I'll throw out there (you said you were new, so maybe you don't know about it), but have you played with the Restitution property of your bodies? The Restitution property controls how much bodies bounce when they collide. Not sure what kind of effect you're going for but you may be able to get what you want by just modifying the Restitution and skipping all the custom collision handling.

Cheers,
Mike.
Mar 1, 2013 at 7:43 PM
Thank you yes restitution helps with what i need.

i was trying to add a current impulse to the other car after collision to make the collision look like a car accident.

Thanks for the help