summaryrefslogtreecommitdiff
path: root/2d/softbody/softbody_1/damped.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/damped.cpp
parent8d4c5116719825dce6222c494cd384fe1df775de (diff)
Fixing spring simulations and some write ups
Diffstat (limited to '2d/softbody/softbody_1/damped.cpp')
-rw-r--r--2d/softbody/softbody_1/damped.cpp10
1 files changed, 4 insertions, 6 deletions
diff --git a/2d/softbody/softbody_1/damped.cpp b/2d/softbody/softbody_1/damped.cpp
index 0525e25..efba99a 100644
--- a/2d/softbody/softbody_1/damped.cpp
+++ b/2d/softbody/softbody_1/damped.cpp
@@ -40,10 +40,8 @@ namespace Damped {
// Update variables
float32 force = 0.f;
- float32 acceleration = 0.f;
float32 velocity = 0.f;
float32 position = 0.f;
-
void load(Renderer2d* renderer, DampedSpringWeight* inWieight, float32 length, float32 loopRadius, float32 initialDisplacement, float32 inK, float32 inC);
void update(float32 dtSeconds);
@@ -163,9 +161,9 @@ namespace Damped {
float32 C = k;
float32 discriminant = B * B - (4 * A * C);
- acceleration = 0;
velocity = 0;
- position = initialDisplacement;
+ position = -initialDisplacement;
+ force = -c * velocity - k * position;
if (discriminant < epsilon && discriminant > -epsilon) {
// Real repeated root (~ zero): Critically damped motion
@@ -221,8 +219,8 @@ namespace Damped {
}
void DampedSpring::update(float32 dtSeconds) {
- force = -weight->mass * acceleration - c * velocity - k * position;
- acceleration = (force * dtSeconds) / weight->mass;
+ force = -c * velocity - k * position;
+ float32 acceleration = force / weight->mass;
velocity = velocity + acceleration * dtSeconds;
float32 lastPosition = position;
position = position + velocity * dtSeconds;