The Making of ”Hitman: Codename 47”

Download Report

Transcript The Making of ”Hitman: Codename 47”

Advanced Character Physics
– the fysix engine
Thomas Jakobsen
Head of R&D
Game Developers Conference
San Jose, March 20-24, 2001
Introduction


Hitman: Codename 47
The fysix engine
–
–
–
–
–
–
Particle systems
Cloth
Plants
Rigid bodies
Articulated bodies / rag dolls
Water
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Overview




Introduction & overview
Demos
Drawbacks of other methods
The approach used in fysix
–
–
–
–

Integration techniques
Solving for constraint forces
Handling friction, contact, singularities etc.
Optimizations
Improvements
–
Algorithms from molecular dynamics
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Drawbacks of Other Methods









Speed issues
Time usage varies unpredictably
Scales poorly with the number of objects in contact
Instability
Unphysical behavior (elastic constraints)
Drift
Unresolvable (illegal) configurations exist
Contact, collision, and penetration are separate cases
Complex mathematics
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Advantages of the
fysix Approach

Simplicity
–
–

Speed
–

Possible to trade off speed vs. accuracy
Stability
–

Basic implementation does not involve complex mathematics
Local approach to solving a global problem
Jittering or ”exploding” systems are rare
Generality
–
–
A unified system for cloth, soft bodies, rigid bodies, articulated
bodies, and inverse kinematics
Handles contact, collision, and penetration
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
A Combination of Several
Techniques Working Together





Verlet integration
Solving constraints by relaxation
Handling contacts, collisions, and penetrations
by projection
Rigid bodies simulated by constrained particles
(A fast square root approximation)
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Two Approaches to
Particle Systems
Standard approach – Euler integration:
( x, v )
Particle state:
x'  x  v  t , v'  v  a  t.
Update rule:
Simple. Used often. Unstable. Low precision.
The fysix approach – Verlet (or Störmer) integration:
(x, x*)
Particle state:
x'  2x  x*  a  t 2 , x*  x.
Update rule:
Simple. Symplectic! Stable.
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Example – Box World, Part 1
Constraints :
0  x0  1000
(C1)
0  x1  1000
0  x2  1000

Time-step procedure:
– Call Verlet integrator
– Satisfy constraints (in this
example clamp positions in
order to respect box limits)
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Example: Stick Constraint
Dist. too large

Simulate a stick by constraining two particles to
have a fixed distance between them
(x[1]  x[0])  (x[1]  x[0])  r 2




(C2)
Pull particles directly together or push them away
from each other to fix an invalid configuration
Move particles a distance inversely proportional to
their respective masses (such that the constraint is
satisfied)
Set the inverse mass of a particle to zero to make
it immovable
Other constraints can be implemented by
considering the constraint Jacobian
Advanced Character Physics – the fysix engine
Correct distance
Dist. too small
www.ioi.dk/~tj
Example – Box World, Part 2
Constraint:
(x[1]  x[0])  (x[1]  x[0])  r 2
(C2)
// Pseudo-code for satisfying
// the stick constraint (C2)
delta = x2-x1;
deltalength = sqrt(delta*delta);
delta *= (deltalength-restlength)
/(deltalength*(invmass1+invmass2));
x1 += invmass1*delta;
x2 -= invmass2*delta;
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Relaxation – Handling Multiple
Simultaneous Constraints

Idea:
–
–
–
–
–
Satisfy constraints locally
(one at a time)
Iterate over all constraints
Hope that the result
converges globally
Indirectly solves a system
of (linearized) equations
By stopping the iterations
early, one can trade off
speed vs. accuracy
Relaxation algorithm
Input:
Particles x[0],..., x[i-1]
Constraints c[0],..., c[j-1]
repeat
for k=0,..., j-1
change particle positions
such that c[k] is satisfied
next
until convergence
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Example – Box World, Part 3
Const raints (i  0, i  1) :
0  x[i ]0  1000
(C1)
0  x[i ]1  1000
0  x[i ]2  1000
(x[1]  x[0])  (x[1]  x[0])  r 2
Advanced Character Physics – the fysix engine
(C2)
www.ioi.dk/~tj
Stick-in-a-box Code
// Implements simulation of a stick in a box
void ParticleSystem::SatisfyConstraints() {
for(int h=0; h<NUM_ITERATIONS; h++) {
// First satisfy the box constraint (C1)
for(int i=0; i<NUM_PARTICLES; i++) { // For all particles
Vector3& x = m_x[i];
x = vmin(vmax(x, Vector3(0,0,0)), Vector3(1000,1000,1000));
}
// Then satisfy the stick constraint (C2)
Vector3& x1 = m_x[0];
Vector3& x2 = m_x[1];
Vector3 delta = x2-x1;
float deltalength = sqrt(delta*delta);
diff *= (deltalength-restlength)
/(deltalength*(invmass1+invmass2));
x1 += invmass1*diff;
x2 -= invmass2*diff;
}
}
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Simulation of Cloth

Standard approach (spring system)
–
–

Stiff springs: Instability or slow integration
Weak springs: Gives cloth an elastic appearance
Using constraints (the fysix approach)
–
–
–
Stable (no visible vibrations or jittering)
Fast (only one division per edge per frame)
Not necessarily physically accurate but it looks nice
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
A Square-root Approximation




