summaryrefslogtreecommitdiff
path: root/2d/softbody/softbody_1.html
diff options
context:
space:
mode:
Diffstat (limited to '2d/softbody/softbody_1.html')
-rw-r--r--2d/softbody/softbody_1.html114
1 files changed, 103 insertions, 11 deletions
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 @@
<section>
<p>
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.
</p>
</section>
<section>
- <h2></h2>
+ <h2>Undamped Spring Explanation</h2>
+ <p>
+ An <b>undamped spring</b> is one that never stops oscillating. Once an undamped spring starts moving back and forth,
+ it moves back and forth forever.
+ </p>
+ <p>
+ 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 <i>spring constant</i>.
+ 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 <i>equlibrium</i>, which means that it is displaced by
+ 0m. This should make sense intuitively.
+ </p>
+ <p>
+ One thing to note is that a damped spring has three cases:
+ <ul>
+ <li><b>Overdamped</b>: the spring will decompress immediately to the equlibrium position</li>
+ <li><b>Underdamped</b>: the spring will do one oscillation before returning to the equlibrium position</li>
+ <li><b>Critically damped: the spring will oscilate, but each oscillation will bring it closer and closer to the equlibrium position</b></li>
+ </ul>
+ 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 <i>c</i> and <i>k</i>, so choose wisely for your use case.
+ </p>
+ <p>
+ The equation is given as such:
+ <div class="formula">
+ <math class="formula">
+ ma + kx = 0
+ </math>
+ </div>
+ where <i>m</i> is the mass, <i>a</i> is acceleration, <i>k</i> is the spring constant, and <i>x</i> is the current displacement
+ from the rest position.
+ </p>
+ <p>
+ We can define an undamped 1D spring in code like so:
+ <pre><code><span class="code_keyword">struct</span> Spring {
+ <span class="code_keyword">float</span> mass = 1.f; <span class="code_comment">// Mass of the weight on the end of the spring</span>
+ <span class="code_keyword">float</span> k = 4; <span class="code_comment">// Spring Constant, in N / m</span>
+ <span class="code_keyword">float</span> force = 0.f;
+ <span class="code_keyword">float</span> velocity = 0.f;
+ <span class="code_keyword">float</span> position = 0.f;
+};
+
+<span class="code_keyword">void</span> updateSpring(Spring* spring, <span class="code_keyword">float</span> dtSeconds) {
+ spring->force = spring->k * spring->position;
+ <span class="code_keyword">float</span> acceleration = spring->force / spring->mass; <span class="code_comment">// F = ma</span>
+ spring->velocity = spring->velocity + acceleration * dtSeconds;
+ spring->position = spring->position + spring->velocity * dtSeconds;
+}
+</code></pre>
+ Note that we are just using Euler Integration here.
+ </p>
</section>
<section>
- <h2>
- Undamped Springs
- </h2>
+ <h2>
+ Undamped Springs Example
+ </h2>
<p>
<span class='widget_container'>
<label for='undamped_spring_length'>Spring Length (m)</label>
@@ -234,8 +284,53 @@
</section>
<section>
+ <h2>Damped Spring Explanation</h2>
+ <p>
+ 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 <b>viscous damping constant</b> (<i>c</i>) force
+ comes in. The damping force grows in proportion to the velocity. The equation is given as such:
+
+ <div class="formula">
+ <math class="formula">
+ ma + cv + kx = 0
+ </math>
+ </div>
+
+ where <i>m</i> is the mass, <i>a</i> is acceleration, <i>c</i> is the damping constant, <i>v</i> is the velocity
+ <i>k</i> is the spring constant, and <i>x</i> is the current displacement from the rest position.
+ </p>
+ <p>
+ 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 (<i>x</i>) will be negative and the velocity (<i>v</i>) 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!
+ </p>
+
+ <p>
+ We can define a damped 1D spring in code like so:
+ <pre><code><span class="code_keyword">struct</span> Spring {
+ <span class="code_keyword">float</span> mass = 1.f; <span class="code_comment">// Mass of the weight on the end of the spring</span>
+ <span class="code_keyword">float</span> k = 4; <span class="code_comment">// Spring Constant, in N / m</span>
+ <span class="code_keyword">float</span> c = 1; <span class="code_comment">// Viscous damping constant, in N / m/s</span>
+ <span class="code_keyword">float</span> force = 0.f;
+ <span class="code_keyword">float</span> velocity = 0.f;
+ <span class="code_keyword">float</span> position = 0.f;
+};
+
+<span class="code_keyword">void</span> updateSpring(Spring* spring, <span class="code_keyword">float</span> dtSeconds) {
+ spring->force = spring->c * spring->velocity + spring->k * spring->position;
+ <span class="code_keyword">float</span> acceleration = spring->force / spring->mass; <span class="code_comment">// F = ma</span>
+ spring->velocity = spring->velocity + acceleration * dtSeconds;
+ spring->position = spring->position + spring->velocity * dtSeconds;
+}
+</code></pre>
+ Note that we are just using Euler Integration here.
+ </p>
+ </section>
+
+ <section>
<h2>
- Damped Springs
+ Damped Springs Example
</h2>
<p>
<span class='widget_container'>
@@ -258,7 +353,7 @@
<span class='widget_container'>
<label for='damped_viscous_constant'>Viscous Damping Constant (N / m)</label>
- <input type='range' id='damped_viscous_constant' min='0' max='1000.0' value='1.0' step='0.1'/>
+ <input type='range' id='damped_viscous_constant' min='0' max='100.0' value='1.0' step='0.1'/>
<span></span>
</span>
@@ -292,9 +387,6 @@
<li>
<a href='http://ambrsoft.com/CalcPhysics/Spring/SpringData.htm'>List of Equations for Spring Motion</a>
</li>
- <li>
- <a href='https://www.ryanjuckett.com/damped-springs/'>Ryan Juckett's Explanation of Damped Springs</a>
- </li>
</ul>
</footer>
</article>