Baraff, 1991 Animation CS 551 / 651 Lecture 4 Rigid-body Simulation Warm-up Assignment Any questions? Zip and email your file to dbrogan@cs Assignment 1 Rigid-body Simulator Goal: To demonstrate understanding of rigid-body dynamics by writing a simulator • 3ds file loader (given) • Compute COM and MOI (given) • User-selected force vector (how?) • Compute location of impact (ray / polygon intersection) • Compute change in linear and angular accelerations (Hecker) • Animate… Rigid-body Simulator Not permitted resources • Physical simulation source code – Not from web, books, bathroom wall… Permitted resources • All books (physics, graphics, C++, game design, …) • Graphics interface libraries • File loader and MOI libraries Rigid-body Simulator Deliverables • Load any legal (closed) 3ds model an aside… Closed objects AKA “solid” More rigorously: closed, orientable manifolds • Local neighborhood of all points isomorphic to disc • Boundary partitions space into interior & exterior Yes No Manifold Examples of manifold objects: • Sphere • Torus • Well-formed CAD part Back-Face Culling Examples of non-manifold objects: • A single polygon • A terrain or height field • polyhedron w/ missing face • Anything with cracks or holes in boundary • one-polygon thick lampshade Rigid-body Simulator Deliverables (basic) • Load any legal (closed) 3ds model • Define an arbitrary force vector • Animate the model as is moves Rigid-body Simulator Deliverables (intermediate) • Add gravity and drop object to the ground. Use constraint forces to make it rest there • Weld a spring to a vertex of the model and suspend model by spring from ceiling – Why springs? Because it is much more complicated to force the model to hang from a string (or bar) of fixed length… more constraints to worry about Rigid-body Simulator Deliverables (advanced) • Add more objects and springs – Newton’s Cradle – Join two objects with springs – Suspend one object by spring and drop other objects on it • Implement constrained dynamics – Replace springs with rods Rigid-body Simulator Grading will take place during a demo you’ll provide • Time TBA, but likely 3.0 weeks from now • Either bring your machine to the demo • Or make sure it works on Windows machine with VisualStudio.net (we’ll try to make a Linux machine) – Machines in Stacks and Small Hall should be set up this way Reading for next Tuesday Timewarp Rigid Body Simulation, B. Mirtich, SIGGRAPH 2000. Stephen Chenney and D.A.Forsyth, "Sampling Plausible Solutions to Multi-Body Constraint Problems". SIGGRAPH 2000 Conference Proceedings, pages 219-228, July 2000. Collisions We’re in 2D… Collisions are: • Vertex / edge • Vertex / vertex • Edge / edge Compute normal to collision • v/e: perpendicular to edge (pointing towards A by convention) • e/e: perpendicular to edge • v/v: something reasonable (perhaps use an edge-perp) Collisions Compute relative normal velocity v AB n (v AP v BP ) n – Must be negative for a collision to take place – If equal to 0… resting contact (special case) After collision is detected Consider applying a force to both bodies • This is how nature “simulates” collisions • Already interpenetrating objects will remain in this state for at least one more time step – Cannot instantaneously change velocity – Forces need time to be integrated to accelerations and then to velocities After collision is detected We need instantaneous change in velocity • Impulse – This is a hack that gets us out of the jam we created when assuming impenetrable bodies exist – Generalization of subtle surface properties – Like simulating a large force for a small time step – Will change velocity like we need Calculating impulse Duration of impulse is “no time” • This is a small amount of time • All other forces are ignored during this period No friction Coefficient of resitution • Models complicated compression and restitution of impacting bodies • Models dissipation of energy Coefficient of restitution AB v n v AB n = 1 superball (perfectly elastic) = 0 clay (perfectly inelastic) Calculating an impulse Solve for one number, j • Apply j in direction of n to A • Apply j in direction of –n to B equal and opposite Computing Impulse 1st: Assume objects cannot rotate 2nd: Use definition of coeff. of rest. to derive a second set of v+ equations 3rd: Use substitution to solve for j Computing impulse Things to note: • n doesn’t have to be normalized • A or B can be fixed by setting mass to infinity • If MA = 1, MB = inf, vB = 0, =1 – Computes reflection of vA about n Accounting for rotation Consider velocity of collision point P after collision rperp Derived from two equations Accounting for rotation Lots of equations from previous slides Solve for j Notes Time of impact • Must apply impulse exactly at time of impact – After detecting interpenetration, use binary search (or more sophisticated) to fine tune – Beware of “tunneling” when dt is so large collisions are missed • Edge/edge collisions are modeled as point/point in this system • Only two colliding bodies at a time • 3D is harder because of variety of collision types Rotation in 3-D Looks similar to 2-D Case • Specify arbitrary rotation as three angles – One per coordinate axis – Called an Euler Angle – Common representation – But order of rotations matters 0 1 0 cos Rotation x 0 sin 0 0 cos 0 Rotation y sin 0 cos sin Rotation z 0 0 0 sin cos 0 0 sin 1 0 0 cos 0 sin cos 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 1 Rotation Matrices Orthonormal matrix: • orthogonal (columns/rows linearly independent) • normalized (columns/rows length of 1) The inverse of an orthogonal matrix is just its transpose: a b d e h i 1 c a b f d e h i j T c a f b c j d e f h i j Rotation Matrix 9 DOFs must reduce to 3 Rows must be unit length (-3 DOFs) Rows must be orthogonal (-3 DOFs) Drifting matrices is very bad • Numerical errors results when trying to gradually rotate matrix by adding derivatives • Resulting matrix may scale / shear • Gram-Schmidt algorithm will re-orthogonalize your matrix Difficult to interpolate between rotation matrices Angular Velocity Vector In 2D we just had w, a scalar In 3D we need a vector, call it w Cross Product The cross product or vector product of two vectors is a vector: ux uy uz v1 v 2 x1 x2 y1 y2 z1 z2 determinant y1 z 2 y 2 z1 ( x1 z 2 x 2 z1) x1 y 2 x 2 y1 The cross product of two vectors is orthogonal to both Right-hand rule dictates direction of cross product Cross Product Right Hand Rule • • • • See: http://www.phy.syr.edu/courses/video/RightHandRule/index2.html Orient your right hand such that your palm is at the beginning of A and your fingers point in the direction of A Twist your hand about the A-axis such that B extends perpendicularly from your palm As you curl your fingers to make a fist, your thumb will point in the direction of the cross product Cross Product Right Hand Rule • • • • See: http://www.phy.syr.edu/courses/video/RightHandRule/index2.html Orient your right hand such that your palm is at the beginning of A and your fingers point in the direction of A Twist your hand about the A-axis such that B extends perpendicularly from your palm As you curl your fingers to make a fist, your thumb will point in the direction of the cross product Cross Product Right Hand Rule • • • • See: http://www.phy.syr.edu/courses/video/RightHandRule/index2.html Orient your right hand such that your palm is at the beginning of A and your fingers point in the direction of A Twist your hand about the A-axis such that B extends perpendicularly from your palm As you curl your fingers to make a fist, your thumb will point in the direction of the cross product Cross Product Right Hand Rule • • • • See: http://www.phy.syr.edu/courses/video/RightHandRule/index2.html Orient your right hand such that your palm is at the beginning of A and your fingers point in the direction of A Twist your hand about the A-axis such that B extends perpendicularly from your palm As you curl your fingers to make a fist, your thumb will point in the direction of the cross product Cross Product Right Hand Rule • • • • See: http://www.phy.syr.edu/courses/video/RightHandRule/index2.html Orient your right hand such that your palm is at the beginning of A and your fingers point in the direction of A Twist your hand about the A-axis such that B extends perpendicularly from your palm As you curl your fingers to make a fist, your thumb will point in the direction of the cross product Calculating change in r Computing change in r requires cross product Cool matrix math does the same Rotation matrix Kinematics for point on moving body Differentiate by parts Angular acceleration Centripetal acceleration term! 3D Dynamics Very similar to 2D • Angular momentum becomes a vector: r x p – r = vector from origin to point – p = linear momentum at point • It measures the amount of momentum that is perpendicular to r Total angular momentum linear momentum substitute Inertia Tensor Use the tilde operator Use LAT and IA-1 (never changes) to compute w Pieces come together Linear terms are identical to their 2-D analogs Angular terms follow same pattern • Orient the default inertial matrix to align with current orientation • Compute angular velocity by multiplying angular momentum by inertial matrix • Use angular velocity to compute change in orientation Numerical Integration (from Dr. Tom Hobbs Systems Ecology course at Colorado State) Y = f(t), unknown f(t+t), unknown Y dy dx t, specified t known Example dy 6 y .007 y 2 dt Analytical solution to dy/dt Y0 = 10 t = 0.5 point to estimate Euler (pronounced “oiler”) y0 = 10 analytical y k1 = dy/dt at y0 y k1 = 6*10-.007*(10)2 y = k1*t estimated y yest= y0 + y y t = 0.5 Runge-Kutta (pronounced Run-gah Kut-tah) point to estimate Problem: estimate the slope to calculate y y dy 6 y .007 y 2 dt t = 0.5 Runge-Kutta (4th order) ( xt t / 2, yt k1t / 2) f '(t , y ) derivative at (t , y ) slope = k1 k1 f '(t , y ) k1t / 2 y k2 f '(t t / 2, y k1t / 2) k3 f '(t t / 2, y k2 t / 2) k4 f '(t t , y k3 t ) (t , y ) t/2 t 1 yt yt t (k1 2k2 2k3 k4 ) 6 Step 1: Evaluate slope at current value of state variable. y0 = 10 k1 = dy/dt at y0 k1 = 6*10-.007*(10)2 k1 = 59.3 k1=slope 1 Step 2: Calculate y1at t +t/2 using k1. Evaluate slope at y1. y1 = y0 + k1* t /2 y1 = 24.82 k2 = dy/dx at y1 k2 = 6*24.8-.007*(24.8)2 k2 = 144.63 y1 t = 0.5/2 k2=slope 2 Step 3: Calculate y2 at t +t/2 using k2. Evaluate slope at y2. y2 = y0 + k2* t /2 y2 = 46.2 k3 = dy/dt at y2 k3 = 6*46.2-.007*(46.2)2 k3 = 263.0 y2 t = 0.5/2 k3 = slope 3 Step 4: Calculate y3 at t +t using k3. Evaluate slope at y3. y3 = y0 + k3* t y3 y3 =141.0 k4 = dy/dt at y2 k4 = 6*141.0-.007*(141.0)2 k4 = 706.9 y2 t = 0.5 k4 = slope 4 Step 5: Calculate weighted slope. Use weighted slope to estimate y at t +t weighted slope = Yt 1 (k1 2k2 2k3 k4 ) 6 1 Yt t (k1 2k2 2k3 k4 ) 6 true value weighted slope estimated value t = 0.5 Conclusions • 4th order Runge-Kutta offers substantial improvement over Euler. • Both techniques provide estimates, not “true” values. • The accuracy of the estimate depends on the size of the step used in the algorithm. Analytical Runge-Kutta Euler