a  r2
a r
if a  r * r
2r
Works nicely together
with the Verlet integrator
Complex calculations are
automatically spread
over several frames
Expensive operations
are down to only one
division (!) per edge per
frame
// Cloth simulation inner loop
// (pseudo-code)
for all pairs of neighbors (x1, x2)
r = orig. dist. between x1 & x2;
// Code for satisfying
// the stick constraint (C2)
// using sqrt approximation
delta = x2-x1;
delta*=r*r/(delta*delta+r*r)-0.5;
x1 += delta;
x2 -= delta;
next
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Rigid Bodies = Constrained
Particles


4 particles + 6 constraints = one rigid body
Degrees of freedom 4*3-6 = 6
6 length constraints

3 length constraints
3 perpendicularity constraints
No need for using quaternions, inertia tensors, torque
calculations etc.
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Articulated Bodies

Pin joint:

Hinge:
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Angular Constraints
x1
x0
x2


Constrain the distance between x1 and x2 to be above
(or below) some fixed value:
(x2 x1)  (x2 x1)  d .
Or satisfy the following dot product constraint:
(x2  x0)  (x1  x0)   .
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Human Bodies




Angular constraints
Unilateral distance constraints for
simple self collision
The physics of rotation around the
length axes of limbs is not simulated
(as an optimization)
The actual mesh is attached to the
skeleton by following a twist
minimization strategy
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Motion Control


With the Verlet integrator it is easy to
control the motion of objects by bombs, bullet
hits etc. – simply move the particle positions
proportionally to the force inflicted on them and
the velocities will be adjusted automatically
To inherit velocities from an underlying
animation system simply record the particle
positions for two frames and let the Verlet
integrator take over
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Inverse Kinematics (IK)


Used in Hitman to animate the main
character’s arms and legs and for dragging
dead bodies
Accomplished by simply setting a particle’s
inverse mass to zero (this makes the particle
immovable) and appropriately adjusting the
particle position
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Collision Detection

Optimized code for collision check and
penetration depth+penetration point calculations
were implemented:
–
–


Between triangles and lines
Between triangles and capped cylinders
Background triangles inside the object bounding box
are culled and a special structure for fast collision
checks against static objects is constructed
Various collision ”cheats” were used to speed things up
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Collision and
Contact Handling

Traditional approaches:
–
–
–

Penalty-based methods
Rewinding to the time of collision
Impulse-based simulation
The approach taken by fysix (projection):
–
–
Offending points or edges are simply projected out
of the obstacle
Works in cooperation with the Verlet approach
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Collision and
Contact Handling

Offending features are simply projected out
of the obstacle (in a way that makes physical
sense) in the relaxation loop:
dp

Requires a subsystem for the computation of
penetration distance and penetration points
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Handling Friction

After projection, the tangential velocity is
reduced by an amount proportional to the
penetration distance:
dp

vt
v’t
The tangential velocity should not reverse its
direction, however – in this case, set it to zero
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Embedding the Tetrahedron
Inside Other Objects


Kinetically, a rigid body behaves like a
tetrahedron but not collision-wise
Solution: It is possible to embed the
tetrahedron inside an arbitrary object
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Miscellaneous





The number of relaxation iterations can be
varied at different levels
Soft constraints give soft bodies
Singularities (=> division by zero) can be handled
simply by slightly dislocating particles at random
The cloth algorithm can be extended to simulate plants
by strategically placing support sticks between vertices
sharing a neighbor
To toy with the ragdoll system in Hitman press
shift+F12 in debug mode to blow people up
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
The Underlying
Mathematical Model




Mathematically, fysix is using a symplectic
time-stepping method for solving differential inclusions
The system state is continually projected onto the
manifold described by the constraints
The relaxation approach implicitly inverts the system
matrix
Relaxation as used in fysix is actually a sort of interiorpoint algorithm for solving LCP-like problems
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Possibilities for Improvements

The SHAKE/RATTLE algorithms from
molecular dynamics
–

Relaxation sometimes converges slowly
–
–
–

Leapfrog integration, velocity Verlet
Remember values from last frame (exploit frame coherence)
Use successive overrelaxation (SOR) or other iterative, sparse
matrix techniques
Use other interior-point algorithms (LCP solvers and similar)
Coulomb friction
–
–
static and dynamic
linearize the friction cone to form LCPs
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Water




Full Navier-Stokes equations
Many behaviors are possible: Breaking waves,
mass transport, vortices etc.
Multigrid relaxation
Demo
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Conclusion


The goal in physics simulation for games is
not necessarily the same as the goal in mechanical
engineering and other related scientific areas.
The field of physically-based modeling in computer
graphics (and games) could really benefit from crossdisciplinary experiences from areas such as molecular
dynamics and mechanical engineering. In particular,
check publications by Ben Leimkuhler and J. C. Trinkle
for inspiration.
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Conclusion

If done correctly, many aspects of
advanced physics simulation are not that hard
to implement.

Go out and do it!
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj
Errata to the Article in
the Proceedings





”Verlet Integration” section: To introduce drag,
the update rule should be changed to x’=1.99x0.99x*+... or something similar, not x’=1.99x-x*+... .
”Relaxation” section (sign error, appears 5 times):
x1 -= ...; x2 += ... should read x1 += ...; x2 -= ... .
”Cloth simulation” section: The Taylor approximation is
in a neighborhood of the squared resting distance, not
the resting distance.
”Comments” section: Shift+F9 should read Shift+F12.
Slides and a revised version of the paper is available at
www.ioi.dk/~tj. Future articles will also be posted here.
Advanced Character Physics – the fysix engine
www.ioi.dk/~tj