Create Presentation
Download Presentation

Download Presentation
## The Physics That Brought Cel Damage to Life: A Case Study

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**The Physics That BroughtCel Damage to Life: A Case Study**• David Wu, Director of Technology, Pseudo Interactive Inc. GDC 2002**Introduction**When I first starting programming Game Physics I had certain beliefs: • If I am not careful people will steal my great ideas • Programming is a battle • Developer Vs Developer • Whomever has the best physics code wins**Introduction (cont)**Since then I have learned: • My great ideas are not that great • Programming is a battle • Developer Vs Physics • Physics usually wins**Introduction (cont)**Without furtur ado - the goals of this lecture: • To Share with you our not so great ideas • To help build the numerical arsenal of programmers around the world in hopes that one day we will win.**Motivation**Together we will move mountains… We will conserve momentum! We will make great Games! We will show the world that physics can be fun! And if all goes well - our integrators will not explode!**Motivation (cont)**Are you motivated yet?**Motivation (cont)**Now go and motivate your producer • Convince her that Physics will make a better game • You will Sell More units • Programmers will think that you are cool**P.I.T.A**• “Pseudo Interactive Technology Arsenal” • Programmers like to name their engines • No one else cares. • That doesn’t stop us.**The PITA Pipeline**-Collect Input -Collision Detection -Logic and AI -Integration -Render+Network Output -Rinse, Lather and repeat Easy as PI**The PITA Pipeline**• Their are two key opponents that you must defeat to build a successful physics system. • Once you have them nailed, you’ve won 96% of the battle. • These opponents are: • Numerical Integration • Degrees of Freedom • This battle is the focus of my lecture**DOF**• Its all about “DOF” • aka “Degrees of Freedom” • Game entities are described in terms of their DOF • The Rendering Engine draws DOF wraped in tri-strips • The Network system sends DOF to your friends and enemys. • The Integrator changes the DOF over time • The Collision detection system determines incompatible DOF • AI attempt to control DOF • DOF make the world go around • DOF make your game fun!**DOF Example**• Sheep**6DOF**6DOF 6DOF 6DOF -3DOF -3DOF -3DOF DOF Example • Constraint Model • 6 DOF Rigid bodies joined by constraints • 24 DOF • -9 DOF in Constraints**3DOF**3DOF 3DOF 6DOF DOF Example • Independent coordinates • Each part described by a minimal DOF representation • i.e. Euler Angles • 15 DOF • No Contstraints**q0**q0’ q1 q1’ DOF Example • Natural Cartesian Coordinates • DOF are described by N points in space connected by constraints • Mostly –1 DOF rod constraints • 39 DOF • -24 DOF of contstraints • No Angular anything**Numerical Integration**• The main weapon in pita’s numerical arsenal • With a good integrator you can accomplish anything. • Implicit Euler is like a Nuclear Missile: • sloppy • sometimes overkill • robust • effective • handles everything that you throw at it**The Process:**Constraints Forces Contacts Potentials Actuators Qt0 Qt0’ • All DOF are collected into a global state vector • Q and its derivative Q’ • Q is positiion • Q’ is velocity • All external influences are collected as potentials • forces, springs, constraints, contacts • give rise to a force vector F • Boom! • the integrator does it’s stuff • You are left with new Q and Q’ vectors. Qt1, Qt1’ Qt1 Qt1’ BOOM! (integration)**The Pita Paradigm**To formulate the problem of dynamics simulation in such a way that it might be solved in real time. pita requires that a few Axioms be accepted on Faith alone. These axioms serve as the foundation for the Implicit Euler Integration Scheme.**Axiom #2**• Time is Discrete • The sampling resolution is 30hz • i.e. • t0,t1,t2 are instances in time • each instance is separated by 1/30 seconds • Europeans are a little slower, at 25hz**Axiom #3:**• Acceleration occurs during short impulses • Like a Dirac Delta Function. • You have you state vector at time t0: Qt0, Qt0’ • Bang • Q’’ occurs, Q’ jumps by Q’’*dt; • Q’ is then constant throughout the time step • Q is piecewise linear • Qt1’ = Qt0’ + Q’’*dt • Qt1 = Qt0 + Qt0’*dt + Q’’*dt2 • Not Qt1 = Qt0 + Qt0’*dt + ½*Q’’*dt2**Axiom #4**• The Acceleration at time t0 satisfies Newton’s second law at time t1 • Ft1 = Mt1*Q’’ • Mt1*Q’’ – Ft1 = 0 • like F=MA but better**The Equations of Motion**• We can think of the implicit Euler equations as constraint equations acting on our DOF. • At first you might think: • Constraint Equations? Those are the bad guys right? • Constraint Equations are like mercenaries, sometimes you have to fight them, sometimes you can use them to eliminate your opponents. • In our case we will use them against our foes, we will use them to eliminate some redundant DOF, namely acceleration (Q’’)**The Equations of Motion**• The equations of motion then become: • Mt1*Qt1’ – Mt0*Qt0’ – Ft1*dt = 0 • Looks like conservation of momentum: • MVt1 = MVt0 + integral(F)dt • Now that we’ve eliminated acceleration and we know that • Qt1 = Qt0 + Qt1’*dt • we can focus our efforts on computing Qt1’ • the velocities at the next time step**q0**q0’ q1 q1’ The Sheep • Now we are ready to model the dynamics of the Sheep. • We will use natural cartesian coordinates. • Q is a vector of 13 3d points • q0,q1,q2 .. q11 • 36 DOF • Q’ is a vector of 13 3d velocities • q0’,q1’,q2’ .. q11’**The Plan of attack**• Guess what Qt1’ might be • Determine what Qt1 would be for this Qt1’: • Qt1 = Qt0 + Q’t0*dt • Each point of the Sheep is moved using its candidate velocity • Compute the inverse dynamics term: • Mt1*Qt1’- Mt0*Qt0’ • Compute the sheep’s new momentum and subtract its previous momentum**The Plan of attack (cont)**• Compute the external forces resulting from this new state vector, Ft1 • Loop through the Rods holding the Sheep together, compute their forces • Apply gravity • Apply contacts and other forces • Determine the error, or Residual: • R(Qt1) = Mt1*Qt1’- Mt0*Qt0’ – Ft1*dt • If R(Qt1) == 0 • Qt1’ is valid for this time step • we are done! • If not, take another guess for Qt1’ and try again.**Will it Work?**• You might be thinking: • “This plan sounds a little Dodgy” • or • “I am not convinced that the Tinker Toy Sheep model is an accurate representation of a real two legged sheep” • I claim that this method is theoretically sound and will produce correct Dynamics for a system governed by the previously stated Axioms.**Three Key Concepts:**• Kinematics • Steps Qt0 to Qt1 • Defines the relationship between Q and the Sheep • Inertia • Defines the relationship between forces and accelerations • Integrate w.r.t. time and you have the relationship between momemtum and impulses • Potentials • Potentials are anything that might give rise to external forces • Gravity, constraints, collisions, contacts, actuators, etc. • All potentials must be able to determine their force vector for a given Sheep Configuration at a given time.**The DOF Interface**• This framework is general in that it can handle any selection of DOF • In Cel Damage we use a number of different DOF representations, the only code required by each is Kinematics, Inertia and Potentials. Interface IDof -Kinematics -Inertia -Potentials Particles Rigid Bodies Articulated Figures Finite Elements**q0**q0’ q1 q1’ Kinematics: • Kinematics relates the Sheep’s DOF to it’s body. For any selection of DOF there are unique Kinematic equations relating any point on the Sheep to some function of the DOF • The sheep has 4 Rigid parts • head body, leg0, leg1 • Each part has four points embedded in it, which describe its DOF • The points uniquely define position, orientation, velocity and angular velocity of each part.**Kinematics:**• The Center of Mass of the head is at a position defined by a linear combination of the points q0,q1,q2,q3; • C = 0.4*q0 + 0.1*q1 + 0.2*q2+ 0.3*q3 • Similarly, the velocity of the head’s center of mass is: • V = 0.4*q0’ + 0.1*q1’ + 0.2*q2’ + 0.3*q3’ • The numbers (0.4, 0.1, 0.2,0.3) are the barycentric coordinates of the head’s center of mass. • This transform can be referred to as the Jacobian J: • [0.4 0 0 0.1 0 0 0.2 0 0 0.3 0 0 ] • [ 0 0.4 0 0 0.1 0 0 0.2 0 0 0.3 0 ] • [ 0 0 0.4 0 0 0.1 0 0 0.2 0 0 0.3 ] • Vhead = J*Q’**Kinematics:**• This transform can be referred to as the Jacobian J: • [0.4 0 0 0.1 0 0 0.2 0 0 0.3 0 0 ] • [ 0 0.4 0 0 0.1 0 0 0.2 0 0 0.3 0 ] • [ 0 0 0.4 0 0 0.1 0 0 0.2 0 0 0.3 ] • And the velocity mapping is: • Vhead = J*Q’**Inertia**• The Inertia of the Sheep is a 36x36 sparse tensor “M”. • The tensor of Inertia can be derived by analytic integration within the volume of the sheep. • Conceptually, At each point in the sheep you take the infinitesimal bit of volume, multiply it by the Sheep’s density at that point in space, and project it onto the DOF that move the point, weighted by a Jacobian. • J*m*Jt**Inertia**• You can think of this as scattering little bits of mass across the Sheep’s DOF, or spray painting the Tensor M. • Computation of the Inertia Tensor may take a fair bit of processing, but the Inertia Tensor for this selection of DOF is constant. The alternative DOF representations of the Sheep mentioned earlier vary with the state vector Q and must be updated or recomputed whenever Q changes.**Constraints**• Each rod constraint maintains a fixed distance between two points: • |q1 – q0| - L = 0 • We approximate this using a very stiff linear springs. Its potential energy is: • ½ k*(|q1 – q0| - L)^2 • K is very large. • Doing a little differentiation we get the Force Applied: • f0 = (q1-q0)/|q1-q0| *k* (L - |q1-q0| ) • f1 is –f0 • This is a non-linear function dependant on (Q)**Constraints**• If someone asks you what you are doing, don’t admit that you are using penalty methods. • Tell them that it’s Quadratic Programming – you are using an interior point method. • While she ponders this claim, switch topics. • Talk about just how much designers enjoy over-determined systems, and how Lagrange Multipliers perform in these situations.**Solving:**• As it turns out, random guessing is not the fastest way to find a valid Q t1’. • More sophisticated techniques such as the Conjugate Gradient Method or Newton’s Method are usually a better choice. • If we do a little hand waving we can repose our Qt1’ guessing as a Mathematical Optimization problem. The Mathematical Optimization people have come up with all sorts of neat ways to minimize scalar functions, we will exploit their research to help fight the battle.**Solving:**• The Residual R(Qt1’) is Disguised to look like a Scalar function: • ½ R(Qt1’)t* R(Qt1’) • This function is minimized when it’s gradient is 0 • Mt1*Qt1’- Mt0*Qt0’ – Ft1*dt ==0 • We can trick the optimizer into finding a valid Qt1’ for which • R(Qt1’) == 0 • The solution to the forward dynamics equations!**Jacobi Iterations:**• The essential concept is to “divide and conquer”. • We know how to solve one point in isolation, so we just ignore the system coupling and solve each point independently. • Here is our Jacobi plan to take on the sheep: • Split up • Squadron #0 takes on the first point of the rear leg • Squadron #1 takes on the second point of the body • Squadron #2 takes on the first point of the head • etc.**Jacobi Iterations:**• Many people start with Jacobi iteration, even if they don’t know what Jacobi iteration is. • It is intuitive and often faster than random Guessing • Jacobi is a nice way to prototype systems, it is easy to implement, it parallelizes well, and it requires little memory for high DOF systems. • From a practical standpoint Jacobi iterations are a good way to pre-condition your system for the Conjugate Gradient Method, but not suitable as a general solver for real-time dynamic simulation in most games.**Gauss Seidel**• A variant on Jacobi iterations, adds some communication between squadrons. • Squad #0 tells Squad #1 it’s results. Sometimes • This helps to prevent some of the ping-pong stale mates seen with Jacobi Iteration.**Successive Over Relaxation**• If you are dead set on using a Jacobi or Gauss Seidel you should look into the literature on SOR • SOR is a variant that is finicky but effective for certain problems**Steepest Descent**• A problem with the Jacobi method is that when you solve one batch of DOF, other DOF become unsolved. • I.e. • While you attempt to eliminate the residual for p1’s DOF p0 re-spawns. • First you eliminate the residual for p0 • Steepest Descent is like Jacobi iteration without the divide and conquer part. • We attack all of the DOF at once. • To find a better Q’ we pick a search direction: S, and walk in this direction until we reach the point where the residual is minimal.**Steepest Descent**• To find a better Q’ we pick a search direction: S, and walk in this direction until we reach the point where the residual is minimal. find the scalar ‘p’ that minimizes: • R(Q’ + p*S)t*R(Q’ + p*S) • for the search direction S • p is the distance along S that you walk.**Steepest Descent**• Using this strategy we can keep tabs on all DOF at once • hopefully, no one will respawn when we are not watching. • The search direction that we choose is simply the residual: • R(Qt1’) = Mt1*Qt1’ – Mt0*Qt0’ - Ft1 • The following example illustrates the choice we have made for the residual: • The sheep’s ear is being pulled with a force (1, 0, 0), but is momentum has changed by (0,1,0), we will search in a direction that should move the momentum towards 1,0,0, namely (1,-1,0) which is the residual.**Steepest Descent**• The following example illustrates the choice we have made for the residual: • The sheep’s ear is being pulled with a force (1, 0, 0) • its momentum has changed by (0,1,0) • we will search in a direction that should move the momentum towards 1,0,0 • namely (1,-1,0) • which is the residual.**The Conjugate Gradient Method**• The conjugate Gradient method uses information from previous search directions to help guide the selection of future search directions • The second search direction is chosen such that it does not step on the toes of the first search**The Conjugate Gradient Method**• If your system is quadratic, the sequence of residuals resulting from each step are mutually conjugate with respect to the Hessian of the quadratic • In practice the equations of motion resemble quadratics near the solution • This is the method that I used for Cel Damage Xbox**Newton’s Method**• If the system resembles a quadratic, why not just pick a search direction that will solve the quadratic in one step? • This is the rationale behind newtons method**Newton’s Method**• We can approximate the residual: • R(Qt1’) = Mt1*Qt0’ – Mt1*Qt1’- Ft1 • With a first order power series: • R(Qt1’) ~= R(Qt0’) + d/dQ’R(Qt0’)*(Qt1’ – Qt0’) • This is a linear system that can be solved using standard techniques (LDL, LU, Gaussian elimination, etc) to find Q’t1. • We can then use Qt1’ – Qt0’ as our search direction. • The matrix d/dQ’R(Qt0’) is called the Hessian.