AnimationCS 551 / 651
Lecture 6Lecture 6
Rigid-body SimulationRigid-body Simulation
Lecture 6Lecture 6
Rigid-body SimulationRigid-body Simulation
Baraff, 1991
Assignment 1Rigid-body Simulator
Goal: To demonstrate understanding of rigid-body Goal: To demonstrate understanding of rigid-body dynamics by writing a simulatordynamics by writing a simulator
• 3ds file loader 3ds file loader (given)(given)
• Compute COM and MOI Compute COM and MOI (given)(given)
• User-selected force vector (how?)User-selected force vector (how?)
• Compute location of impact (ray / polygon intersection)Compute location of impact (ray / polygon intersection)
• Compute change in linear and angular accelerations (Hecker)Compute change in linear and angular accelerations (Hecker)
• Animate…Animate…
Goal: To demonstrate understanding of rigid-body Goal: To demonstrate understanding of rigid-body dynamics by writing a simulatordynamics by writing a simulator
• 3ds file loader 3ds file loader (given)(given)
• Compute COM and MOI Compute COM and MOI (given)(given)
• User-selected force vector (how?)User-selected force vector (how?)
• Compute location of impact (ray / polygon intersection)Compute location of impact (ray / polygon intersection)
• Compute change in linear and angular accelerations (Hecker)Compute change in linear and angular accelerations (Hecker)
• Animate…Animate…
Rigid-body Simulator
Not permitted resourcesNot permitted resources
• Physical simulation source codePhysical simulation source code
– Not from web, books, bathroom wall…Not from web, books, bathroom wall…
Permitted resourcesPermitted resources
• All books (physics, graphics, C++, game design, …)All books (physics, graphics, C++, game design, …)
• Graphics interface librariesGraphics interface libraries
• File loader and MOI librariesFile loader and MOI libraries
Not permitted resourcesNot permitted resources
• Physical simulation source codePhysical simulation source code
– Not from web, books, bathroom wall…Not from web, books, bathroom wall…
Permitted resourcesPermitted resources
• All books (physics, graphics, C++, game design, …)All books (physics, graphics, C++, game design, …)
• Graphics interface librariesGraphics interface libraries
• File loader and MOI librariesFile loader and MOI libraries
Rigid-body Simulator
DeliverablesDeliverables
• Load any legal (closed) 3ds modelLoad any legal (closed) 3ds model
DeliverablesDeliverables
• Load any legal (closed) 3ds modelLoad any legal (closed) 3ds model
an aside…an aside…an aside…an aside…
Closed objects
AKA “solid”AKA “solid”
More rigorously: More rigorously: closed, orientable manifoldsclosed, orientable manifolds
• Local neighborhood of all points isomorphic to discLocal neighborhood of all points isomorphic to disc
• Boundary partitions space into interior & exteriorBoundary partitions space into interior & exterior
AKA “solid”AKA “solid”
More rigorously: More rigorously: closed, orientable manifoldsclosed, orientable manifolds
• Local neighborhood of all points isomorphic to discLocal neighborhood of all points isomorphic to disc
• Boundary partitions space into interior & exteriorBoundary partitions space into interior & exterior
Yes No
Manifold
Examples of Examples of manifoldmanifold objects: objects:
• SphereSphere
• TorusTorus
• Well-formedWell-formedCAD partCAD part
Examples of Examples of manifoldmanifold objects: objects:
• SphereSphere
• TorusTorus
• Well-formedWell-formedCAD partCAD part
Back-Face Culling
Examples of non-manifold objects:Examples of non-manifold objects:
• A single polygonA single polygon
• A terrain or height fieldA terrain or height field
• polyhedron w/ missing facepolyhedron w/ missing face
• Anything with cracks or holes in Anything with cracks or holes in boundaryboundary
• one-polygon thick lampshadeone-polygon thick lampshade
Examples of non-manifold objects:Examples of non-manifold objects:
• A single polygonA single polygon
• A terrain or height fieldA terrain or height field
• polyhedron w/ missing facepolyhedron w/ missing face
• Anything with cracks or holes in Anything with cracks or holes in boundaryboundary
• one-polygon thick lampshadeone-polygon thick lampshade
Rigid-body Simulator
Deliverables (basic)Deliverables (basic)
• Load any legal (closed) 3ds modelLoad any legal (closed) 3ds model
• Define an arbitrary force vectorDefine an arbitrary force vector
• Animate the model as is movesAnimate the model as is moves
Deliverables (basic)Deliverables (basic)
• Load any legal (closed) 3ds modelLoad any legal (closed) 3ds model
• Define an arbitrary force vectorDefine an arbitrary force vector
• Animate the model as is movesAnimate the model as is moves
Rigid-body Simulator
Deliverables (intermediate)Deliverables (intermediate)• Add gravity and drop object to the ground. Use Add gravity and drop object to the ground. Use
constraint forces to make it rest thereconstraint forces to make it rest there
• Weld a spring to a vertex of the model and suspend Weld a spring to a vertex of the model and suspend model by spring from ceilingmodel by spring from ceiling
– Why springs?Why springs? Because it is much more Because it is much more complicated to force the model to hang from a complicated to force the model to hang from a string (or bar) of fixed length… more constraints to string (or bar) of fixed length… more constraints to worry aboutworry about
Deliverables (intermediate)Deliverables (intermediate)• Add gravity and drop object to the ground. Use Add gravity and drop object to the ground. Use
constraint forces to make it rest thereconstraint forces to make it rest there
• Weld a spring to a vertex of the model and suspend Weld a spring to a vertex of the model and suspend model by spring from ceilingmodel by spring from ceiling
– Why springs?Why springs? Because it is much more Because it is much more complicated to force the model to hang from a complicated to force the model to hang from a string (or bar) of fixed length… more constraints to string (or bar) of fixed length… more constraints to worry aboutworry about
Rigid-body Simulator
Deliverables (advanced)Deliverables (advanced)
• Add more objects and springsAdd more objects and springs
– Newton’s CradleNewton’s Cradle
– Join two objects with springsJoin two objects with springs
– Suspend one object by spring and Suspend one object by spring and drop other objects on itdrop other objects on it
• Implement constrained dynamicsImplement constrained dynamics
– Replace springs with rodsReplace springs with rods
Deliverables (advanced)Deliverables (advanced)
• Add more objects and springsAdd more objects and springs
– Newton’s CradleNewton’s Cradle
– Join two objects with springsJoin two objects with springs
– Suspend one object by spring and Suspend one object by spring and drop other objects on itdrop other objects on it
• Implement constrained dynamicsImplement constrained dynamics
– Replace springs with rodsReplace springs with rods
Rigid-body Simulator
Grading will take place during a demo you’ll Grading will take place during a demo you’ll provide for the TAprovide for the TA
• Time TBA, but as early as 6:15 Wed, Sep 19Time TBA, but as early as 6:15 Wed, Sep 19 thth
• Either bring your machine to himEither bring your machine to him
• Or make sure it works on Windows machine with Or make sure it works on Windows machine with VisualStudio .netVisualStudio .net
– Machines in Stacks and Small Hall should be set Machines in Stacks and Small Hall should be set up this wayup this way
Grading will take place during a demo you’ll Grading will take place during a demo you’ll provide for the TAprovide for the TA
• Time TBA, but as early as 6:15 Wed, Sep 19Time TBA, but as early as 6:15 Wed, Sep 19 thth
• Either bring your machine to himEither bring your machine to him
• Or make sure it works on Windows machine with Or make sure it works on Windows machine with VisualStudio .netVisualStudio .net
– Machines in Stacks and Small Hall should be set Machines in Stacks and Small Hall should be set up this wayup this way
Reading for Monday
Prepare three questions for each paperPrepare three questions for each paper
Timewarp Rigid Body Simulation, B. Mirtich, Timewarp Rigid Body Simulation, B. Mirtich, SIGGRAPH 2000. SIGGRAPH 2000.
Stephen Chenney and D.A.Forsyth, "Sampling Stephen Chenney and D.A.Forsyth, "Sampling Plausible Solutions to Multi-Body Constraint Plausible Solutions to Multi-Body Constraint Problems". SIGGRAPH 2000 Conference Problems". SIGGRAPH 2000 Conference Proceedings, pages 219-228, July 2000. Proceedings, pages 219-228, July 2000.
Prepare three questions for each paperPrepare three questions for each paper
Timewarp Rigid Body Simulation, B. Mirtich, Timewarp Rigid Body Simulation, B. Mirtich, SIGGRAPH 2000. SIGGRAPH 2000.
Stephen Chenney and D.A.Forsyth, "Sampling Stephen Chenney and D.A.Forsyth, "Sampling Plausible Solutions to Multi-Body Constraint Plausible Solutions to Multi-Body Constraint Problems". SIGGRAPH 2000 Conference Problems". SIGGRAPH 2000 Conference Proceedings, pages 219-228, July 2000. Proceedings, pages 219-228, July 2000.
We’re in 2D… Collisions are:We’re in 2D… Collisions are:• Vertex / edgeVertex / edge
• Vertex / vertexVertex / vertex
• Edge / edgeEdge / edge
Compute normal to collisionCompute normal to collision• v/e: perpendicular to edge (pointing towards A by convention)v/e: perpendicular to edge (pointing towards A by convention)
• e/e: perpendicular to edgee/e: perpendicular to edge
• v/v: something reasonable (perhaps use an edge-perp)v/v: something reasonable (perhaps use an edge-perp)
We’re in 2D… Collisions are:We’re in 2D… Collisions are:• Vertex / edgeVertex / edge
• Vertex / vertexVertex / vertex
• Edge / edgeEdge / edge
Compute normal to collisionCompute normal to collision• v/e: perpendicular to edge (pointing towards A by convention)v/e: perpendicular to edge (pointing towards A by convention)
• e/e: perpendicular to edgee/e: perpendicular to edge
• v/v: something reasonable (perhaps use an edge-perp)v/v: something reasonable (perhaps use an edge-perp)
Collisions
Collisions
Compute relative normal velocityCompute relative normal velocity
• ff
– Must be negative for a collision to Must be negative for a collision to take placetake place
– If equal to 0… resting contact If equal to 0… resting contact (special case) (special case)
Compute relative normal velocityCompute relative normal velocity
• ff
– Must be negative for a collision to Must be negative for a collision to take placetake place
– If equal to 0… resting contact If equal to 0… resting contact (special case) (special case)
nvvnv BPAPAB )(
After collision is detected
Consider applying a force to both bodiesConsider applying a force to both bodies
• This is how nature “simulates” collisionsThis is how nature “simulates” collisions
• Already interpenetrating objects will remain in this Already interpenetrating objects will remain in this state for at least one more time stepstate for at least one more time step
– Cannot instantaneously change velocityCannot instantaneously change velocity
– Forces need time to be integrated to accelerations Forces need time to be integrated to accelerations and velocitiesand velocities
Consider applying a force to both bodiesConsider applying a force to both bodies
• This is how nature “simulates” collisionsThis is how nature “simulates” collisions
• Already interpenetrating objects will remain in this Already interpenetrating objects will remain in this state for at least one more time stepstate for at least one more time step
– Cannot instantaneously change velocityCannot instantaneously change velocity
– Forces need time to be integrated to accelerations Forces need time to be integrated to accelerations and velocitiesand velocities
After collision is detected
We need instantaneous change in velocityWe need instantaneous change in velocity
• ImpulseImpulse
– This is a hack that gets us out of the jam we This is a hack that gets us out of the jam we created when assuming impenetrable bodies existcreated when assuming impenetrable bodies exist
– Generalization of subtle surface propertiesGeneralization of subtle surface properties
– Like simulating a large force for a small time stepLike simulating a large force for a small time step
– Will change velocity like we needWill change velocity like we need
We need instantaneous change in velocityWe need instantaneous change in velocity
• ImpulseImpulse
– This is a hack that gets us out of the jam we This is a hack that gets us out of the jam we created when assuming impenetrable bodies existcreated when assuming impenetrable bodies exist
– Generalization of subtle surface propertiesGeneralization of subtle surface properties
– Like simulating a large force for a small time stepLike simulating a large force for a small time step
– Will change velocity like we needWill change velocity like we need
Calculating impulse
Duration of impulse is “no time”Duration of impulse is “no time”• This is a small amount of timeThis is a small amount of time
• All other forces are ignored during this periodAll other forces are ignored during this period
No frictionNo friction
Coefficient of resitutionCoefficient of resitution• Models complicated compression and restitution of Models complicated compression and restitution of
impacting bodiesimpacting bodies
• Models dissipation of energyModels dissipation of energy
Duration of impulse is “no time”Duration of impulse is “no time”• This is a small amount of timeThis is a small amount of time
• All other forces are ignored during this periodAll other forces are ignored during this period
No frictionNo friction
Coefficient of resitutionCoefficient of resitution• Models complicated compression and restitution of Models complicated compression and restitution of
impacting bodiesimpacting bodies
• Models dissipation of energyModels dissipation of energy
Coefficient of restitution
nvnv ABAB
= 1 = 1 superball (perfectly elastic) superball (perfectly elastic)
= 0 = 0 clay (perfectly inelastic) clay (perfectly inelastic)
= 1 = 1 superball (perfectly elastic) superball (perfectly elastic)
= 0 = 0 clay (perfectly inelastic) clay (perfectly inelastic)
Calculating an impulse
Solve for one number, jSolve for one number, j
• Apply Apply jj in direction of in direction of nn to A to A
• Apply Apply jj in direction of in direction of ––nn to B to B
Solve for one number, jSolve for one number, j
• Apply Apply jj in direction of in direction of nn to A to A
• Apply Apply jj in direction of in direction of ––nn to B to Bequal and oppositeequal and oppositeequal and oppositeequal and opposite
Computing Impulse
11stst: Assume objects cannot rotate: Assume objects cannot rotate
22ndnd: Use definition of coeff. of rest. to derive : Use definition of coeff. of rest. to derive a second set of va second set of v++ equations equations
33rdrd: Use substitution to solve for j: Use substitution to solve for j
11stst: Assume objects cannot rotate: Assume objects cannot rotate
22ndnd: Use definition of coeff. of rest. to derive : Use definition of coeff. of rest. to derive a second set of va second set of v++ equations equations
33rdrd: Use substitution to solve for j: Use substitution to solve for j
Computing impulse
Things to note:Things to note:
• n doesn’t have to be normalizedn doesn’t have to be normalized
• A or B can be fixed by setting mass to infinityA or B can be fixed by setting mass to infinity
• If MIf MAA = 1, M = 1, MBB = inf, v = inf, vBB = 0, = 0, =1=1
– Computes reflection of vComputes reflection of vAA about n about n
Things to note:Things to note:
• n doesn’t have to be normalizedn doesn’t have to be normalized
• A or B can be fixed by setting mass to infinityA or B can be fixed by setting mass to infinity
• If MIf MAA = 1, M = 1, MBB = inf, v = inf, vBB = 0, = 0, =1=1
– Computes reflection of vComputes reflection of vAA about n about n
Accounting for rotation
Consider velocity of collision point P after collisionConsider velocity of collision point P after collision
Derived from two equationsDerived from two equations
Consider velocity of collision point P after collisionConsider velocity of collision point P after collision
Derived from two equationsDerived from two equations
Accounting for rotation
Returning to elasticityReturning to elasticity
Start substitutingStart substituting
Solve for jSolve for j
Returning to elasticityReturning to elasticity
Start substitutingStart substituting
Solve for jSolve for j
Notes
Time of impactTime of impact
• Must apply impulse exactly at time of impactMust apply impulse exactly at time of impact
– After detecting interpenetration, use binary search (or more After detecting interpenetration, use binary search (or more sophisticated) to fine tunesophisticated) to fine tune
– Beware of “tunneling” when dt is so large collisions are Beware of “tunneling” when dt is so large collisions are missedmissed
• Edge/edge collisions are modeled as point/point in this systemEdge/edge collisions are modeled as point/point in this system
• Only two colliding bodies at a timeOnly two colliding bodies at a time
• 3D is harder because of variety of collision types3D is harder because of variety of collision types
Time of impactTime of impact
• Must apply impulse exactly at time of impactMust apply impulse exactly at time of impact
– After detecting interpenetration, use binary search (or more After detecting interpenetration, use binary search (or more sophisticated) to fine tunesophisticated) to fine tune
– Beware of “tunneling” when dt is so large collisions are Beware of “tunneling” when dt is so large collisions are missedmissed
• Edge/edge collisions are modeled as point/point in this systemEdge/edge collisions are modeled as point/point in this system
• Only two colliding bodies at a timeOnly two colliding bodies at a time
• 3D is harder because of variety of collision types3D is harder because of variety of collision types
dy
dx known
Y = f(t), unknown
t, specified
Y
t
f(t+t), unknown
Numerical Integration (from Dr. Tom Hobbs Systems Ecology course at Colorado State)
dy
dty y 6 007 2.
Analytical solution to dy/dt
Y0 = 10
t = 0.5
point to estimate
Example
y
y0 = 10
k1 = dy/dt at y0
k1 = 6*10-.007*(10)2
y = k1*t
yest= y0 + y
t = 0.5
y
estimated y
analytical y
Euler (pronounced “oiler”)
dy
dty y 6 007 2.
t = 0.5
point to estimate
Problem: estimate the slope to
calculate y
y
Runge-Kutta (pronounced Run-gah Kut-tah)
1
2 1
3 2
4 3
1 2 3 4
'( , ) ( , )
'( , )
'( / 2, / 2)
'( / 2, / 2)
'( , )
1( 2 2 )
6t t
f t y derivative at t y
k f t y
k f t t y k t
k f t t y k t
k f t t y k t
y y t k k k k
t/2
1( / 2, / 2)t tx t y k t
( , )t y
slope = k1
1 / 2k t
t
y
Runge-Kutta (4th order)
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.63k2=slope 2
t = 0.5/2
y1
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
k3 = slope 3
t = 0.5/2
y2
Step 4: Calculate y3 at t +t using k3.
Evaluate slope at y3.
y3 = y0 + k3* t
y3 =141.0
k4 = dy/dt at y2
k4 = 6*141.0-.007*(141.0)2
k4 = 706.9
k4 = slope 4
t = 0.5
y2
y3
Step 5: Calculate weighted slope.
Use weighted slope to estimate y at t +t
1 2 3 4
1( 2 2 )
6k k k k weighted slope =
1 2 3 4
1( 2 2 )
6t tY Y t k k k k
true value
estimated valueweighted slope
t = 0.5
• 4th order Runge-Kutta offers substantial improvement over Eulers.4th order Runge-Kutta offers substantial improvement over Eulers.
• Both techniques provide estimates, not “true” values.Both techniques provide estimates, not “true” values.
• The accuracy of the estimate depends on the size of the step used in the The accuracy of the estimate depends on the size of the step used in the algorithm.algorithm.
• 4th order Runge-Kutta offers substantial improvement over Eulers.4th order Runge-Kutta offers substantial improvement over Eulers.
• Both techniques provide estimates, not “true” values.Both techniques provide estimates, not “true” values.
• The accuracy of the estimate depends on the size of the step used in the The accuracy of the estimate depends on the size of the step used in the algorithm.algorithm.
Runge-Kutta
Analytical
Euler
Conclusions