Adam P08
Adam P08
Adam P08
The Content
Motivation
Algorithm
Some of the math behind
Constraint handling
Constraint representation:
a cardinality nj
the constraint j is a function Cj(x): R3nj → R
set of indices
stiffness parameter (defines the strength of the constraint)
equality constraint j is satisfied if: Cj (x) = 0
inequality constraint j is satisfied if: Cj (x) ≥ 0
Algorithm
(1) forall vertices i
(2) initialize xi = xi0, vi = vi0, wi = 1/ mi
(3) endfor
(4) loop
(5) forall vertices i do vi = vi +∆twifext(xi)
(6) dampVelocities(v1, ... , vN)
(7) forall vertices i do pi = xi + ∆tvi
(8) forall vertices i do generateCollisionConstraints(xi → pi)
(9) loop solverIterations times
(10) projectConstraints(C1, ... , CM+Mcoll ;p1, ... ,pN)
(11) endloop
(12) forall vertices i
(13) vi = (pi - xi) / ∆t
(14) xi = pi
(15) endfor
(16) velocityUpdate(v1, ... , vN)
(17) endloop
Algorithm description
Initialization:
(1)-(3) initialize the state variables.
Velocity manipulation:
(5) allows to hook up external forces
(6) damps the velocities if necessary
(16) the velocities of colliding vertices are modified according to friction and restitution
coefficients
Constraint manipulation:
(8) generates the Mcoll collision constraints
(10) projects all of the constraints
Position based dynamics:
(7) estimates pi of the vertices are computed using explicit Euler
(9)-(11) manipulate these position estimates such that they satisfy the constraints
(13-14) vertices are moved to the optimized estimates and the velocities are updated
accordingly
Solver
Input:
M +Mcoll constraints
estimates p1, ... , pN
The solver tries to modify the estimates such that they satisfy all the constraints. The
resulting system of equations is non-linear.
Solution:
iterative, similiar to the Gauss-Seidel method
the idea is to solve each constraint independently one after the other
repeatedly iterate through all the constraints and project the particles to valid locations
order of constraints is important
Constraint projection
moving the points such that they satisfy the constraint
internal constraints must conserve both linear and angular momentum
The Issue:
let us have a constraint with cardinality n on the points p1, ... , pN with constraint function C
and stiffness k.
let p be the concatenation [p1T, ..., pNT] T
for internal constraints rotating or translating the points does not change the value of the
constraint function
The Solution:
if the correction ∆p is chosen to be along the gradient ∇ p C(p) both momenta are
conserved
Constraint projection
The Correction:
given p we want to find a correction ∆p such that C(p+ ∆p) = 0 ( ≥ 0).
approximation: C(p+ ∆p) ≈ C(p) + ∇ p C(p) . ∆p = 0
to solve the problem one needs to find a scalar λ (lagrange multiplier): ∆p = λ ∇ p C(p)
C ( p)
λ= −
| ∇ p C ( p ) |2
solving for λ and substituting it into the formula yields the final formula for ∆p:
C ( p)
∆p = − ∇ pC ( p)
| ∇ p C ( p ) |2
The result is a non-linear equation, which can be solved iteratively for each point pi
alone
Constraint projection
∆pi = − s∇ pi C ( p1 ,..., p N ) , where s is the scaling factor (same for all points)
C ( p1 ,..., p N )
s=
∑ pj 1 N
|
j
∇ C ( p ,..., p ) | 2
The methods described so far work if all the points have the same masses
Weighted projection
If the points have individual masses then the corrections ∆p must be weighted by the
inverse masses wi = 1 / mi
In this case a point with infinite mass, i.e. wi = 0, does not move for example as expected
C ( p1 ,..., p N )
s=
∑ w j | ∇ p j C ( p1 ,..., pN ) |2
∆pi = − s wi ∇ pi C ( p1 ,..., p N )
Constraint projection
Type handling is straightforward:
For equality constraint always perform a projection
For inequality constraint perform a projection only when C(p) < 0
Stiffness parameter k:
1/ n s
better solution: multiply by 1 - (1 - k) where ns is the number of iterations
resulting material stiffness is applied linearly, but it is still dependent on the time step of the
simulation.
Distance constraint
C(p1,p2) = |p1 - p2| - d = 0
The gradients:
(p1 - p 2 )
∇ p1 C(p1 , p 2 ) =
| p1 - p 2 |
(p1 - p 2 )
∇ p 2 C(p1 , p 2 ) = -
| p1 - p 2 |
The scaling factor s:
| p1 - p 2 | - d
s=
w1 + w 2
Final formula:
w1 p -p
∆p1 = - (| p1 - p 2 | - d) 1 2
w1 + w 2 | p1 - p 2 |
Example – Distance Constraint
Let us consider a 2D case of 3 vertexes A, B, C bound by 2 distance constraints.
The parameters:
Weights: mA = 10, mB = 5, mC = 2
Inverse weights: wA = 1/10, wB = 1/5, wC = 1/2
Constraints: C1(A, B) = | A – B | - 1, C2(A, C) = | A – C | - 1
New predicted positions: pA = [1, 1], pB = [4, 2], pC = [2, 3]
Stiffness = 1
Example
Both constraints are violated: | A − B | = 10 > 1 | A − C | = 5 >1
Constraint projection:
Lets handle the constraints in order: C1, C2
Formulas:
w1 p -p w1 p -p
∆p1 = - (| p1 - p 2 | - d) 1 2 ∆p 2 = (| p1 - p 2 | - d) 1 2
w1 + w 2 | p1 - p 2 | w1 + w 2 | p1 - p 2 |
C2 : | A − C | ≅ 1.8 > 1
1 [-0.32 , - 1.77] A = [1.7, 1.36], C = [1.88, 2.35]
∆p A = - (1.8 - 1) ≅ [0.02, 0.13] | A - C | = 1,0125
6 1,8
5 [-0.32 , - 1.77]
∆p C = - (1.8 - 1) ≅ [-0.12, - 0.65]
6 1,8
Example
New positions:
A = [1.7, 1.36]
B = [2.63, 1.54]
C = [1.88, 2.35]
Continuous collisions:
for each vertex i the ray xi → pi is tested if it enters an object
compute the entry point qc and the surface normal nc at this position
add a new inequality constraint that ensures non-penetration to the list, such constraint has
function C(p) = (p - qc) . nc ≥ 0 and stiffness k = 1
Static collisions:
compute the surface point qs closest to the point pi and the surface normal nc at this position
add add a new inequality constraint with C(p) = (p - qs) . ns ≥ 0 and stiffness k = 1
To make the simulation faster, the collision constraint generation is done outside of
the solver loop.
Example – Plane Constraint
Consider a case of a particle (single vertex) that has entered a wall (plane), however
the particle is elastic, so it shouldn’t penetrate the wall, but bounce off it.
The parameters:
Plane given by three points: A = [1, 0, 0], B = [0, 1, 0], C = [0, 0, 1]
Particle X position: pX = [0, 0, 0]
Stiffness = 1
Constraint:
C(p) = (p - qs) . ns ≥ 0
1 1 1
ns = normal vector = (1, 1, 1); normalized = , ,
3 3 3
| ∇ p X C ( p X ) |2 = 1
Example
1 1 1 1
∆p X = − ( x + y + z − 1). , ,
3 3 3 3
Solution:
X = [0, 0, 0]
1 1 1 1 1 1 1
∆p X = − (−1). , , = , ,
3 3 3 3 3 3 3
1 1 1
New position: X = , ,
3 3 3
Collision Detection
Proposed method:
(1) forall vertices i
(2) ∆vi = vcm + ω x ri - vi
(3) vi ← vi + kd∆vi
(4) endfor
The variables:
pcm = (Σi pi mi) / (Σi mi)
vcm = (Σi vi mi) / (Σi mi) (velocity due to global body motion)
ri = pcm – pi
L = Σi ri x (mi vi )
J = Σi (rxi)(rxi) T mi , where rxi is the cross product matrix
ω = J-1 L
Attachments
Attaching vertices to static or kinematic objects
the position based dynamics framework has been used to implement a real time cloth
simulator for games
Representation of cloth:
simulator accepts as input arbitrary triangle meshes
the input mesh must represent a 2-manifold
each node of the mesh becomes a simulated vertex
user inputs cloth density and thickness, which are used to calculate the mass of each triangle
the mass of a vertex is set to the sum of one third of the mass of each adjacent triangle
constraints are defined along edges and faces
Constraints
Stretching constraints:
generated for each edge
Cstretch(p1, p2) = |p1 – p2| - l0 = 0
l0 is the initial length of the edge
the stiffness parameter kstretch is set by the user
Bending constraints:
generated for each pair of adjacent triangles (p1, p3, p2) and (p1, p2, p4)
(p 2 - p1 ) x (p3 - p1 ) (p 2 - p1 ) x (p 4 - p1 )
C bend (p1 , p 2 , p 3 , p 4 ) = acos( . ) - ϕ0
| (p 2 - p1 ) x (p3 - p1 ) | | (p 2 - p1 ) x (p 4 - p1 ) |
φ0 is the initial dihedral angle between the two triangles
the stiffness parameter kbend is set by the user
Cloth simulation
Collisions
Collision with rigid bodies:
to get two-way interactions an impulse mi∆pi / ∆t is applied at the contact point each time
the vertex i is projected due to collision
Self-collisions:
assume the triangles all have about the same size and use spatial hashing to find vertex
triangle collisions
if a vertex q moves through a triangle p1, p2, p3, use the constraint function:
C(q, p1, p2, p3 ) = ± (q - p1) . [(p2 – p2) x (p3 – p1)] – h (h is cloth thickness)
testing continuous collisions is insufficient if cloth gets into a tangled state
Cloth Balloons
For closed triangle meshes, overpressure inside the mesh can easily be modeled
The model:
an equality constraint concerning all N vertices of the mesh
compute the actual volume of the closed mesh and compare it against the original volume
V0 times the overpressure factor kpressure
ntriangles
C ( p1 ,..., p N ) = ∑ ( pt i × pt i ). pt i − k pressureV0
i =1
1 2 3
t1i , t 2i , t3i are the three indices of the vertices belonging to triangle i
Cloth Tearing