CGP Lecture 9
Total Page:16
File Type:pdf, Size:1020Kb
CGP 9 Dr Wojciech Palubicki Where we were at… State Broad Narrow Collision Update Phase Phase Response Today… State Broad Narrow Collision Update Phase Phase Response Basic Idea • Limit motion of particles/ rigid bodies by introducing constraints. Constraint Types • Contact and Friction Constraint Types • Distance: Cloth Constraint Types • Joint angle constraints of ragdolls Collision Response • Solving physical constraints in addition to the equations of motion • Constraints are mostly contact constraints, but also – Friction constraints – Distance constraints – Joint angle constraints – ... • The ideal collision response system deals with all of these Naive Take • Apply the constraints to the objects in pairs • Use the laws of conservation of motion for each collision; P0 is the - point of collision, xA and xB are the centers of mass of the objects, v is the pre-impact velocity of the objects, v+ is the post-impact velocity of the objects − − − − − 1+휀 푁0∙ 푣퐴 −푣퐵 +(휔퐴∙ 푟퐴×푁0 −휔퐵∙(푟퐵 × 푁0)) • 푓 = 푇 − 푇 − 1 푚퐴+1 푚퐵+ 푟퐴×푁0 퐼퐴 푟퐴×푁0 + 푟퐵×푁0 퐼퐴 (푟퐵 × 푁0) • 푟퐴 = 푃0 − 푥퐴, 푟퐵 = 푃0 – 푥퐵 + − • 푣퐴 = 푣퐴 + 푓푁0/푚퐴 + − − • 휔퐴 = 휔퐴 + + 퐼퐴 (푟퐴 × (푓푁0)) • Push away from interpenetration as long as interpenetration exists Naive Take • Apply the constraints to the objects in pairs • Use the laws of conservation of motion for each collision; P0 is the - point of collision, xA and xB are the centers of mass of the objects, v is the pre-impact velocity of the objects, v+ is the post-impact velocity of the objects − − − − − 1+휀 푁0∙ 푣퐴 −푣퐵 +(휔퐴∙ 푟퐴×푁0 −휔퐵∙(푟퐵 × 푁0)) • 푓 = 푇 − 푇 − 1 푚퐴+1 푚퐵+ 푟퐴×푁0 퐼퐴 푟퐴×푁0 + 푟퐵×푁0 퐼퐴 (푟퐵 × 푁0) • 푟퐴 = 푃0 − 푥퐴, 푟퐵 = 푃0 – 푥퐵 + − • 푣퐴 = 푣퐴 + 푓푁0/푚퐴 + − − • 휔퐴 = 휔퐴 + + 퐼퐴 (푟퐴 × (푓푁0)) • Push away from interpenetration as long as interpenetration exists Naive Take • Jitters a lot, and does not support stacking • May be acceptable in very sparse scenarios (space/flight simulator) Naive Take 2 • Apply the constraints to the objects in pairs • Apply again during the same tick • Average/combine the various impulses • Push apart objects so they do not penetrate Naive Take 2 • Apply the constraints to the objects in pairs • Apply again during the same tick • Average/combine the various impulses • Push apart objects so they do not penetrate • Constraints are still broken • Slow Unconstrained Kinematics • A rigid body is characterized by • 푥 = 푣 • 푞 = 1/2휔푞 Unconstrained Kinematics • For a system of N bodies, we can define the system derivative as 푣1 휔1 푉 = ⋮ 푣푛 휔푛 Constraints • Our system allows pairwise constraints between bodies • The k-th constraint, between bodies i and j , has the form 퐶푘 (푥푖, 푞푖, 푥푗, 푞푗) = 0 • The vector C holds all the constraints. 퐶 = 0, or 퐶(푥) = 0, is a function of the state vector, so by the chain rule 퐶 = 퐽푉 = 0 Constraint forces • Each constraint causes a reaction force 푓푐 and a reaction torque τ푐 • The vector of all reaction forces is 푓 푐1 휏 푐1 퐹푐 = ⋮ 푓 푐푛 휏 푐푛 Constraint forces We know that 퐶 = 퐽푉 = 0 퐽1 ∙ 푉 • 퐽푉 = ⋮ = 0 퐽푚 ∙ 푉 • This means that 푉 is orthogonal to each row of 퐽 Constraint forces We know that 퐶 = 퐽푉 = 0 퐽1 ∙ 푉 • 퐽푉 = ⋮ = 0 퐽푚 ∙ 푉 • This means that 푉 is orthogonal to each row of 퐽 Constraint forces • Constraint forces perform no work, so 퐹푐 ∙ 푉 = 0 Contact Constraint velocity Contact Constraint velocity F Contact Constraint adjusted velocity velocity 퐹푐 ∙ 푉 = 0 F Distance Constraint Distance Constraint velocity velocity F1 F2 velocity F1 blend forces: 퐹1휆1 + 퐹2휆2 F2 Constraint forces 푇 • We can use 퐹푐 = 퐽 휆 for some vector 휆 of undetermined (Lagrangian) force multipliers 푇 퐹푐 ∙ 푉 = 퐽 휆 ∙ 푉 = 퐽푖휆푖 ∙ 푉= 퐽푖 ∙ 푉휆푖 푖 푖 = 0 ∙ 휆푖 = 0 푖 Constraint forces • We compute the matrix 퐽 of constraints from the collision system • We then solve for 휆, compute 퐹푐, and finally obtain 푉 Distance constraints • The simplest constraint is a distance constraint • Two points of two bodies must remain at a given distance 1 • 퐶 (푥 , 푞 , 푥 , 푞 ) = ( p − p 2 − 퐿2) = 0 푖 푖 푗 푗 2 j i • If we derive this, we get (푑 = 푝푗 − 푝푖 ) 퐶(푥푖, 푞푖, 푥푗, 푞푗) = 푑 ∙ (푣푗 + 휔푗 × 푟푗 − 푣푖 − 휔푖 × 푟푖 ) • We split this into a rowa rowfor of 퐽 J and a partsome columns of 푉: of V 푇 푇 푇 푇 퐶 푥푖, 푞푖, 푥푗, 푞푗 = −푑 − 푟푖 × 푑 푑 푟푗 × 푑 푇 푣푖휔푖푣푗휔푗 Distance constraints • Note that a row of J also contains many zeroes (6 × (푁푏표푑푖푒푠 − 2)) • The only columns that are not zeroed are those corresponding to the bodies i and j • 퐶 푥푖, 푞푖, 푥푗, 푞푗 = 푇 푇 0 ⋯ 0 푇 푇 ⋯ 0 − 푑 − 푟푖 × 푑 푑 푟푗 × 푑 ⋯ 0 푉 a row of J Contact constraints • The contact constraint measures the object separation; it is negative in case of overlap • 퐶 푥푖, 푞푖, 푥푗, 푞푗 = (푥푗 + 푟푗 − 푥푖 − 푟푖 ) ∙ 푛푖 = 0 • 퐶 푥푖, 푞푖, 푥푗, 푞푗 = (푣푗 + 휔푗 × 푟푗 − 푣푖 − 휔푖 × 푟푖 ) ∙ 푛푖 + (푥푗 + 푟푗 − 푥푖 − 푟푖 ) ∙ 휔푖 × 푛푖 • We assume that both penetration and angular velocity are small, so we ignore the second term • 퐶 푥푖, 푞푖, 푥푗, 푞푗 ≈ (푣푗 + 휔푗 × 푟푗 − 푣푖 − 휔푖 × 푟푖) ∙ 푛푖 Contact constraints • We can now separate 퐶 into 퐽 and 푉: • 퐶 푥푖, 푞푖, 푥푗, 푞푗 = 푣푗 + 휔푗 × 푟푗 − 푣푖 − 휔푖 × 푟푖 ∙ 푛푖 = 푇 푇 푇 푇 푇 −푛 − 푟 × 푛 푛 푟 × 푛 푣 휔 푣 휔 푖 푖 푖 푖 푗 푖 푖 푖 푗 푗 Contact constraints • Notice that the force between bodies in contact can push them apart, but not pull them together • This means that 0 ≤ 휆푘 ≤ +∞, where 푘 is the constraint index for a contact constraint Contact constraints • Due to numerical errors or issues with discrete steps penetration might happen anyway • We allow the velocity to be modulated by a pushing factor which is proportional to the penetration • This means that for contact constraints 1 퐽 푉 = − 훽퐶 , for 훽 ≤ 푖 푖 ∆푡 Friction constraints • Friction constraints are very similar to contact constraints • Friction happens along the tangent plane, so we have two constraints (one for ui = T and one for uj = B) • 퐶 푥푖, 푞푖, 푥푗, 푞푗 = 푣푗 + 휔푗 × 푟푗 − 푣푖 − 휔푖 × 푟푖 ∙ 푢푖 = 푇 푇 푇 푇 푇 −푢 − 푟 × 푢 푢 푟 × 푢 푣 휔 푣 휔 푖 푖 푖 푖 푗 푖 푖 푖 푗 푗 • 퐶 푥푖, 푞푖, 푥푗, 푞푗 = 푣푗 + 휔푗 × 푟푗 − 푣푖 − 휔푖 × 푟푖 ∙ 푢푗 = 푇 − 푇 푇 푇 푇 −푢푗 푟푖 × 푢푗 푢푗 푟푗 × 푢푗 푣푖 휔푖 푣푗 휔푗 Friction constraints • We must also bound the friction value (this is an approximation) to take the friction coefficient into account • −휇푚푐푔 ≤ 휆푢1 ≤ 휇푚푐푔 and −휇푚푐푔 ≤ 휆푢2 ≤ 휇푚푐푔, where 푚푐 is the mass assigned to the contact point Equations of motion • We now integrate our constraint system with the equations of motion • We know the Newton - Euler equations of motion are • 푚푣 = 퐹 = 푓푐 + 푓푒푥푡 • 퐼휔 = 휏 = 휏푐 + 휏푒푥푡 Equations of motion • We can define a single matrix for all the bodies 푚1퐸3푥3 0 ⋯ 0 0 0 ⋯ 0 0 퐼1 푀 = ⋮ ⋮ ⋱ ⋮ ⋮ ⋯ 0 0 푚푛퐸3푥3 0 ⋯ 0 0 0 퐼푛 • 퐸3푥3 is the identity matrix Equations of motion • We can easily invert this matrix (푚 퐸 −1 ⋯ 0 0 1 3푥3) 0 −1⋯ 0 0 0 퐼1 푀−1 = ⋮ ⋮ ⋱ ⋮ ⋮ ⋯ 0 −1 0 (푚푛퐸3푥3 0 ⋯ 0 ) −1 0 0 퐼푛 Equations of motion • We define a single vector for all the external forces 푓 푒푥푡1 휏 푒푥푡1 • 퐹푒푥푡 = ⋮ 푓 푒푥푡푛 휏 푒푥푡푛 Equations of motion 푇 • Since we know that 퐹퐶 = 퐽 휆, we can rewrite the equations of motion for 푛 bodies as 푀푉 = 퐽푇휆 + 퐹 푒푥푡 퐽푉 = 휀 • 휀 is the vector of force offsets which allows contact forces to perform work • We have too many unknowns: 푉, 푉 , and 휆 Equations of motion 푇 • Since we know that 퐹퐶 = 퐽 휆, we can rewrite the equations of motion for 푛 bodies as 푀푉 = 퐽푇휆 + 퐹 푒푥푡 퐽푉 = 휀 • 휀 is the vector of force offsets which allows contact forces to perform work • We have too many unknowns: 푉, 푉 , and 휆 Equations of motion V −V • We approximate 푉 ≈ 2 1 ∆푡 V −V 푀 2 1 = 퐽푇휆 + 퐹푒 • We replace 푉 ∆푡 푥푡 퐽푉2 = 휀 • We solve for −1 푇 푉2 = ∆푡푀 퐽 휆 + 퐹푒푥푡 + 푉1 푉2 푇 푉2 = 퐽 휀 Equations of motion V −V • We approximate 푉 ≈ 2 1 ∆푡 V −V 푀 2 1 = 퐽푇휆 + 퐹푒 • We replace 푉 ∆푡 푥푡 퐽푉2 = 휀 • We solve for −1 푇 푉2 = ∆푡푀 퐽 휆 + 퐹푒푥푡 + 푉1 푉2 푇 푉2 = 퐽 휀 Equations of motion V −V • We approximate 푉 ≈ 2 1 ∆푡 V −V 푀 2 1 = 퐽푇휆 + 퐹푒 • We replace 푉 ∆푡 푥푡 퐽푉2 = 휀 • We solve for −1 푇 푉2 = ∆푡푀 퐽 휆 + 퐹푒푥푡 + 푉1 푉2 푇 푉2 = 퐽 휀 Equations of motion • We can now finish solving for 휆 푇 −1 푇 • 퐽 휀 = ∆푡푀 퐽 휆 + 퐹푒푥푡 + 푉1 푇 −1 −1 푇 • 퐽 휀 − 푉1 − ∆푡푀 퐹푒푥푡 = ∆푡푀 (퐽 휆) −1 −1 푇 • 휀 − 퐽푉1 − ∆푡퐽푀 퐹푒푥푡 = ∆푡퐽푀 퐽 휆 Equations of motion • Once 휆 is computed, we can determine 퐹푐 , 퐹, and then 푉2 • A regular integration step is then performed with the new velocities 푉2 Iterative Solution • The equation can be restated in simpler form: 휀 퐽푉1 −1 −1 푇 • − − 퐽푀 퐹 = 퐽푀 퐽 휆 ∆푡 ∆푡 푒푥푡 b A x • 퐴푥 = 푏 for some 퐴, 푏 • These systems can be solved iteratively with a method such as Projected Gauss-Seidel (PGS) Projected Gauss-Seidel • while not converged – ∆푥푖 = (푏푖 − 푗 푎푖푗푥푗 )/퐴푖푖 – 푥 = clamp(푥 + ∆푥 , 푚푖푛 , 푚푎푥 ) i 푖 푖 푖 푖 Iterative Solution • A is going to be very sparse – You may optimize the summation ∆푥푖 = (푏푖 − 푗 푎푖푗푥푗 )/퐴푖푖 a lot by ignoring the zero entries of A Contact caching (warm start) • PGS is faster the closer the initial x vector is to the solution • If we store previous contact points and their 휆i values, PGS converges sooner State Broad Narrow Collision Update Phase Phase Response Physics Libraries • PhysX, Box2D, Chipmunk Physics and Bullet Physics .