From 38d89c1182a61ca4e024e0834ae9187db76c67f8 Mon Sep 17 00:00:00 2001 From: mattkae Date: Sat, 26 Feb 2022 20:08:53 -0500 Subject: Fixing spring simulations and some write ups --- 2d/softbody/softbody_1/snippet1.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 2d/softbody/softbody_1/snippet1.cpp (limited to '2d/softbody/softbody_1/snippet1.cpp') diff --git a/2d/softbody/softbody_1/snippet1.cpp b/2d/softbody/softbody_1/snippet1.cpp new file mode 100644 index 0000000..19c2782 --- /dev/null +++ b/2d/softbody/softbody_1/snippet1.cpp @@ -0,0 +1,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; +} -- cgit v1.2.1