430 likes | 544 Views
Structured Methods in Physical Modeling for Games. Àëeêcaíäp Peøeòoâ & Michael Shantz - Intel Corporation. Pros and Cons The Basics: Spatial Vectors & Coordinate Frames Inertia, F = Ia, Gravity, Contact Forces Numerical integration Articulated Bodies, Joints & Joint Limits
E N D
Structured Methods in Physical Modeling for Games Àëeêcaíäp Peøeòoâ & Michael Shantz - Intel Corporation • Pros and Cons • The Basics: • Spatial Vectors & Coordinate Frames • Inertia, F = Ia, Gravity, Contact Forces • Numerical integration • Articulated Bodies, Joints & Joint Limits • Forward Dynamics via Structural Recursion • Kinematic Loop Constraints - Mechanisms • Hybrid Dynamics & Controllers • Game Engines & Performance Issues Physical Modeling for Games
Physical Modeling Pros & Cons • Difficult - steep learning curve • Compute Intensive: collisions, dynamics • floating point limited • cache miss sensitive! • Controllers are difficult • cars are ok but try walking • Long term potential - this is gonna be great! • Incremental design - start modestly • There are many ways to cheat Physical Modeling for Games
Forward Dynamics • Given joint torques and external forces compute the resulting joint accelerations • Reduced or Generalized coordinate methods O(n) Structural Recursion (Featherstone, Brandl) joint space 1..3 DOF versus motion space 6DOF most efficient in flops, eliminates “drift” • Solve a system of constraints • Maximal coordinate methods O(n) Lagrange Multipliers (Baraff) most efficient in memory access? Handles general constraints. Serious drift problems. Physical Modeling for Games
Articulated Body Tree frame 0, body 0 3 6 DOF joint 2 body 1 5 6 4 7 i p(i) 8 9 The bodies form a tree with the bodies numbered such that the predecessor number p(i) is less than the body’s number. Structural recursion operates on this tree. Physical Modeling for Games
Basics: Rigid body i “link” Bodyi P r y Center of Mass s x framei z Bodyi has coordinate framei fixed in the body. The body may experience angular and linear velocity and acceleration. These spatial vectors may be expressed in framei or in other frames. s is a vector to bodyi’s center of mass expressed in framei Physical Modeling for Games
Articulated Body Joint Frames 2A2’ , z2 q body 1 body 2 y y 2’R1 , c1 x x y frame 2’ inner joint frame z z frame 2 outer joint frame x frame 1 z y 6 DOF joint x frame 0, “inertial frame”, world coordinates z The joint variables q are zero when the inner frame and outer frame are coincident. Frame 1 is fixed in body 1, frame 2’ is fixed in body 1 but is associated with body 2. Joint action moves frame 2 wrt frame 2’ (by rotation matrix A, translation vector z). Physical Modeling for Games
Basics: Spatial Vectors - Velocity Angular Velocity x vector specifies axis of rotation. Length of is radians per second y z jVelocityi = x Linear Velocity y z vector specifies direction Length of is meters per second The spatial velocity jvi of body i expressed in coordinate frame j includes both angular and linear components. Notation is critical. Physical Modeling for Games
Basics: Spatial Vectors - Acceleration Angular Acceleration x y vector specifies axis of rotational acceleration. Length of is radians per second2. z jAccelerationi = ax Linear Acceleration ay a a vector specifies direction. Length of a is meters per second2. az The spatial acceleration jai of body i expressed in coordinate frame j includes both angular and linear components. Physical Modeling for Games
-az ay 0 ~ ~ = - a T a = az -ax 0 -ay ax 0 Basics: The Cross Operator ~ ~ Vector Cross Product: a b = a x b The cross operator gives a matrix algebra representation of the vector cross product. Note that its transpose is its negation. For 6-vector let Physical Modeling for Games
Basics: Spatial Vectors - Force Torque: Moment of Force x y vector specifies axis of torque. Length of is newton meters: kilogram meters2 per second2. z jForcei = fx Linear Force fy f fz f vector specifies direction. Length of f is newtons: kilogram meters per second2. The spatial force jfi on body i expressed in coordinate frame j includes both torque and linear force components. Physical Modeling for Games
Basics: Force applied at a point ~ rf iforcei = = f f f P y r x z frame i The spatial force ifi on body i expressed in coordinate framei resulting from a linear force f applied at point P (line of force) in framei where r is the vector to P, has a torque = r x f (right hand rule) and a linear component f. Physical Modeling for Games
Basics: Gravitational Force on Body i 0g = [0,-9.8 meters/sec2,0]T ~ siig ifg = m ig = iR00g ig si Center of Mass y x Trick: 0a0 = -0g ig z frame i The acceleration of gravity 0g in frame 0 (world coordinates) is a 3x1 vector eg. [0, -g, 0]T. iR0 is the 3x3 rotation matrix that transforms it to frame i. si is the vector in frame i to the center of mass of body i. ifg is the spatial force expressed in frame i which is exerted on body i due to gravity. Cheap trick: just give frame0 a fake acceleration -0g. Physical Modeling for Games
Basics: Transforming Spatial Vectors kfi = kCjjfi y R x R 0 r y kfi = z ~ f -Rr R frame k x z frame j A spatial vector jfi expressed in coordinate frame j may be expressed in frame k by transforming the spatial vector by the 6x6 matrix kCj where R is the 3x3 rotation matrix st. kv = kRjjv transforms a 3 vector expressed in frame j into a vector expressed in frame k. r is a 3 vector from the origin of frame j to frame k expressed in frame j. Physical Modeling for Games
Basics: Inertia Matrix Ii , F = Ia ifi = Ii iai ~ Ii hi = ~ 0 -sz sy f a -hi Mi ~ sz -sx hi = mi 0 -sy sx 0 The force ifi on body i expressed in frame i is equal to the inertia matrix of body i times the spatial acceleration of body i. mi is the mass of body i in kilograms, s is the vector in frame i to the center of mass of body i. Use the Lien/Kajiya method to get I, m, s. mi 0 0 Mi = 0 mi 0 0 0 mi Physical Modeling for Games
Basics: Contact Forces from Collisions y • Spring damper • if = -kdx - ksx • Nonlinear spring/damper • if = -kdxnx - ksxn frame i body i x z ri if ~ riif ifc= x if P The contact force on body i due to a collision at point P which is penetrating another object by a depth of x can be modeled as a spring. The 3x1 vector if has direction based on normal to the surface and the velocity of body i (to simulate friction) . See Marhefka and Orin Physical Modeling for Games
Basics: Contact Forces kill Integrators y if body i • If time step is too large, collision may cause integrator instability. • Delayed reaction for articulated body frame i x z ri x P Physical Modeling for Games
Basics: Numerical Integration . . .. • Euler or Verlet:qi += qit; qi += qit; • Runge-Kutta Fehlberg (adaptive step size) . . If the error computed from the pair of estimates is too large, reduce t. . qi qi-1 . q Estimate 1 . Estimate 2 t ti-1 ti t The exact solution (white curve) is estimated at t time steps. See Numerical Recipes in C and excellent survey by Enright et al. Physical Modeling for Games
More about Joints 2A2’ , z2 body 1 body 2 y y 2’R1 , c1 x x y frame 2’ inner joint frame z z frame 2 outer joint frame x frame 1 z y 6 DOF joint x frame 0, “inertial frame”, world coordinates z If the joint variables q are zero then the inner frame and outer frame are coincident. Frame 1 is fixed in body 1, frame 2’ is fixed in body 1 but is associated with body 2. Joint action moves frame 2 wrt frame 2’ (by rotation matrix A, translation vector z). Physical Modeling for Games
The Joint Transform iCi-1 Rigid bodies connected by joints. iCi-1 transform iCi-1 transforms spatial vectors from framei-1 to framei. R is a rotation matrix that rotates vectors from framei-1 to framei’. A is the matrix associated with joint rotation, zi is for sliding (prismatic) joints. Physical Modeling for Games
Revolute (Hinge) Joint Joint space parameter q0 = radians about z axis cos(q0) sin(q0) 0 Rotation matrix iAi’ = -sin(q0) cos(q0) 0 0 0 1 Translation zi = 0 Project Joint space into Motion space: iAi’ , zi 0 y y 0 iq0 = 1 q0 x x 0 frame i’ inner joint frame z z frame i outer joint frame 0 0 Physical Modeling for Games
Prismatic (Sliding) Joint Joint space parameter q0 = meters along z axis 1 0 0 Rotation matrix iAi’ = 0 1 0 0 0 1 Translation zi = <0 0 q0> Project Joint space into Motion space: iAi’ , zi 0 y y 0 iq0 = 0 q0 x x 0 frame i’ inner joint frame z z frame i outer joint frame 0 1 Physical Modeling for Games
Spherical Joint “Joint space parameters” qe0 qe1 qe2 qe3 = quaternion rotation ~ ~ ~ Rotation matrix iAi’ = 1 + 2(qe qe - qe3 qe); whereqe = <qe0 qe1 qe2> and 1 is identity. Normalized Euler: | qe0 qe1 qe2 qe3| = 1. Translation zi = 0 qe = 0.5 t for qe(0) = 0 Project Joint space into Motion space: iAi’ , zi 1 0 0 y q0 y 0 1 0 iq = 0 0 1 x = q1 x 0 0 0 frame i’ inner joint frame 0 frame i outer joint frame z z 0 0 0 q2 0 0 0 Physical Modeling for Games
Articulated Body Joint Limits • Use counteracting torques to limit joints. q • r = ks(q-qmax) - kdq qmax r = 0 qmin • r = ks(q-qmin) - kdq Given joint position q and limits qmin qmax, compute limit torques to prevent joint from exceeding its range. Physical Modeling for Games
Articulated Body Forward Dynamics External forces: collisions, gravity fE Joint space accelerations Body Transforms .. Structurally Recursive Forward Dynamics Controller generated joint torques i qi Integrator qi Joint parameters, mass, inertia Ii... O(n) computational complexity Physical Modeling for Games
Articulated Body Tree frame 0, body 0 3 6 DOF joint 2 body 1 5 6 4 7 i p(i) 8 9 The bodies form a tree with the bodies numbered st. the predecessor number p(i) is less than the body’s number. Physical Modeling for Games
Articulated Body Recursion rel . vi = iCi-1 vi-1 + vrel vrel = iqi = rel .. ~ AR(i-1rel) ai = iCi-1 ai-1 + iqi + i i = ~ ~ AR(i-1 (i-1 ri + 2rel)) _ ~ T fi = jCi fj + Iiai - i i Ii i i = fEi - ~ ~ j s(i) mii (i si) The velocity of bodyi is just the velocity of bodyi-1 expressed in framei plus the relative velocity due to joint motion. Acceleration is similar except for the centripetal acceleration term i. Force is similar except that recursion is from successor bodies s(i), plus bodyi force Ia, minus external, Coriolis, and centrifugal forces i . i is the angular velocity of bodyi , si the vector to center of mass, ri = ci + zi . Physical Modeling for Games
Structurally Recursive Forward Dynamics Velocity dependent terms Brandl algorithm for case with no loops. Structurally recursive O(n) algorithm “felt” inertias accelerations Physical Modeling for Games
Matrix and Vector info 6x1 Size of matrix variables assuming joints have ni DOF; no kinematic loops 6xni 6x6 nix1 nixni Rows x Columns 6x6 6x1 nix1 6-nix1 6-nix6 Physical Modeling for Games
Create Dynamic Model from Static Modeling work by Sonja Jeter, Nola Donato The “Mechanic” toolkit for interactively creating dynamic models from art obtained from modelers such as 3DStudio MAX. Physical Modeling for Games
Forward/Inverse Dynamic Walking Modeling work by Sonja Jeter, Nola Donato The “Mechanic” toolkit for interactively creating dynamic models from art obtained from modelers such as 3DStudio MAX. Physical Modeling for Games
Kinematic Loops Behav • Body • Joint & Rigid Body • Set Joint Torques • Set External Forces • BaseBody • Compute Body • Accelerations • Loop Joint • Breaks Cycles Base Body Joint t Body Body Body Body Body Loop Joint
Kinematic Loop Objects Behav G G G BaseBody Body t Body : TransformGroup Joint & Rigid Body Set Joint Torques Set External Forces BaseBody : Body Compute Dynamics Loop : Body loop constraint equations Body Body Loop
Mechanisms Modeling by Sonja Jeter, Jill Hunt, Nola Donato Steam engine with 3 kinematic loop constraints. The crankshaft, the rocker arm pivot, the valve and piston cylinders are all fixed to the base. Physical Modeling for Games
Hybrid Dynamics & Controllers 1 (t) f(t) Physical Modeling for Games
- - Hybrid Dynamics & Controllers 2 • Forward Dynamics • Inverse kinematics • Hybrid dynamics Physical Modeling for Games
Hybrid Dynamics & Controllers 3 a = f/m x = -kdx - ksx Physical Modeling for Games
Hybrid Dynamics & Controllers 4 opt f(p) min f(p) Physical Modeling for Games
Performance • Equipment • Intel 400 MHz Pentium II Processor • Dynamic Models • Raptor: • 1500 polygons, 50 DOF, • 12 inverse dyn. • 40K-80K floating ops per frame • 25 fps dynamics + render + skin • 300+ fps dynamics only. • Steam Engine: • 1000 polygons, 11 joints, 3 loops • 70K floating ops per frame • 50 frames per second. Physical Modeling for Games
Optimizations • Behaviors: behavioral culling, stop/start, freeze/resume • Collisions: OBB inner loop: Parallel float optimize Hierarchical Sphere bounds, Collide sets, kinetic prediction, interval arithmetic • Dynamics: Linear Algebra: Parallel float opt. • Occlusion: Portals, Visibility map (UNC), Switch, app cull • Scaleable: LOD, Billboards, Grid Sort, BSP • Render: Threads - Behavior, Cull, Draw, Audio Property sort, Tri-strips, Flatten Physical Modeling for Games
Math Library Templated Matrix Library #define e4(i, j, l, m, n, a, b) \ flm(0, i, j, m, n, a, b) \ flm(1, i, j, m, n, a, b) \ flm(2, i, j, m, n, a, b) \ flm(3, i, j, m, n, a, b) \ __asm faddp st(1), st(0) \ __asm fxch st(2) \ __asm faddp st(1), st(0) \ __asm faddp st(1), st(0) \ __asm fstp dword ptr [eax + mi(l, 0, i, j)] Resizable Matrix Library ftable_mmm(Add, operator+); inline ISMXTmpMatrix& __fastcall operator+(const ISMXMatrix& A, const ISMXMatrix& B) { void* pf = AddPool[ISMXMatrix::lutSizes[A.nr]][ISMXMatrix::lutSizes[A.nc]]; pf = ((PVOIDMX2&)pf)(A, B);; (int&)((ISMXTmpMatrix*) pf)->nr = (int&)A.nr; return *(ISMXTmpMatrix*) pf; } Physical Modeling for Games
Key References 1 • Andy Witkin, Dave Baraff, and Michael Kass • http://www.cs.cmu.edu/afs/cs/user/baraff/www/pbm/pbm.html • B. Mirtich, Multibody Dynamics Package • http://www.merl.com/people/mirtich/multibodyDynamics.html • Chris Hecker's Rigid Body Dynamics Information • http://www.d6.com/users/checker/dynamics.htm • This presentation • http://www.cgdc.com/1998/vault/wcd00506.doc Physical Modeling for Games
Key References 2 • Brandl et al ‘86, ‘87 Structurally recursive methods • Featherstone ‘87 Spatial vectors • Lien & Kajiya ‘84 Integral properties of polyhedra • Marhefka & Orin ‘96 Contact forces • Roberson & Schwertassek ‘87 Great reference text • Baraff ‘96 Lagrange Multipliers • Walker & Orin ‘85 Classic robotics paper • Enright et al ‘95 Runge-Kutta Integrators Physical Modeling for Games
Acknowledgments Animation&Vision Group @ MGL @Intel Baining Guo Demetri Terzopoulos Feng Xie Gary Bradski John Funge Mark Holler Radek Grzeszczuk Xiaoyuan Tu NSTL Àëeêceé Ñóïèêoâ Physical Modeling for Games