Physics for Games

Springs

It is time to investigate what it means to have a deformation in our physics system. By deformation, I mean that the vertices of our shapes are no longer fixed to one point within the model. Towards this end, we will begin by investigating springs.

Undamped Spring Explanation

An undamped spring is one that never stops oscillating. Once an undamped spring starts moving back and forth, it moves back and forth forever.

From Newton's second law, we know that the sum of all the forces acting on an object will equal zero. We know that one of these forces is mass times acceleration, but for a spring we will add another force which uses the spring constant. This force grows in proportion to the displacment that spring is currently set at. So, if the spring is currently displaced 10m, the effect of this force will be greater than if the spring was at equlibrium, which means that it is displaced by 0m. This should make sense intuitively.

One thing to note is that a damped spring has three cases:

  • Overdamped: the spring will decompress immediately to the equlibrium position
  • Underdamped: the spring will do one oscillation before returning to the equlibrium position
  • Critically damped: the spring will oscilate, but each oscillation will bring it closer and closer to the equlibrium position
I will not go into the mathematical details as to why this happens, as the links at the end of this video can very easily tell you. Just know that they are dependent on your values of c and k, so choose wisely for your use case.

The equation is given as such:

ma + kx = 0
where m is the mass, a is acceleration, k is the spring constant, and x is the current displacement from the rest position.

We can define an undamped 1D spring in code like so:

struct Spring {
    float mass = 1.f;       // Mass of the weight on the end of the spring
    float k = 4;            // Spring Constant, in N / m
    float force = 0.f;
    float velocity = 0.f;
    float position = 0.f;
};

void updateSpring(Spring* spring, float dtSeconds) {
    spring->force = spring->k * spring->position;
    float acceleration = spring->force / spring->mass;  // F = ma
    spring->velocity = spring->velocity + acceleration * dtSeconds;
    spring->position = spring->position + spring->velocity * dtSeconds;
}
Note that we are just using Euler Integration here.

Undamped Springs Example

Damped Spring Explanation

Undamped springs are loads of fun, but if we want to make a softbody physics system, we're going to want our simulation to stop oscillating at some point. That is where the viscous damping constant (c) force comes in. The damping force grows in proportion to the velocity. The equation is given as such:

ma + cv + kx = 0
where m is the mass, a is acceleration, c is the damping constant, v is the velocity k is the spring constant, and x is the current displacement from the rest position.

This force produces the most effect when the velocity is largest. Let's try to intuit exactly what this means. When our spring is extended downward, it will want to move upward to return to its equlibrium position. At this point, the displacement (x) will be negative and the velocity (v) will be positive. Since the signs are different, we can see that our spring force will be mitigated by the damping force. Hence, we damp!

We can define a damped 1D spring in code like so:

struct Spring {
    float mass = 1.f;       // Mass of the weight on the end of the spring
    float k = 4;            // Spring Constant, in N / m
    float c = 1;            // Viscous damping constant, in N / m/s
    float force = 0.f;
    float velocity = 0.f;
    float position = 0.f;
};

void updateSpring(Spring* spring, float dtSeconds) {
    spring->force = spring->c * spring->velocity + spring->k * spring->position;
    float acceleration = spring->force / spring->mass;  // F = ma
    spring->velocity = spring->velocity + acceleration * dtSeconds;
    spring->position = spring->position + spring->velocity * dtSeconds;
}
Note that we are just using Euler Integration here.

Damped Springs Example