Chapter 12: Mechanics 2: Linear & Rotational Dynamics Fletcher Dunn Ian Parberry Valve Software University of North Texas 3D Math Primer for Graphics & Game Development What You’ll See in This Chapter This chapter considers the cause of motion, its orientation, and how we might go about simulating it on a computer. It is divided into six sections. • Section 12.1 gives an overview of Newton’s 3 laws. • Section 12.2 talks about the cause of motion: the force. • Section 12.3 introduces momentum. • Section 12.4 looks at collisions and impulse. • Section 12.5 is about rotational dynamics. • Section 12.6 discusses digital simulation of mechanics. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 2 Word Cloud Chapter 11 Notes 3D Math Primer for Graphics & Game Dev 3 Section 12.1: Newton’s 3 Laws Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 4 Sir Isaac Newton • Sir Isaac Newton established three simple laws that provide a framework, which we call Newtonian or classical mechanics. • It doesn’t hold at high speeds or small distances, but it’s good enough for everyday life, and video games. • (Image from Wikimedia Commons.) Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 5 Newton’s First Law Every body persists in its state of being at rest or of moving uniformly straight forward, except insofar as it is compelled to change its state by force impressed. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 6 Newton’s Second Law The acceleration π of a body is proportional to (and in the same direction as) the net external force π acting on the body, and inversely proportional to the mass π of the body: π = ππ. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 7 The Force • Force is a vector. It has units like kg.m/sec2, also called a Newton. m m π = π kg . π = ππ kg 2 sec sec 2 • “Duct tape is like the force. It has a light side, a dark side, and it holds the universe together.” (Carl Zwanzig) Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 8 Free Body Diagram Starting with a representation of the object. 1. Draw and label all the forces acting on it. 2. Sum those forces (using vector addition) to compute the net force. 3. Use Newton's 2nd law to compute the acceleration of the object. 4. Integrate the acceleration to determine the motion of the object. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 9 Differential Equations When solving problems analytically, this means solving differential equations. We don't use any differential equations in this book because there are only a few simple cases that we will look at analytically. Numerical methods of integration must be used. Later, we examine Euler integration, which is the most simple method imaginable, but also the one used by most realtime rigid body simulators. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 10 Inertial Reference Frames • This only works in a reference frame that is not accelerating. • You have to invent fictional forces to explain why objects are not accelerating according to Newton’s 1st and 2nd laws. • A robot in a falling elevator is in a noninertial frame. He must invent a fictitious upward force to counteract gravity to explain why his herring sandwich doesn’t fall. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 11 To a passing alien who is not accelerating, Newton’s laws work just fine, and there is no need to invent a fictional force. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 12 Newton’s Third Law To every action there is always an equal and opposite reaction. Or, the forces of two bodies on each other are always equal and are directed in opposite directions. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 13 Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 14 Example There are four forces here. 1. Moe pushing the box. 2. The box pushing Moe. 3. Moe pushing the Earth. 4. The Earth pushing Moe. Note that ππΈπππ‘β and ππ΅ππ₯ cancel out. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 15 Consequence of Newton’s 3rd Law • As long as the internal forces cancel out, we are justified in representing a complex body by a single point or particle. • This is called rigid body dynamics. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 16 Section 12.2: Some Simple Force Laws Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 17 Gravity in the Real World Newton’s Law of Universal Gravitation: π1 π2 π=πΊ π2 where π is the magnitude of the force, π1 and π2 are the masses of the two objects, and π is the distance between their centers of mass, and πΊ = 6.637 × 10−11 Nm2 kg −2 . Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 18 Video Game Gravity π = ππ , where π = 9.8 m/sec 2 . This is not physically accurate, but then again, neither is being able to jump two or three times your own height, steer in midair, or double jump. When it comes to jumping in video games, reality is not just overrated, it's completely ignored. It just doesn't feel right. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 19 Video Game Gravity • In most first-person shooters, when you jump, you are given an initial burst of upward velocity, and then your position is simulated just like every other airborne object in the world. • In most third-person games your character will spring up almost instantaneously and reach a maximum height very quickly. In many games the character will hover there, then slam back down on the ground as quickly as it rose up, perhaps leaving a crater behind. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 20 Video Game Gravity • Simulating a jump mechanic using a value of 9.8 m/sec 2 may be even worse, because most players expect a jump to take a certain amount of time but also expect to be capable of jumping to unrealistic heights. • When real-world gravity is used to attain these heights, the player is in the air too long, and it feels “floaty”. • Many arcade racing games increase gravity to get the car back on the ground quickly. The player wants to be in full control again as quickly as possible, and waiting for realworld gravity to get them back down usually takes too long. • There are other racing games that use a value of gravity that is less than the real world value, to facilitate unrealistic jumps at realistic vehicle speeds. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 21 Video Game Gravity • There are also reasons to fiddle with gravity for nonplayer-character objects as well. Sometimes real-world gravity can create an “objects made of styrofoam” feeling, so gravity is increased to get an object to tip over and come to rest more quickly. • In other situations, an artificially low value of gravity can make a large object seem even more massive (especially when accompanied by the right sound effects), because acceleration on Earth is constant and is one of a few cues humans instinctively use to establish an absolute scale for objects in the distance. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 22 Realism versus Entertainment • What “feels right" is a subjective matter. It is based more on player expectation than physical reality. • In the end, what matters most in a video game is not what's going on in the CPU or even on the screen, but what is going on in the player's mind. The human mind is highly susceptible to suggestion. • The quest for realism should never be an end unto itself. A successful video game will harness realism only where it serves the ultimate goal, which is entertainment. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 23 Friction • The standard dry friction model is sometimes called Coulomb friction. • Charles-Augustin de Coulomb (1736-1806). • (Image from Wikimedia Commons.) Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 24 Static Friction • When an object is at rest on top of another object, a certain amount of force is required to get it unstuck and set it in motion. • If any less force is applied, the force of friction will push back with a counteracting force up to some maximum amount. • This is called static friction. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 25 Static Friction • The following equation is a good approximation for the maximum magnitude of static friction: ππ = ππ π. • ππ is a constant called the coefficient of static friction that depends on the type of surfaces rubbing together. Just look it up in a table. • π is the magnitude of the normal force. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 26 Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 27 The Normal Force • The normal force is the force acting perpendicular to the surfaces that prevent them from overlapping. • For example, when an object (such as a bowl of petunias) is resting on top of another object (such as a table), the normal force is the force required to counteract gravity. • It is the force required to counteract the component of gravity that acts perpendicular to the surfaces. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 28 Normal and Lateral Components • If the table is at an incline, then we can separate gravity into a normal component and a lateral component. • Inside a computer, we describe the orientation of the table with a normal vector, and use the dot product to separate gravity into the relative and normal components. • Since the bowl and the table do not accelerate relative to each other, we know that the normal force of the table pushing against the bowl must be exactly equal to the normal component of the force of gravity pulling the bowl towards the table. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 29 Not Sliding Chapter 12 Notes On the Brink 3D Math Primer for Graphics & Game Dev Sliding 30 Kinetic Friction • Once static friction is overcome and the object is moving, friction continues to push against the relative motion of the two surfaces. • This is called kinetic friction. • The magnitude kinetic friction is generally less than that of static friction. • It’s computed the same way of static friction: ππ = ππ π. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 31 Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 32 Coulomb’s Law of Friction • The direction of the force of kinetic friction is always opposed to the relative motion of the surfaces. • As we said earlier, the coefficient of kinetic friction is usually less than the coefficient of static friction. • Thus, if we increase the angle of the table slowly so that static friction is just overcome, the petunias will begin to accelerate. • Coulomb's primary contribution to the theory, sometimes called Coulomb's law of friction, was that the force of kinetic friction does not depend on the relative velocities of the surfaces. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 33 Spring Forces • Even if you don't see very many actual springs in a video game, there are likely very many virtual springs at work. • Springs exhibit a general behavior that is very useful for enforcing constraints, for example, preventing objects from overlapping, cloth rendering, and rag-doll character animation. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 34 Control Systems • There are two types of spring motion, damped oscillation and undamped oscillation. • A virtual spring (often in the form of a springdamper system) is a type of control system. • There are certain advantages to be had when the physical nature of the problem is dropped and we think of it purely in mathematical terms. • Indeed, many times the problem was never really physical to begin with, and was only recast in physical terms so that the spring-damper apparatus could be applied. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 35 The Rest Length • Consider a spring with one end fixed and the other end free to move in one dimension. • When the spring is at equilibrium with no external forces on it, it has a natural length, called the rest length. • If we stretch the spring, then it will pull back to try to regain its rest length. • Likewise, if we compress the spring, it will push back. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 36 Rest length Compress Stretch Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 37 Hooke’s Law • Robert Hooke (1635 –1703). • (Image from Wikimedia Commons.) • The magnitude of the restorative force is proportional to the distance from the rest length (provided the force does not exceed the elastic limit of the material used to construct the spring). Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 38 Hooke’s Law ππ = π(ππππ π‘ − π) ππππ π‘ Where π is the spring constant that describes how stiff the spring is, ππππ π‘ is the spring’s rest length, and π is the length that the spring has been stretched or compressed to. π Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 39 Rewriting Hooke’s Law • Things get easier if we adopt a reference frame where the position x = 0 designates the rest position, in which the spring has its rest length and there are no restorative forces. • Let πΎ = π π . Since πΎ contains both the spring constant π and the mass of the particle π, it measures the spring's ability to accelerate a particle at its end. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 40 Rewriting Hooke’s Law • With those notational changes, we can rewrite Hooke’s Law as π π‘ = −πΎπ₯(π‘), where π π‘ is acceleration as a function of time and π₯ π‘ is position as a function of time. • This is called a differential equation, since it is an equation in both position π₯ π‘ and its second derivative, acceleration π π‘ . Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 41 Solving Differential Equations • We don’t have the tools to solve general differential equations, but this one is not too hard. • If we grab a spring and experimentally graph the position of its end as a function of time after compression, we get a graph like this: Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 42 Solving Our Differential Equation • This function ought to look familiar to you: it's the graph of the cosine function. • Let's see what happens if we just try π₯(π‘) = cos(π‘) as our position function. Differentiating twice to get the velocity and acceleration functions, we get: π₯(π‘) = cos(π‘) π₯ π‘ = − sin(π‘) π₯ π‘ = − cos(π‘) which is close, but we're missing the factor of πΎ. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 43 Does πΎ Matter? • To understand where πΎ should appear in π₯(π‘), consider what happens to the graph of π₯(π‘) when we change the value of πΎ. • In other words, we repeat our physical experiment and vary the stiffness of the spring. • The result is that larger values of πΎ (stiffer springs) result in a graph that is horizontally compressed: the frequency of oscillation is increased. • Likewise, smaller values of K cause the spring to oscillate more slowly, and the graph is expanded. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 44 How Much Does πΎ Matter? • Furthermore, we observe that the frequency is proportional to the square root of πΎ. For example, when we increase πΎ by a factor of four, the frequency doubles. • This gives us a hint as to where πΎ should appear, since all we are doing is scaling the time axis. π₯(π‘) = cos( πΎπ‘) π₯ π‘ = − πΎ sin( πΎπ‘) π₯ π‘ = −πΎ cos( πΎπ‘) Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 45 Angular Frequency The quantity πΎ is called the angular frequency and comes up often enough that we find it helpful to introduce the notation π = πΎ= π π and we can write the solution as π₯(π‘) = cos(ππ‘) Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 46 Three More Degrees of Freedom There are some degrees of freedom inherent in the motion of the spring that we have not accounted for. 1. We are not accounting for the maximum displacement, known as the amplitude of the oscillations and denoted π΄. Our equation always has an amplitude of 1. 2. We are assuming that π₯(0) = π΄, meaning the spring was initially stretched to the maximum displacement π΄ and released with zero initial velocity. However, in general, we could have pulled it to displacement π₯0 ≠ π΄ and then given it a shove so it has initial velocity π£0 . Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 47 The Three are Two • It would appear that we have three more variables that need to be accounted for in our equation if it is going to be completely general. • As it turns out, the three variables we have just discussed (the amplitude, initial position, and initial velocity) are interrelated. • If we pick any two, the value for the third is fixed. • We'll keep π΄ as is, but we'll replace π₯0 and π£0 with the phase offset π0 , which describes where in the cycle the spring is at π‘ = 0. • Adjustments to the phase offset have the simple effect of shifting the graph horizontally on the time axis. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 48 Simple Harmonic Motion Adding these two variables, we arrive at the general solution, the equations of simple harmonic oscillation: π₯(π‘) = π΄ cos(ππ‘ + π0 ) π₯ π‘ = −π΄π sin(ππ‘ + π0 ) π₯ π‘ = −π΄π2 cos(ππ‘ + π0 ) Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 49 Damping Forces • So far, we have been studying a physically nonexistent situation in which the spring will oscillate forever. • In reality, there are usually at least two more interesting forces, driving force and friction. • The driving force is an external force, that acts as the input to the system and causes the motion to begin. Friction we have already met. • The general term used to describe any effect that tends to reduce the amplitude of an oscillatory system is damping, and we call oscillation where the amplitude decays over time damped oscillation. • Damping forces are useful in video games, so let's discuss them in more detail. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 50 Damping Force The damping force is simply ππ = −π π₯, where ππ indicates the instantaneous magnitude and direction of the damping force, π₯ is the instantaneous velocity, and π is a constant that describes physical conditions such as the viscosity, roughness, etc. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 51 Qualitative Observations • The damping force has an extremely simple form, but things get interesting when we study motion over time. • Qualitatively, we can make some basic predictions about how damped oscillation of a spring would differ from undamped oscillation of the same spring. • The more obvious prediction is that we would expect the amplitude of oscillation to decay over time. Like the force of friction, damping removes energy from the system. • The second observation is only slightly less obvious: Since damping in general slows the velocity of the mass on the end of the spring, we would expect the frequency of oscillation to be reduced compared to undamped oscillation. • Those two intuitive predictions turn out to be correct, although, of course, to be more specific we will need to analyze the math. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 52 Harmonic Motion with Damping Forces • Combining the restorative and damping forces, the net force can be written as ππππ‘ = ππ + ππ = −ππ₯ − π π₯. • To derive the equation of motion, we will need accelerations, not forces. Applying Newton's Second Law and dividing both sides by the mass, we have: ππππ‘ ππ₯ ππ₯ π₯ = π = − π −π Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 53 Spring-Damper Systems in Video Games Spring-damper systems are used in video games as control systems. A control system takes as input a function of time that represents some target value. For example: 1. Camera code might compute a desired camera position based on the player's position each frame; 2. AI code might determine an exact targeting angle for an enemy; 3. We may have a desired player character velocity based on the instantaneous amount of control stick deflection; 4. We might have a desired screen-space position for some highlight effect, based on the currently selected choice in a menu. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 54 The Set Point • The current value of the input signal is known as the set point in control system terminology. • The set point is essentially the rest position of the spring, and the input signal is like somebody taking the other end of the spring and yanking it around. • It is similar to a driving force, but we are given a function describing a position rather than a force or acceleration. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 55 What Does a Control System Do? The job of any control system is to take this input signal and produce an output signal. Using our examples from 2 slides ago, the output signal might be (respectively): 1. The camera position to use for each frame 2. The animated targeting angle the enemy will use to aim the weapon, 3. The player character velocity. 4. The screen-space position of the highlight. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 56 No Jerks Allowed • For many control systems, the actual position and set point are not used; rather, only the error is needed. • Of course, an obvious question is, if we know the desired value, why don't we just use that directly? Because it's too jerky. • In the same way that the shocks and springs on a car don't just pass along the elevation of the road directly to the car, a control system in a video game is often designed to smooth out the bumps caused by sudden state changes that might make the camera snap to a new position or the player jerk into motion. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 57 PD Controllers • The camera or screen-space highlight are nonphysical examples in which the quantity of mass is not really appropriate and is dropped. • But the differential equations are still the same, and they have the same solution. • Stripped of the spring metaphor, we are left with what is known as a PD controller. • The P stands for proportional, and this is the spring part of the controller, since it is proportional to the current error. • The damper is the D part, which stands for derivative, because the action of the damper at any given instant is proportional to the derivative (the velocity). Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 58 Don’t Reinvent the Wheel (Or the Spring) • PD controllers (and their more robust cousin, the PID controller, where the I stands for integral and is used to remove steady-state error) are broadly applicable tools. • They have been standard engineering tools for decades (centuries?) and are well understood. • Nevertheless, they are one of the most frequently reinvented wheels in video game programming. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 59 Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 60 Tuning • Different cars have suspensions that are tuned differently; sports cars are tighter and the cars retirees like to drive are smoother. In the same way, we tune our control systems to get the response we like. • Notice that the code uses the π and π from our earlier equations. However, most people don't find those to be the most intuitive dials to have for tweaking. • Instead, the damping ratio and frequency of oscillation are used for the designer interface, while π and π are computed as derived quantities. • To tune the frequency, we might adjust either the damped or undamped version, using either angular frequency or simply Hertz; the units and absolute value are often not important because the value that feels good will be determined experimentally anyway. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 61 Alternatives and Generalizations • The second-order systems we have described here are certainly not the only type of control system, nor even the simplest, but they do behave nicely under a very broad set of circumstances and are easy to implement and tune. • Another commonly used control system is a simple first order lag, π₯ = ππ₯, under which the error decays exponentially. • This is similar to a critically damped second-order system, but with a bit jerkier response to a sudden change in the set point. • Another common technique is to chase the set point at a fixed velocity. • A filter is another broad class of control system in which the output is computed by taking some linear combination of set points or values on previous frames. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 62 Section 12.3: Momentum Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 63 Moe’s Box • Suppose Moe's box has mass π, and at a certain instant we observe it moving with a velocity π£. • Coming in late to the story, we cannot tell what magnitude of forces were used to achieve that motion, or how long the forces were applied, or what the history of the box's velocity was. Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 64 Section 12.4: Impulsive Forces and Collisions Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 65 Section 12.5: Rotational Dynamics Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 66 Section 12.6: Real-Time Rigid Body Simulators Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 67 That concludes Chapter 12. Next, Chapter 13: Curves in 3D Chapter 12 Notes 3D Math Primer for Graphics & Game Dev 68