# How to calculate acceleration and deceleration

I have a car with speed x, and can accelerate at a rate of 0.5 m/s^2, and can decelerate at a rate of -0.1 m/s^2. Maximum speed is 20 m/s.

To accelerate the car speed, at each frame I need to do:

car.CurrentSpeed = car.CurrentSpeed + 0.5

To decelerate the car speed, at each frame I need to do:

car.CurrentSpeed = car.CurrentSpeed - 0.1

Is this correct?

I have found this which suggest the following speed formula:

Speed += ((MoveDirection * MaximumSpeed) - Speed) * AccelerationFactor

Where:

• Speed is the current speed the entity is travelling at on the current axis.
• MoveDirection is the direction the entity is trying to travel in on the current axis, 1 is forward, 0 is still and -1 is backwards. All values in between are allowed.
• MaximumSpeed is a constant determining the fastest that the entity can travel on the current axis.
• AccelerationFactor is a constant between 0 and 1 that represents the rate of acceleration and deceleration. 1 is instant, and 0 will never move.

Using the same formula then:

To accelerate:

car.CurrentSpeed += ((MoveDirection * 20) - car.CurrentSpeed) * 0.5

What would be the move direction? Can it be the heading?

move direction is the heading. You have to distinguish speed from velocity; when you multiply speed (a scalar) with the move direction (a unit vector), you get the velocity, which is a vector.

You don't take the time into consideration.

If your speed is based on m/s and your acceleration is in m/s^2, at each frame you should do something like:

car.CurrentSpeed = car.CurrentSpeed + (0.5 * delta_time)


and to decelerate, you need to reduce the speed:

car.CurrentSpeed = car.CurrentSpeed - (0.1 * delta_time)


where delta_time is the time for each frame.

This will take the frame-rate into consideration.

To cap the speed at it's maximum:

car.CurrentSpeed = min( maximum_speed, car.CurrentSpeed + (0.5 * delta_time) )


You can then apply this speed to the heading direction, you'll get the velocity vector (if you need it).

Now be aware that if you brake your speed will need to be capped to zero:

car.CurrentSpeed = max ( zero, car.CurrentSpeed - (0.1 * delta_time) )


...as opposed to if you reverse, where your reverse speed will need to defined differently. Now it's either going to be a positive speed with a flag that says you're in reverse, or a negative speed; this will depend on the rest of your architecture.

• Okay, and to decelerate I need to multiply by 0.1, like so: car.CurrentSpeed = car.CurrentSpeed + (0.1 * delta_time) – lucidgold Feb 26 '16 at 16:47
• @lucidgold I edited my answer. – Vaillancourt Feb 26 '16 at 16:53
• Excellent, I think this is making a lot of sense to me. The only issue now is to figure out how to calculate delta_time. Okay, so I am communicating with a server which I can provide a new speed and new heading for the car to move, the server then responds with GPS coordinates where the car is now located and its current speed and heading. It also provides start_time and end_time. So, can delta_time = end_time - start_time? – lucidgold Feb 26 '16 at 16:58
• "It also provides start_time and end_time. So, can delta_time = end_time - start_time?" This is getting a bit out of the scope of the the question; however, it depends; what are they? The acceleration is based on time. What are start_time and end_time? If they represent the time between the start of the and the end of the acceleration, yes, you have your delta time. – Vaillancourt Feb 26 '16 at 17:04
• One note, your way of calling the speed will hard cap it at maximum_speed whereas the equation quoted in the question will mean it will smoothly approach the maximum. When I get a computer I can make some plots displaying this. – Malrig Feb 27 '16 at 19:29