summaryrefslogtreecommitdiff
path: root/2d/softbody/softbody_1/snippet1.cpp
blob: 19c27827e7600ae6ba36982dc574cdfb8f59f5fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

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;
}