Help understanding UI Spring Animation

Hi! I am working on making a Spring-based animation module for UI elements.

The part I am struggling to understand is how to apply the physics. I have very limited knowledge of calculus (something I would like to learn more of) and would love a basic understanding of how a system like this could work.

If you have any questions or are struggling to understand something, just let me know and I’ll try to clarify.

Thank you for your help!

From the spring-based modules I’ve seen, they typically implement and solve a 2nd order DFE based on initial parameters to get a position function of time. From there, they use RunService.Heartbeat or some other tick-based method to get delta time and use that to update the position of the spring.

If you’re not familiar with 2nd order DFEs especially in the context of a spring-mass context, I’d say start there. It’s some heavy math but once you understand that, implementing it shouldn’t be too difficult.

Here’s some resources regarding 2nd order DFEs. The first deals with applying them to spring-mass systems while the first goes into more detail about how to actually solve them.

If you’re not strong with calculus then maybe start with learning how to derive and integrate functions first. Below is a good resource (imo) for general calculus. I’d say focus mostly on derivatives and integrals.

The numerical way to solve a spring is to calculate the forces of the spring every frame, and translate that into a position

The force is F = -kx -bv, where F = ma (k being the return constant of your spring, how stiff it is kinda, and b is the drag coefficient, or how fast it slows down. You can also write it as -bv^2)

So, a = -(k/m) * x -(b/m) * v
We can calculate the acceleration numerically using x and v from the last frame

Velocity is (if you approximate) acceleration * the time, so you can write

v = a * dt + v (we have to add to the previous velocity)

And same for the position

x = v * dt + x

dt is the amount of time between frames, a smaller dt gives a more accurate simulation

These are annoying though, and springs can be simulated analytically pretty well

The analytical formula for a spring with no drag force is x(t) = Acos(wt - C), where w is sqrt(k/m), but at this point you can just hardcode w. It’s the angular frequency. C is an offset that will depend on initial position, at t = 0. A is the amplitude

Now, if there is drag
Analytically, solving for drag forces with velocity squared is very hard (don’t even know if there is a formula for it), so the drag force will be -bv

There are 3 different solutions for this one, depending on the value of b, k, and m. But you can just chose whichever solution you want

First solution is x(t) = e^(-Gt) * Acos(wt + C)
It is very similar to the one without drag, but with an added term to make the oscillations die down over time. G determines how fast it decreases

Second solution is x(t) = Ae^(-Gt) * (C1 + C2t)
This solutions is a critically dampened system, the x(t) basically just smoothly goes to 0, has no oscillations

Third solution is x(t) = A1e^(-G1t) + A2e^e(-G2t)
This one is similar to the second one but it overshoots x = 0, and goes in the negative before going to 0

One thing to note is that we don’t have to follow these formulas. We can use modified versions that fit our needs, for example e^(-Gt) is a bit annoying because it never truly reaches 0, you basically have to stop it when it is basically 0. It could be replaced with a quadratic equation, in the form of (x - 1)^2, at t = 0, it is 1, and at t = 1, it is 0, so we know when it reaches 0. You can also adjust the exponent to adjust its slope

Hope this helps!