# How do I manipulate a trio of equations for Acceleration / Velocity / Position such that if I change the number of iterations, the result is the same?

This is for a thought experiment I'm programming.

Let's say I have 2 cars; 1 in front, 1 in back. I'm adjusting the rear car's acceleration so that it will never be closer than 1 second's worth of distance from a half car's length behind the front car.

I have created the below formulae to calculate the acceleration, velocity, and position of the rear vehicle, as a result. Since this for a programmatic animation, these formulae are calculated for every frame of animation.

$$A_r = \frac{V_f + \frac{1}{2}A_f+S_f-2V_r-S_r}{2}$$ $$V_r = V_r + A_r$$ $$S_r = S_r + V_r$$

$A_r$ = Rear Car's Acceleration
$V_r$ = Rear Car's Velocity
$S_r$ = Rear Car's Position of Front Bumper
$A_f$ = Front Car's Acceleration
$V_f$ = Front Car's Velocity
$S_f$ = Front Car's Position of Rear Bumper minus a half-car length

For your understanding, here's how I generated the acceleration formula. I'm just combining two displacement formulae, and solving for the rear car's acceleration.

Word Explanation: The position of the rear car after 2 seconds should be equal to the position of the front car's position after 1 second. The displacement equation I'm using is:

$$S_t = V_x t + \frac{1}{2} A_x t^2 + S_x$$

Where $S_t$ = Target Position

Therefore: The position of the front car after 1 second:

$$S_t = V_f + \frac{1}{2} A_f + S_f$$

And, the position of the second car after 2 seconds:

$$S_t = 2V_r + 2A_r + S_r$$

And now, to solve for $A_r$:

$$2A_r = S_t - 2V_r - S_r$$

$$A_r = \frac{S_t - 2V_r - S_r}{2}$$

And lastly, to plug in $S_t$ from the front car:

$$A_r = \frac{V_f + \frac{1}{2}A_f+S_f-2V_r-S_r}{2}$$

Now, this works perfectly; after every iteration, the car will accelerate up to a safe speed, and slow down so that it can stop by the time $S_r = S_f$.

Since I'm simulating this in a computer application, the issue I'm having is that when I simulate it, it take very few iterations to make $S_r \approx S_f$. I'd like to increase the amount of iterations required to achieve the same result. In other words, my simulation currently takes $\approx$10 frames to complete this simulation, but I'd like it to take at least 200 frames (so, let's say I need 20× more iterations).

However, if I make the seemingly obvious change (with the below equation), the car will "drift" past the safe point and "crash" into the vehicle, driving past it entirely, before its velocity reverses to correct itself.

$$V_r = V_r + \frac{A_r}{20}$$

It should also be noted that if I keep the time variables in my displacement formulae, and use that as a value (for example, 2 instead of 1), a similar result occurs (although less pronounced):

$$A_r = \frac{2(V_f \cdot t + \frac{1}{2}A_f \cdot t^2 + S_f-2V_r-S_r}{(2t)^2}$$

How exactly would I go about incorporating some sort of "time step" such that it would increase the number of iterations by a factor of $x$, and would achieve the same result at the end of the iterations?

When you update $V_r$ you are computing the new velocity at the end of the time step. You are then using that velocity for the whole time step when you update $S_r$. If you assume the acceleration is constant for the whole time step the update of $V_r$ is fine, but the update of $S_r$ should use the average of the starting velocity and the ending velocity. I am a little surprised that doesn't get damped out the next time step but during acceleration $S_r$ will be too great.
• Hmm, so I changed it so I calculate $S_r = S_r + (2V_r + A_r)/2$ first, and update my velocity after calculating my position; $V_r = V_r + A_r$. That does not seem to make a noticeable difference. – MrMusAddict Jan 8 '18 at 19:35
• Looking again, your $S_r$ update assumes a $1$ second time step. It should be $S_r=S_r+(2V_r+A_r)\Delta t/2$ where $\Delta t$ is the time step. You should go through looking for other cases as well where you need $\Delta t$ factors. – Ross Millikan Jan 8 '18 at 20:05
• I knew it was something simple! I kept my original Velocity function: $V_R = V_R + A_R$, but simply changed my position formula to be $S_R = S_R + \frac{V_R}{\Delta t}$ Solved! – MrMusAddict Jan 8 '18 at 20:42
• It should be $V_R \Delta t$ to make the units right. Maybe you are using $\Delta t$ as the frame rate, not as the time for a frame. – Ross Millikan Jan 8 '18 at 21:50