diff options
author | Matthew Kosarek <mattkae@protonmail.com> | 2021-06-22 20:23:33 -0400 |
---|---|---|
committer | Matthew Kosarek <mattkae@protonmail.com> | 2021-06-22 20:23:33 -0400 |
commit | f34116f1da8465851d684620b6b94e0a3f3c0fbc (patch) | |
tree | d17c3531f017caa3c60bc66a96d804d03fa60b21 /2d/rigidbody/rigidbody_1.html.content | |
parent | a36f425491aaf019243a31179e80cb10ea62db59 (diff) |
Impulses, and entirely finished with rigid body demo #1
Diffstat (limited to '2d/rigidbody/rigidbody_1.html.content')
-rw-r--r-- | 2d/rigidbody/rigidbody_1.html.content | 57 |
1 files changed, 46 insertions, 11 deletions
diff --git a/2d/rigidbody/rigidbody_1.html.content b/2d/rigidbody/rigidbody_1.html.content index de3898a..d61acaf 100644 --- a/2d/rigidbody/rigidbody_1.html.content +++ b/2d/rigidbody/rigidbody_1.html.content @@ -28,7 +28,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. @@ -41,19 +41,54 @@ <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. #SNIPPET rigidbody_1/snippet1.cpp - 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. #SNIPPET rigidbody_1/snippet2.cpp + + 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>: + + #SNIPPET rigidbody_1/snippet3.cpp + + 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> @@ -61,7 +96,7 @@ 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> @@ -72,11 +107,11 @@ 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> |