340 likes | 354 Views
Rigid Body Dynamics. Jim Van Verth (jim@essentialmath.com). Rigid Body Dynamics. Simplest form of physical simulation Gets you a good way towards making a more realistic looking game Not that hard, either. Rigid Body. Objects we simulate will not deform Brick vs. clay
E N D
Rigid Body Dynamics Jim Van Verth (jim@essentialmath.com)
Rigid Body Dynamics • Simplest form of physical simulation • Gets you a good way towards making a more realistic looking game • Not that hard, either Essential Math for Games
Rigid Body • Objects we simulate will not deform • Brick vs. clay • Fixed model: only change position and orientation Essential Math for Games
Dynamics • Want to move objects through the game world in the most realistic manner possible • Applying velocity not enough – need ramp up, ramp down – acceleration • Same with orientation Essential Math for Games
Calculus Review • Have function y(t) • Function y'(t) describes how y changes as t changes (also written dy/dt) • y'(t) gives slope at time t y y(t) y'(t) t Essential Math for Games
Calculus Review • Our function is position: • Derivative is velocity: • Derivative of velocity is acceleration Essential Math for Games
Basic Newtonian Physics • All objects affected by forces • Gravity • Ground (pushing up) • Other objects pushing against it • Force determines acceleration (F = ma) • Acceleration changes velocity ( ) • Velocity changes position ( ) Essential Math for Games
Basic Newtonian Physics • Assume acceleration constant, then • Similarly Essential Math for Games
Basic Newtonian Physics • Key equations • Note: force is derivative of momentum P • Remember for later – easier for angular Essential Math for Games
Basic Newtonian Physics • General approach • Compute all forces on object, add up • Compute acceleration • (divide total force by mass) • Compute new position based on old position, velocity, acceleration • Compute new velocity based on old velocity, acceleration Essential Math for Games
Newtonian Physics • Works fine if acceleration is constant • Not good if acceleration dependant on position or velocity – changes over time step • E.g. spring force: Fspring = –kx • E.g. drag force: Fdrag = –mv Essential Math for Games
Analytic Solution • Can try and find an analytic solution • I.e. a formula for x and v • In case of simple drag: • But not always a solution • Or may want to try different simulation formulas Essential Math for Games
Numeric Solution • Problem: Physical simulation with force dependant on position or velocity • Start at x(0) = x0, v(0) = v0 • Only know: • Basic solution: Euler’s method Essential Math for Games
Euler’s Method • Idea: we have the derivative (x or v) • From calculus, know that • Or, for sufficiently small h: Essential Math for Games
Euler’s Method • Can re-arrange as: • Gives us next function value in terms of current value and current derivative Essential Math for Games
Final Formulas • Using Euler’s method with time step h Essential Math for Games
What About Orientation? • Force (F) applies to center of mass* of object – creates translation • Torque () applies to offset from center of mass – creates rotation • Add up torques just like forces Essential Math for Games
Force vs. Torque (cont’d) • To compute torque, take cross product of vector r (from CoM to point where force is applied), and force vector F • Applies torque ccw around vector r F Essential Math for Games
Other Angular Equivalents • Force F vs. torque • Momentum P vs. angular momentum L • Velocity v vs. angular velocity • Position x vs. orientation • Mass m vs. moments of inertia J Essential Math for Games
Why L? • Difficult to compute angular velocity from angular acceleration • Compute ang. momentum by integrating torque • Compute ang. velocity from momentum • Since then Essential Math for Games
Moments of Inertia • Moments of inertia are 3 x 3 matrix, not single scalar factor (unlike m) • Many factors because rotation depends on shape and density • Describe how object rotates around various axes • Not easy to compute • Change as object changes orientation Essential Math for Games
Computing J • Can use moments of inertia for closest box or cylinder • Can use sphere (one factor: 2mr2/5) • Or, can just consider rotations around one axis and fake(!) the rest • With the bottom two you end up with just one value… can simplify equations Essential Math for Games
Computing J • Alternatively, can compute based on geometry • Assume constant density, constant mass at each vertex • Solid integral across shape • See Eberly for more details • Also at www.geometrictools.com Essential Math for Games
Using J in World Space • Remember, • J computed in local space, must transform to world space • If using rotation matrix , use formula • If using quaternion, convert to matrix Essential Math for Games
Computing New Orientation • Have matrix and vector • How to integrate? • Convert to give change in • Change to linear velocity at tips of basis vectors • One for each basis gives 3x3 matrix • Can use Euler's method then Essential Math for Games
Computing New Orientation • Example: Essential Math for Games
Computing New Orientation • r gives linear velocity at r • Could do this for each basis vector • Better way: • Use symmetric skew matrix to compute cross products • Multiply by orientation matrix Essential Math for Games
Computing New Orientation • If have matrix, then where Essential Math for Games
Computing New Orientation • If have quaternion q, then • See Baraff or Eberly for derivation where Essential Math for Games
Computing New Orientation • We can represent wq as matrix multiplication where • Assumes q = (w, x, y, z) Essential Math for Games
Angular Formulas Essential Math for Games
Reducing Error • Keep time step as small as possible • Clamp accelerations, velocities to maximum values – avoid large forces • If velocity, acceleration very small, set to zero (avoids little shifts in position) • Damping acceleration based on velocity (i.e. friction) can help Essential Math for Games
Improving Performance • If not moving, don’t simulate • Only do as much as you have to • If you can fake it, do so • objects on ground, don’t bother with gravity • only rotate around z, don’t bother with J • simple drag instead of full friction model Essential Math for Games
References • Burden, Richard L. and J. Douglas Faires, Numerical Analysis, PWS Publishing Company, Boston, MA, 1993. • Hecker, Chris, “Behind the Screen,” Game Developer, Miller Freeman, San Francisco, Dec. 1996-Jun. 1997. • Witken, Andrew, David Baraff, Michael Kass, SIGGRAPH Course Notes, Physically Based Modelling, SIGGRAPH 2002. • Eberly, David, Game Physics, Morgan Kaufmann, 2003. Essential Math for Games