# Perfect Tracking with Springs

### Created on Jan. 25, 2023, 3:44 p.m.

If you have some animation data with discontinuities you want to remove, you might have considered tracking that animation data with some kind of spring:

This does a good job at removing the discontinuities - but the spring needs to be very stiff - meaning the discontinuities get aggressively blended out - and even worse - it never quite reproduces exactly the input signal in places where nothing is wrong.

For this reason, I've always tried to avoid going down this route whenever I could, and to me, inertialization always seemed like the "correct" way of dealing with these kind of discontinuities if you knew where they occurred.

However, a few years ago, my old colleague Jack Potter showed me a kind of spring that can filter animation in exactly the way we want: it can remove discontinuities smoothly while still perfectly tracking the original signal when nothing is wrong.

The trick is to make a spring that tracks the acceleration, velocity, and position of the input animation in different proportions.

In code it looks something like this:

void tracking_spring_update(
float& x,
float& v,
float x_goal,
float v_goal,
float a_goal,
float x_gain,
float v_gain,
float a_gain,
float dt)
{
v = lerp(v, v + a_goal * dt, a_gain);
v = lerp(v, v_goal, v_gain);
v = lerp(v, (x_goal - x) / dt, x_gain);
x = x + dt * v;
}


First, we blend the current velocity with the current velocity plus the acceleration of the input animation multiplied by the dt. Then, we blend this with the velocity of the input animation itself. Finally, we blend this with a velocity that pulls us toward the position of the input animation.

For each of these blends we can use a gain to control the strength. For perfect tracking, we will want to set the acceleration gain to 1, the velocity gain to something like 0.2, and the position gain to something quite small such as 0.01 (when running at 60 frames per second).

This spring will need to be fed the acceleration, velocity, and position of the input signal. The easiest way to get these is by finite difference:

float tracking_target_acceleration(
float x_next,
float x_curr,
float x_prev,
float dt)
{
return (((x_next - x_curr) / dt) - ((x_curr - x_prev) / dt)) / dt;
}

float tracking_target_velocity(
float x_next,
float x_curr,
float dt)
{
return (x_next - x_curr) / dt;
}


If we know there is a discontinuity in this signal, we know the values computed by this finite difference will not be good (at least the acceleration and velocity targets wont be) - so we just ignore them for those time steps and only blend the velocity with values we know are good:

void tracking_spring_update_no_acceleration(
float& x,
float& v,
float x_goal,
float v_goal,
float x_gain,
float v_gain,
float dt)
{
v = lerp(v, v_goal, v_gain);
v = lerp(v, (x_goal - x) / dt, x_gain);
x = x + dt * v;
}

void tracking_spring_update_no_velocity_acceleration(
float& x,
float& v,
float x_goal,
float x_gain,
float dt)
{
v = lerp(v, (x_goal - x) / dt, x_gain);
x = x + dt * v;
}


This allows the spring to ignore the discontinuity and naturally converge back onto the input stream, tracking it perfectly:

If we don't know where the discontinuities are in the input signal we have two options - we can try to detect them and ignore them based on some kind of heuristic - or we can clip whatever we get to some maximum and minimum velocity and acceleration. Clipping can still sometimes give us a nasty jump when discontinuities occur but works okay given the imperfect situation we might be in:

This can be useful when our input signal may jump around in hard to specify ways or when it is coming from some black box we can't control.

One important thing to note here is that this code is not in any way robust to varying timesteps! The use of these gains will give us very different results if we tick at different rates:

We can improve things somewhat by switching lerp to damper_exact and using half-lives to control the blending (in this case I use 0.0 for the acceleration halflife, 0.05 for the velocity halflife and 1.0 for the position halflife).

void tracking_spring_update_improved(
float& x,
float& v,
float x_goal,
float v_goal,
float a_goal,
float x_halflife,
float v_halflife,
float a_halflife,
float dt)
{
v = damper_exact(v, v + a_goal * dt, a_halflife, dt);
v = damper_exact(v, v_goal, v_halflife, dt);
v = damper_exact(v, (x_goal - x) / dt, x_halflife, dt);
x = x + dt * v;
}


This version is certainly far from dt-invariant either, but feels a bit better than before to me.

Although basically stable, I would warn against ticking this spring at a variable timestep. Other than that I think it is pretty neat.

If anyone wants to go through the maths to try and derive an exact, dt-invariant version of this spring that would be awesome - or if anyone knows if this spring exists under a different name in engineering/control theory I would love to know!

That's all for now.