# How to simulate acceleration and then deceleration?

Currently I am only using deceleration.

Here is how I do it

I need to cover Vector(x,y) unit distance to reach B from A.

I simply do -

rendering loop->

1. position = A + Vector(x,y).scale(factor);
2. Vector(x,y) = Vector(x,y) - Vector(x,y).scale(factor);

...factor = 10 for example So if I have to cover 100 units, I cover 10 then 9 then 8.1 and so on in every frame.

How do I simulate acceleration then deceleration here?

-
Acceleration changes velocity in the same way velocity changes position. So, change your velocity over time to simulate acceleration. (BTW, "deceleration" isn't really a separate thing, it's just a short way of saying "acceleration in the direction opposite of travel") – Byte56 Jul 31 '13 at 16:39
Your have basically simulated drag coefficients, although you'd genereLly have a different "factor" for drag vs whatever your doing with position there, which some people struggle quite a bit with, so good job there. – Sean Middleditch Jul 31 '13 at 22:18

A simple way is to animate the object using a smoothstep curve:

``````lerpFactor = (curTime - startTime) / timeToReachTarget;
smoothstepFactor = lerpFactor * lerpFactor * (3.0 - 2.0 * lerpFactor);
position = lerp(startPos, targetPos, smoothstepFactor);
``````

Here, I'm assuming a specified amount of time to reach the target (`timeToReachTarget`) and calculating how far the object should have moved by the current time (`lerpFactor`), then passing it through the smoothstep curve to make it accelerate and decelerate smoothly.

This isn't an especially physics-based approach, nor does it work well if the target point is moving, but it does create a smooth-looking motion.

-
`lerpFactor * lerpFactor * (3.0 - 2.0 * lerpFactor)` is not the same that `3x^2 -2x^3`, is there an error ? – tbruyelle Mar 24 '14 at 22:52
@kosokund They're the same; I just wrote it in a factored form that avoids redundant calculations. It's a common trick for polynomials. – Nathan Reed Mar 25 '14 at 1:20
ok thanks, I need to re-learn maths... – tbruyelle Mar 25 '14 at 12:27