From f34116f1da8465851d684620b6b94e0a3f3c0fbc Mon Sep 17 00:00:00 2001
From: Matthew Kosarek
- 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 force applied to our rigidbody. We will use that force to get accleration, 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 mass for this object.
- Now, let's put that Rigidbody data structure to work! As I mentioned earlier, you can think of dynamics as the input 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 input 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:
+
The Kinematics Data Structure
The Dynamics Functions
+
+
+ Voila! We have everything we need to start applying forces. But wait! There is one more thing to consider...
+
+ Although it might be good enough for your use case, allow me to explain why the previous approach is neither realistic nor reliable:
+
+ 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 over time, or for a given duration of time. At the moment, our current implementation fails to account for this. forces are applied for a given frame, and then forgotten about.
+
+ Our current approach has another problem too: the applied force is not 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 deltaTimeSeconds would be much larger. This is generally something that you'd want to avoid in most applications.
+
+ One potential fix for this is to use impulses:
+
+ #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 over 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.
+
+ For anyone interested, the algorithm for using impulses is as follows:
+
- 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.