Rigid Bodies Simulation Homework

• Build a particle system based either on F=ma or procedural simulation – Examples:Thursday Smoke, Fire, Water, Wind, Leaves, Cloth, Magnets, Flocks, Fish, Insects, Crowds, etc. • SimulateSimulation a Rigid Body & – Examples: Angry birds, Bodies tumbling, bouncing, moving around in a room and colliding, Explosions & Fracture, Drop theUnity camera, Etc… Rigid Body as “Particle Chunks”

A small chunk of a rigid body

Consider a rigid body It can be broken up into chunks (or elements), and each chunk can be treated as a single particle if it is small enough Center of

• A body composed of 푛 particle “chunks” with 푚푖 has total mass 푛

푀 = ෍ 푚푖 푖=1

• If the particles have positions 푥Ԧ푖, the is

σ푛 푚 푥Ԧ 푥ҧ = 푖=1 푖 푖 푀

• The center of mass for a rigid body has 푥ҧ and translational 푣ҧ similar to a single particle • When we refer to the position and velocity of a rigid body, we are referring to the position and translational velocity of its center of mass • The center of mass obeys the same ODEs for position and velocity as a particle does, but using the TOTAL mass and the NET Orientation • A rigid body can rotate or change its orientation while its center of mass is stationary • Different ways to keep track of the 푅: – 3x3 Matrix, 3 , 1 • Place a at the center of mass in object • The rotation 푅 rotates the rigid body (and the object space coordinate system) into its world space orientation • Recall: the columns of 푅 are the three object space axes in their world space orientations Combining Position and Orientation • The rigid body has an intrinsic coordinate system in its object space, with its center of mass at the origin • It’s put into world space with a and a rotation

푥ҧ t

푥푤 푥푏 Combining Position and Orientation • The translation of the origin of the object space coordinate system is given by the position of the center of mass 푥ҧ • The world space orientation of the object space coordinate system is given by 푅 – Assume 푅 is a matrix, equivalently expressed as a unit quaternion • A 푥표 in object space has a world space location 푥푤 = 푥ҧ + 푅푥표 – Notice that the center of mass (at the origin) maps to 푥ҧ – Notice that (1,0,0), (0,1,0), (0,0,1) are all rotated by 푅 before being translated by 푥ҧ Angular Velocity • Both 푥ҧ and 푅 are functions of • The rate of change of the position of the center of mass 푥ҧ with respect to time is the translational velocity of the center of mass 푣ҧ • (From our quaternion discussion…) The orientation of the body is changing as it is rotated about some axis 푛ො emanating from the center of mass • The rate of change of the orientation 푅 is given by the world space angular velocity 휔 – its direction is the axis of rotation, 푛ො – Its magnitude is the of rotation • The pointwise velocity of any point x on the rigid body is given by

푣푝 = 푣ҧ + 휔 × 푥 − 푥ҧ = 푣ҧ + 휔 × 푟 where r is the arm and × is the cross-product Aside: Matrix

• Given vectors 푎 = [푎1, 푎2, 푎3] and 푏 = 푏1, 푏2, 푏3 , their cross product 푎 × 푏 can be written as matrix multiplication 푎∗푏 by converting 푎 to a cross product matrix

0 −푎3 푎2 ∗ • 푎 = 푎3 0 −푎1 −푎2 푎1 0 • 푎∗푇 = −푎∗ • 푎∗푏 = −푏∗푎

• Using this new notation, the pointwise velocity of any point x on the object is then given by: ∗ 푣푝 = 푣ҧ + 휔 푟 Linear and Angular Velocity

