summaryrefslogtreecommitdiff
path: root/2d/rigidbody/rigidbody_1.html
diff options
context:
space:
mode:
Diffstat (limited to '2d/rigidbody/rigidbody_1.html')
-rw-r--r--2d/rigidbody/rigidbody_1.html137
1 files changed, 118 insertions, 19 deletions
diff --git a/2d/rigidbody/rigidbody_1.html b/2d/rigidbody/rigidbody_1.html
index 0751499..bf703d2 100644
--- a/2d/rigidbody/rigidbody_1.html
+++ b/2d/rigidbody/rigidbody_1.html
@@ -78,7 +78,7 @@
<ul>
<li>
- <b>Kinematics</b> is the study of how an object's movement changes over time. These are the classic position, velocity, and acceleration equations that you're most likely familiar with from high school or college physics. Kinematics doesn't care about <i>how</i> a system entered into the state that it is out, but rather that the system <i>is</i> in that state.
+ <b>Kinematics</b> is the study of how an object's movement changes over time. These are the classic position, velocity, and acceleration equations that you're most likely familiar with from high school or college physics. Kinematics doesn't care about <i>how</i> a system entered into the state that it is in, but rather that the system <i>is</i> in that state.
</li>
<li>
<b>Dynamics</b> is the study of whats <i>causes</i> kinematic movement. These are the classic force and momentum equations that you may already be familiar with as well. Whereas kinematics only worries itself with the current state of the system, dynamics wants to know <i>how</i> the system entered the state that it is currently in.
@@ -91,23 +91,22 @@
<section>
<h2>The Kinematics Data Structure</h2>
<p>
- Now that we have that understanding, we can begin setting up our rigidbody data structure.
+ Now that we have an understanding of these two fundamental fields of physics, we can begin setting up our rigidbody data structure.
<pre><code><span class="code_keyword">struct</span> Rigidbody {
<span class="code_keyword">Vector2</span> force = { 0, 0 };
- <span class="code_keyword">Vector2</span> acceleration = { 0, 0 };
<span class="code_keyword">Vector2</span> velocity = { 0, 0 };
<span class="code_keyword">Vector2</span> position = { 0, 0 };
<span class="code_keyword">float32</span> mass = 1.f;
};
</code></pre>
- As you can see, the base data structure exactly mirrors what we already know from 2D newtonian physics.
+ As you can see, the base data structure exactly mirrors what we already know from 2D newtonian physics. Every frame, we will have some <i>force</i> applied to our rigidbody. We will use that <i>force</i> to get <i>accleration</i>, which, when differentied with respect to time, yields velocity and, ultimately, the new position of our rigidbody. For all of this to work, of course, we need a constant <i>mass</i> for this object.
</p>
</section>
<section>
<h2>The Dynamics Functions</h2>
<p>
- Now, let's put that Rigidbody data structure to work! As I mentioned earlier, you can think of dynamics as the <i>input</i> to the system. What we're going to do now is add a way to
+ Now, let's put that Rigidbody data structure to work! As I mentioned earlier, you can think of dynamics as the <i>input</i> to the system. What we're going to do now is add a way apply some sort of force to our rigidbody instantaneously.
<pre><code><span class="code_keyword">struct</span> Rigidbody {
<span class="code_keyword">Vector2</span> force = { 0, 0 };
@@ -115,31 +114,131 @@
<span class="code_keyword">Vector2</span> position = { 0, 0 };
<span class="code_keyword">float32</span> mass = 1.f;
- <span class="code_keyword">void</span> applyForce(Vector2 f) {
- force += f;
+ <span class="code_keyword">void</span> update(float32 deltaTimeSeconds) {
+ applyGravity(deltaTimeSeconds);
+
+ <span class="code_keyword">Vector2</span> acceleration = force / mass;
+ velocity += (acceleration * deltaTimeSeconds);
+ position += (velocity * deltaTimeSeconds);
+ force = <span class="code_keyword">Vector2</span> { 0.f, 0.f };
}
<span class="code_keyword">void</span> applyGravity(float32 deltaTimeSeconds) {
- velocity += (Vector2 { 0.f, -50.f } * deltaTimeSeconds);
+ velocity += (Vector2 { 0.f, -9.8.f } * deltaTimeSeconds);
}
+ <span class="code_keyword">void</span> applyForce(Vector2 f) {
+ force += f;
+ }
+};
+</code></pre>
+ We have three new functions here:
+ <ul>
+ <li><b>update</b>: This function will run every single frame during our simulation. In it, we use the total force being applied on the object at this time (found by reordering the classic equation <i>F = ma</i>). From there, we differentiate once with respect to time to get the velocity, and again to get the position. Finally, we reset the total force to zero so that we can start fresh on the next frame.</li>
+ <li><b>applyGravity</b>: We all know that gravity is a constant 9.8 m/s<sup>2</sup>, so we can simply add that to the velocity every single frame. (Note that the gravity in your game may not 9.8; don't be afraid to mess around with this number).</li>
+ <li><b>applyForce</b>: This function provies a way for external forces to affect our rigidbody directly.</li>
+ </ul>
+
+ Voila! We have everything we need to start applying forces. But wait! There is one more thing to consider...
+ </p>
+ </section>
+ <section>
+ <h2>Impulses & Frame-Rate Independence</h2>
+ <p>
+ Although it might be good enough for your use case, allow me to explain why the previous approach is neither realistic nor reliable:<br/><br/>
+
+ When a force is applied in the real world, it doesn't just get applied for a single moment (i.e. frame) in time: it gets applied <i>over</i> time, or for a given <i>duration</i> of time. At the moment, our current implementation fails to account for this. forces are applied for a given frame, and then forgotten about.<br/><br/>
+
+ Our current approach has another problem too: the applied force is <b>not</b> frame-rate independent. If you were to apply a force of 50N in the Y direction right now, slower computers would experience larger resultant velocities because their <i>deltaTimeSeconds</i> would be much larger. This is generally something that you'd want to avoid in most applications.<br/><br/>
+
+ One potential fix for this is to use <b>impulses</b>:
+
+ <pre><code><span class="code_keyword">struct</span> Impulse {
+ <span class="code_keyword">Vector2</span> force = { 0, 0 };
+ <span class="code_keyword">float32</span> timeOfApplicationSeconds = 0.25f;
+ <span class="code_keyword">float32</span> timeAppliedSeconds = 0.f;
+ <span class="code_keyword">bool</span> isDead = false;
+};
+
+const <span class="code_keyword">int32</span> NUM_IMPULSES = 4;
+
+<span class="code_keyword">struct</span> Rigidbody {
+ <span class="code_keyword">int32</span> numImpulses = 0;
+ Impulse activeImpulses[NUM_IMPULSES];
+ <span class="code_keyword">Vector2</span> velocity = { 0, 0 };
+ <span class="code_keyword">Vector2</span> position = { 0, 0 };
+ <span class="code_keyword">float32</span> mass = 1.f;
+
<span class="code_keyword">void</span> update(float32 deltaTimeSeconds) {
applyGravity(deltaTimeSeconds);
-
+
+ // Add up all of the forces acting at this moment
+ <span class="code_keyword">Vector2</span> force;
+ for (int32 idx = 0; idx < numImpulses; idx++) {
+ Impulse& i = activeImpulses[idx];
+
+ <span class="code_keyword">float32</span> nextTimeAppliedSeconds = i.timeAppliedSeconds + deltaTimeSeconds;
+ if (nextTimeAppliedSeconds >= i.timeOfApplicationSeconds) {
+ nextTimeAppliedSeconds = i.timeOfApplicationSeconds;
+ i.isDead = true;
+ }
+
+ <span class="code_keyword">float32</span> impulseDtSeconds = nextTimeAppliedSeconds - i.timeAppliedSeconds;
+ force += i.force * impulseDtSeconds;
+ i.timeAppliedSeconds = nextTimeAppliedSeconds;
+ }
+
<span class="code_keyword">Vector2</span> acceleration = force / mass;
- velocity += (acceleration * deltaTimeSeconds);
+ velocity += (acceleration * impulseDtSeconds);
position += (velocity * deltaTimeSeconds);
- force = <span class="code_keyword">Vector2</span> { 0.f, 0.f };
+
+ // Cleanup any impulses that have expired in the mean time
+ for (int32 idx = 0; idx < numImpulses; idx++) {
+ if (activeImpulses[idx].isDead) {
+ for (int j = idx + 1; j < numImpulses; j++) {
+ activeImpulses[j - 1] = activeImpulses[j];
+ }
+
+ idx = idx - 1;
+ numImpulses--;
+ }
+ }
+ }
+
+ <span class="code_keyword">void</span> applyGravity(float32 deltaTimeSeconds) {
+ velocity += (Vector2 { 0.f, -9.8f } * deltaTimeSeconds);
+ }
+
+ <span class="code_keyword">void</span> applyImpulse(Impulse i) {
+ if (numImpulses > NUM_IMPULSES) {
+ printf("Unable to apply impulse. Buffer full.\n");
+ return;
+ }
+
+ activeImpulses[numImpulses] = i;
+ numImpulses++;
}
};
-</code></pre> </p>
+</code></pre>
+ While a bit more verbose than our previous example, this approach has more reliable behavior. Forces are no longer treated as single moments in time, but rather "forces applied <i>over</i> time". Because we ensure that the force is applied over time, we guarantee that all users see the same amount of force applied, regardless of frame-rate.<br/><br/>
+
+ For anyone interested, the algorithm for using impulses is as follows:
+ <ul>
+ <li>Get all impulses acting on the rigidbody at this moment</li>
+ <li>For each impulse, find out how much time is remaining. Apply the remaining force, and mark those that have expired as dead</li>
+ <li>Calculate the acceleration, velocity, and position as before</li>
+ <li>Remove any dead impulses from the list</li>
+ </ul>
+
+ Feel free to look at the example program below (and browse/download the code) if you want to see it in action.
+ </p>
</section>
<section>
<h2>
Live Example
</h2>
<p>
- That's all there is to a rigidbody system with 2D linear forces. Now let's see it in action. Click 'Play' on the WebAssembly demo below to see a square bouncing around the screen. When you drag the pointer through the square, we will apply a force equivalent to how fast you were moving your mouse in the direction that you were moving it. (The speed is capped in the demo, or else things get a little out of hand.)
+ Click 'Play' on the WebAssembly demo below to see a square bouncing around the screen. When you drag the pointer through the square, we will apply an impulse equivalent to how fast you were moving your mouse in the direction that you were moving it.
</p>
<div class="opengl_canvas_container">
<canvas id="gl_canvas" width="800" height="600"></canvas>
@@ -150,13 +249,13 @@
Stop
</button>
</div>
-
- <footer id="references">
- <h2>References</h2>
- <ul>
- </ul>
- </footer>
</section>
+ <footer id="references">
+ <h2>References</h2>
+ <ul>
+ <li><a href='https://www.thoughtco.com/impulse-2698956'>Impulse Information</a></li>
+ </ul>
+ </footer>
</article>
</main>
</body>