summaryrefslogtreecommitdiff
path: root/2d/softbody/softbody_1/snippet2.cpp
diff options
context:
space:
mode:
authormattkae <mattkae@protonmail.com>2022-02-26 20:08:53 -0500
committermattkae <mattkae@protonmail.com>2022-02-26 20:08:53 -0500
commit38d89c1182a61ca4e024e0834ae9187db76c67f8 (patch)
treeeeb0cdabac28a0eed05d090c85028933a4645c7a /2d/softbody/softbody_1/snippet2.cpp
parent8d4c5116719825dce6222c494cd384fe1df775de (diff)
Fixing spring simulations and some write ups
Diffstat (limited to '2d/softbody/softbody_1/snippet2.cpp')
-rw-r--r--2d/softbody/softbody_1/snippet2.cpp16
1 files changed, 16 insertions, 0 deletions
diff --git a/2d/softbody/softbody_1/snippet2.cpp b/2d/softbody/softbody_1/snippet2.cpp
new file mode 100644
index 0000000..0e8523e
--- /dev/null
+++ b/2d/softbody/softbody_1/snippet2.cpp
@@ -0,0 +1,16 @@
+
+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;
+}