푣ҧ 푥ҧ ODE for Orientation • The ODE for orientation (angular position) is given by 푅ሶ = 휔∗푅 • That is, 푅ሶ = 휔 × 푅 where the cross product is applied independently to each of the three columns of 푅 • Writing the 3x3 matrix 휔∗ and using matrix multiplication 휔∗푅 automatically performs these 3 cross products Tensor Inertia Tensor • Linear is defined as the product of the mass the translational velocity – Mass is something that resists change in velocity • 퐿 is defined as an “angular mass” times the angular velocity 휔 • The “angular mass” is called the (or inertia tensor) 퐼 of the rigid body • If you spin in your chair while extending your legs, and then suddenly pull your legs closer the chair spins faster – 퐼 reduces when you pull your legs closer. – Hence 휔 has to increase to keep angular momentum 퐿 = 퐼휔 constant Inertia Tensor • For a system of n particles, the angular momentum is 푤 ∗ ∗ ∗ ∗ 퐿 = ෍(푥푖 ) 푚푖푣푖 = ෍ 푥ҧ + 푟푖 푚푖푣푖 = 푥ҧ 푀푣ҧ + ෍ 푟푖 푚푖푣푖 푖 푖 푖 푡ℎ – where 푟푖 is the moment arm of the 푖 particle ∗ • 푣푖 can be written as 푣ҧ + 휔 푟푖 so ∗ ∗ ∗ ∗ 퐿 = 푥ҧ 푀푣ҧ + ෍ 푟푖 푚푖푣ҧ + ෍ 푟푖 푚푖휔 푟푖 푖 푖 ∗ ∗ 푇 • σ푖 푟푖 푚푖푣ҧ = (푣ҧ ) σ푖 푚푖푟푖 = 0 so ∗ ∗ 푇 ∗ ∗ 퐿 = 푥ҧ 푀푣ҧ + ෍ 푚푖(푟푖 ) 푟푖 휔 = 푥ҧ 푀푣ҧ + 퐼휔 푖 ∗ 푇 ∗ – where 퐼 = σ푖 푚푖(푟푖 ) 푟푖 Object Space Inertia Tensor • We can pre-compute the object space inertia tensor as a 3x3 matrix 퐼표 • Then, the world space inertia tensor is given by the 3x3 matrix 퐼 = 푅퐼표푅푇

• One can compute the SVD of the symmetric 3x3 matrix 퐼표 to obtain 퐼표 = 푈퐷푈푇 where 퐷 is a 3x3 diagonal matrix of 3 singular values • Then rotating the object space rest state of the rigid body by 푈−1 gives a new object space inertia tensor of 푈−1퐼표푈−푇 = 푈−1푈퐷푈푇푈−푇 = 퐷 • That is, properly orienting the rest pose of a rigid body in object space gives a diagonal object space inertia tensor 퐼표 and Forces and Torques • Newton’s second law for angular quantities • A force 퐹 changes both the linear momentum 푀푣ҧ and the rotational (angular) momentum 퐿 • The change in linear momentum is independent of the point on the rigid body 푥 where the force is applied • The change in angular momentum does depend on the point 푥 where the force is applied • The is defined as 휏 = 푥 − 푥ҧ × 퐹 = 푟 × 퐹 • The net change in angular momentum is given by the sum of all the external torques 퐿ሶ = 휏 ODEs Rigid Body: Equations of

• State vector for a rigid body 푥ҧ 푅 푋 = 푀푣ҧ 퐿 • 푥ҧ 푣ҧ 푑 푑 푅 휔∗푅 푋 = = 푑푡 푑푡 푀푣ҧ 퐹 퐿 휏 Rigid Body: Equations of Motion

• State vector for a rigid body 푥ҧ 푅 푋 = 푣ҧ 퐿 • Equations of motion

푥ҧ 푣ҧ Equations of 푑 푑 푅 휔∗푅 motion for a 푋 = = particle at the 푑푡 푑푡 푣ҧ 퐹/푀 center of mass 퐿 휏 Forward Euler Update

푣ҧ 푛 휔∗푅 푋푛+1 = 푋푛 + Δ푡 퐹/푀 휏

• Newmark for better accuracy and stability, etc… • Better results are obtained on the second equation by rotating the columns of 푅 directly using the vector Δ푡휔 • Need to periodically re-orthonormalize the columns of 푅 to keep it a Rigid Body: Equations of Motion • State vector for a rigid body 푥ҧ 푞 푋 = 푣ҧ 퐿 • Equations of motion 푣ҧ 푥ҧ 1 푑 푑 푞 휔∗푞 푋 = = 2 푑푡 푑푡 푣ҧ 퐿 퐹/푀 휏 • Once again, preferable to rotate q by Δ푡휔 • Renormalize q using a square root Question #1

LONG FORM: • Summarize rigid body simulation. • Identify 10 rigid bodies in a typical room.

SHORT FORM: • Identify 10 rigid bodies in this room. Geometry Rigid Body Modeling • Store an object space triangulated surface to represent the surface of the rigid body • Store an object space implicit surface to represent the interior volume of the rigid body • Collision detection between two rigid bodies can then be carried out by checking the surface of one body against the interior volume of another • Implicit surfaces can be used to model the interior volume of kinematic and static objects as well • Implicit surface representations of interior volumes can also be used for collisions with particles and particle systems Recall: Implicit Surfaces

