forward motion car physics - gradual slow

Posted by spartan2417 on Game Development See other posts from Game Development or by spartan2417
Published on 2012-12-12T14:36:33Z Indexed on 2012/12/12 17:20 UTC
Read the original article Hit count: 312

Filed under:
|

Im having trouble creating realistic car movements in xna 4. Right now i have a car going forward and hitting a terminal velocity which is fine but when i release the up key i need to the car to slow down gradually and then come to a stop. Im pretty sure this is easy code but i cant seem to get it to work

the code - update

if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                double elapsedTime = gameTime.ElapsedGameTime.Milliseconds;
                CalcTotalForce();
                Acceleration = Vector2.Divide(CalcTotalForce(), MASS);
                Velocity = Vector2.Add(Velocity, Vector2.Multiply(Acceleration, (float)(elapsedTime)));
                Position = Vector2.Add(Position, Vector2.Multiply(Velocity, (float)(elapsedTime)));
            }

added functions

public Vector2 CalcTraction()
    {
        //Traction force = vector direction * engine force
        return Vector2.Multiply(forwardDirection, ENGINE_FORCE);
    }
        public Vector2 CalcDrag()
        {
            //Drag force = constdrag * velocity * speed
            return Vector2.Multiply(Vector2.Multiply(Velocity, DRAG_CONST), Velocity.Y);
        }

        public Vector2 CalcRoll()
        {
            //roll force = const roll * velocity
            return Vector2.Multiply(Velocity, ROLL_CONST);
        }

        public Vector2 CalcTotalForce()
        {
            //total force = traction + (-drag) + (-rolling)
            return Vector2.Add(CalcTraction(), 
                Vector2.Add(-CalcDrag(), -CalcRoll()));
        }

anyone have any ideas?

© Game Development or respective owner

Related posts about physics

Related posts about xna-4.0