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.html | 114 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 103 insertions(+), 11 deletions(-) (limited to '2d/softbody/softbody_1.html') diff --git a/2d/softbody/softbody_1.html b/2d/softbody/softbody_1.html index 578024e..576b9c4 100644 --- a/2d/softbody/softbody_1.html +++ b/2d/softbody/softbody_1.html @@ -185,17 +185,67 @@

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. We will begin with what I believe - by investigating the most common of all deformations: a weight attached to a spring in two dimensions. + 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: +

+ 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 -

+

+ Undamped Springs Example +

@@ -233,9 +283,54 @@

+
+

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 + Damped Springs Example

@@ -258,7 +353,7 @@ - + @@ -292,9 +387,6 @@

  • List of Equations for Spring Motion
  • -
  • - Ryan Juckett's Explanation of Damped Springs -
  • -- cgit v1.2.1