• Implicit surfaces represent a surface with a function 휙(푥) defined over the whole 3D space • The inside region Ω−, the outside region is Ω+, and the surface 휕Ω are all defined by the function 휙(푥)

• 휙 푥 < 0 inside

• 휙 푥 > 0 outside

• 휙 푥 = 0 surface

• Easy to check if a point is inside an object • Efficient to make changes to an object • Efficient boolean operations: Union, Difference, Intersection Analytic Implicit Surfaces

• For simple functions, write down the function and analytically evaluate 휙(푥) to see if 휙 푥 < 0 and thus 푥 is inside the object • 2D circle • 3D Discrete Implicit Surfaces • Lay down a grid that spans the space you are trying to represent, e.g. a padded bounding box • Store values of the function at grid points • Then for arbitrary locations in space, interpolate from the nearby values on the grid to see if 휙 푥 < 0 • use trilinear interpolation, like 3D textures • Signed Functions are implicit surfaces where the magnitude of the function gives the distance to the closet point on the surface Constructing Signed Distance Functions • Start with a triangulated surface • Place a grid inside a slightly padded bounding box of the object – This grid will contain point samples for the signed distance function – The resolution of the grid is based on heuristics • Place a sphere at every grid point and find all intersecting triangles – the radius of the sphere only needs to be a few grid cells wide, because we only care about grid points near the triangulated surface – If the sphere does not intersect any triangles, then the grid point is not near the triangulated surface – (An structure is useful, e.g. bounding box hierarchy) • For each nearby triangle, find the closest point on that triangle – see this document for details • Take the minimum of all such as the magnitude of 휙 • Could initialize all grid points this way, but it is expensive for points farther from the surface where one has to check many more (potentially all) triangles Fast Marching Method

• Similar to Dijkstra's algorithm • Walk outwards from the previously initialized points to fill the rest of the domain Initialization • Mark all the previously computed points nearby the triangulated surface as Black • Mark the rest of the points White Iteration • White points adjacent to Black points are re-labeled as Red • Estimate the distance value for all Red points using only their Black neighbors – by solving a quadratic equation for distance • The Red point with the smallest distance value is found and labeled Black – a heap data structure is ideal, and then the fast marching method runs in 풪 푁푙표푔푁 where 푁 is the number of grid points • Labeling this point Black turns some White points into Red – and also changes the value of some of the previously computed Red points, since there is a new Black point to use in their distance computation Sign of 휙

• Perform a flood fill on the grid – Start from a random grid point – Put it on a stack; mark it as 0 – Pop it off; put its connected non- occluded neighbors on the stack; mark them as 0; repeat – When there are no more cells on the stack, find a random uncolored cell, mark it as 1, and repeat • The region (0 or 1) that touches the grid boundary is marked as outside • The other region is marked as inside • Could have more than two regions in some cases Question #2

LONG FORM: • Summarize rigid body geometric modeling. • Answer short form question below

SHORT FORM: • Give an example of a rigid body with an interesting shape that could be used in a game. How would it be used? Collisions Collision Detection • Test all the triangulated surface points of one body against the implicit surface volume of the other (and vice versa) – A world space point is put into object space to check against an implicit surface using 푥표 = 푅−1(푥푤 − 푥ҧ) • Partial are used to compute the normal 푑휙 푑휙 푑휙 푛 푥 , 푦 , 푧 = , , อ 0 0 0 푑푥 푑푦 푑푧 푥0,푦0,푧0 푛 푥0, 푦0, 푧0 푁표푟푚푎푙 푥0, 푦0, 푧0 = 푛 푥0, 푦0, 푧0 • Note: A particle can be moved to the surface of the implicit surface by tracing a ray in the normal direction and looking for the intersection with the 휙 = 0 isocontour (see CS148) Rigid Body Collisions

푢1 푗1

푢2 푗2

Collision detection Compute the The final relative After evolving the initial is calculated bodies in time, they velocity using the coefficient eventually separate of restitution.

Calculate and apply the collision Collision Response Equations for applying an impulse to one body with collision location 푟푝 with respect to its center of mass: • 푀푣ҧ푛푒푤 = 푀푣ҧ + 푗 푛푒푤 ∗ • 퐼휔 = 퐼휔 + 푟푝 푗 (note 퐼 doesn’t change) • And then, in terms of the pointwise velocity… 푛푒푤 푛푒푤 푛푒푤∗ 푛푒푤 ∗푇 푛푒푤 • 푢푝 = 푣ҧ + 휔 푟푝 = 푣ҧ + 푟푝 휔 푗 • 푢푛푒푤 = 푣ҧ + + 푟∗푇 휔 + 퐼−1푟∗푗 푝 푀 푝 푝 1 • 푢푛푒푤 = 푢 + 퐼 + 푟∗푇퐼−1푟∗ 푗 = 푢 + 퐾푗 푝 푝 푀 3푥3 푝 푝 푝 • Infinite mass kinematic/static objects (e.g. ground plane) are treated by setting the impulse factor 퐾 = 0 Collision Response • Equal and opposite impulse applied to each body: 푛푒푤 푛푒푤 푢1 = 푢1 + 퐾1푗 and 푢2 = 푢2 − 퐾2푗

• Calculate the relative velocity 푢푟푒푙 = 푢1 − 푢2 at the point of collision

– Relative normal velocity is 푢푟푒푙,푁 = 푢푟푒푙 ⋅ 푁 – Only collide when 푢푟푒푙,푁 < 0, i.e. bodies not already separating • Define a total impulse factor 퐾푇 = 퐾1 + 퐾2, then 푛푒푤 푢푟푒푙 = 푢푟푒푙 + 퐾푇푗 푛푒푤 푇 푢푟푒푙,푁 = 푢푟푒푙,푁 + 푁 퐾푇푗 • Since the collision impulse should be in the normal direction, we can write 푗 = 푁푗푛, hence 푛푒푤 푇 푢푟푒푙,푁 = 푢푟푒푙,푁 + 푁 퐾푇푁푗푛 푛푒푤 • Given 푢푟푒푙,푁 = −푐푅푢푟푒푙,푁, we solve for 푗푛 and apply 푗 = 푁푗푛

• Relative tangential velocity is 푢푟푒푙,푇 = 푢푟푒푙 − 푢푟푒푙,푁푁 푛푒푤 • First assume static friction, i.e. 푢푟푒푙,푇 = 0, so that 푛푒푤 푢푟푒푙 = −푐푅푢푟푒푙,푁푁 푛푒푤 • Solve for a full 3D impulse 푗 using 푢푟푒푙 = 푢푟푒푙 + 퐾푇푗, by inverting the 3x3 matrix 퐾푇 • If this impulse is in the friction cone, i.e. if 푗 − 푗 ⋅ 푁 푁 ≤ 휇푠 푗 ⋅ 푁 , then the assumption of sticking due to static friction was correct

• Otherwise we start over using kinetic friction instead (휇푘≤ 휇푠) 푢 – With tangential direction 푇 = 푟푒푙,푇 , the kinetic friction impulse 푢푟푒푙,푇 is 푗 = 푗푛푁 − 휇푘푗푛푇 푇 – And we can solve −푐푅푢푟푒푙,푁= 푢푟푒푙,푁 + 푁 퐾푇 푁 − 휇푘푇 푗푛 to find 푗푛 before applying 푗 = 푁 − 휇푘푇 푗푛 Question #3

LONG FORM: • Summarize rigid body collision handling. • Answer short form question below.

SHORT FORM: • Give an example of using collisions between rigid bodies for a game. Fracture Fracture Fracture • Suppose a rigid body fractures into n pieces with masses 푚1, , 푚2 … 푚푛, 푣1, 푣2 … . 푣푛, inertia tensors 퐼1, 퐼2, … 퐼푛 and angular velocities 휔1, 휔2, … 휔푛 • The mass and inertia tensor of each new piece can be computed based on the geometry • What can we say about the fractured pieces? – M푣 = σ 푚푖푣푖 ∗ – 퐼휔 = σ(푟푖 푚푖푣푖 + 퐼푖휔푖) • To ensure this: – Assign each rigid body the velocity its newly created center of mass had before fracturing i.e. 푣푖 = 푣 + 휔 × 푟푖 • where 푟푖 points from the center of mass of the original rigid body to the center of mass of the i-th child – Angular momentum is then conserved by setting 휔푖 = 휔