Beruflich Dokumente
Kultur Dokumente
Who are we
BeRo
urs
What is KRAFT
Works on most operating systems and platforms, which is by FreePascal (and Delphi, of
course) supported
Inspired by the design and ideas of and from the open-source Box2D and the closed-source
Rubikon (from Valve) physics engines.
Many thanks to Erin Catto (Box2D) and Dirk Gregorius (Rubikon/Valve)
In other words:
This talk does not describe how a physics
engine works in general
This talk does describe how the KRAFT
physics engine is different.
Collision response
Constraints
Gyroscope simulation
etc.
Single-threaded, because the current CCD time-step-subdividing implementation inside KRAFT must be processed in a serial way
A rigid body in KRAFT can have multiple shapes without a need for compound shapes or similar constructs,
which would only increase the code complexity
Discrete solver
When
CCD is on
Discrete solver
When
CCD is on
Broad phase
Constant bounds
Current velocity of the owner rigid body of a shape (for dynamic and kinematic rigid bodies, but
not for static rigid bodies)
Performs better than Sweep-and-prune (SAP) based broad phases, Octree based broad phases and simple
grid-based spatial-hashing based broad phases in the most cases for the CPU-side application in 3D worlds
Generates contact pairs for the next mid phase and over-next narrow phase steps
Broad phase
Discrete solver
When
CCD is on
Mid phase
Handles the possible contact detection between convex shapes and single
triangles of mesh shapes
Uses the same temporal caching concept as the broad phase
Realized with a secondary contact pair list, but here convex shapes versus triangle subshapes from mesh shapes
Mid phase
New possible contact pairs will be only generated when a contact pair did exist not yet
Contact pairs, these are no longer in contact (expanded-AABB-wise), will destroyed
Realized by a dynamic-sized dirty-queue-buffer (which can only grow on the memory manager
allocation-side, no (or very seldom) shrinks due to performance reasons)
All shapes, which have a new world position and/or world orientation or which are fresh
inserted into the simulated world, are inserted into this one dirty-queue-buffer
The mid phase processes then only these shapes from this one dirty-queue-buffer, and
clears then this one dirty-queue-buffer (i.e. the count of contained items will be reset to
zero, but the already allocated memory will neither freed or shrinked)
Discrete solver
When
CCD is on
Narrow phase
4
5
Convex
Concave
Convex Set C
Contact points
struct ContactPoint {
Vector3 position;
float penetrationDepth;
};
Penetration depth
Contact manifolds
struct ContactManifold {
int countContactPoints;
ContactPoint contactPoints[4];
Vector3 normal;
};
Normal
Solving contact
Normal
2
Penetration depth
Solving contact
Normal
2
rel
=(
=0
Solving contact
+P
-P
Normal
Solving contact
Normal
Penetration depth
= Delta time
rel
= (0.1 )
One-shot manifold
Find closest features (for
examples with SAT) to
compute a contact
manifold in one single shot
using clipping techniques
and analytical geometry
techniques
KRAFT supports both approaches, where the one shot manifold approach is the
default
Margins
Normal
=1
=2
=1
=2
Ooooh!
This is the worst case where objects might rotate out of the world
A contact manifold is stable when the center of mass projects inside manifold
Following from this we need at least up to four frames to construct a stable
contact manifold using the incremental manifold approach
2 points: We can still rotate about the axis through the two contact points across a diagonal
(for example like a hinge)
3 points: We can still rotate around the axis through the two contact points across a diagonal
4 points: Stable manifold after a minimum of four frames
1 point
2 points
3 points
4 points
Incremental manifold
Touching features
Clip plane
=1
=2
A quick look how a one shot manifold is constructed at multiple several frames.
Hopefully you can see the advantages with this approach.
Collision shapes
r
r
c
Sphere
Capsule
4
5
HuIl
Mesh
Collision shapes
Box
8 vertices, 6 faces
Triangle
3 vertices, 2 faces (one face in two orientations double-sided face)
YES
Capsule
Hull
Mesh
YES
YES
YES
YES
YES
YES
YES
YES
NO
MPR (Minkowski Portal Refinement) for penetrating cases (two penetration points, penetration depth, penetration normal)
cheaper than EPA (Expanding Polytope Algorithm) for the two-algorithm GJK/EPA combination solution
SAT (Separating Axis Test) for convex polyhedra (minimum separation vector, penetration depth, two closest points, distance)
P=Support(d)
P=Support(d)
The Configuration Space Object (CSO) of A and B is the Minkowski Sum of A and -B, denoted A B.
Geometrically, the Configuration Space Object (CSO) of A and B is a shape, which is the collection of
points, where each point is the result of a point from A minus a point from B.
A B = { PA - PB | PA A, PB B }
The Configuration Space Object (CSO) has one important property, that when the Configuration Space
Object (CSO) of two shapes contains the origin, the two shapes are colliding.
GJK (GilbertJohnsonKeerthi)
GJK finds the closest points P1 and P2 and distance d between two
disjoint convex shapes
GJK (GilbertJohnsonKeerthi)
KRAFTs GJK implementation also features simplex caching as temporalbased performance optimization
GJK relies solely on a support mapping function to iteratively generate closer
simplices to the correct answer using the Minkowski sum (CSO) of two
convex shapes
0 - simplex
1 - simplex
2 - simplex
3 - simplex
Q = { Q0 ,Q1 , Q2 }
Q1
C
Q2
Q0
Q = { Q0 ,Q1 , Q2 }
Q1
C
P
Q2
Q0
Q = { Q1 , Q 2 }
Q1
C
P
Q2
Q0
Q = { Q2 , V }
Q1
C
V
Q0
Q2
V` = SC(-P) = Q2
Q = { Q2 , V }
DONE!
Q1
C
V
P
Q2
Q0
Recursive formulation
Robustness problems
Mathematically equivalent
GJK (GilbertJohnsonKeerthi)
For more complete details see links in the last References slide
MPR finds the two contact points (or alternatively penetration depth
and (not-always-optimal-best) minimum separation normal vector)
between two penetrating convex shapes
The algorithm was created by Gary Snethen in 2006 and was first published in
Game Programming Gems 7 (XenoCollide)
The algorithm was used in Tomb Raider: Underworld and other games created by
Crystal Dynamics and its sister studios within Eidos Interactive
MPR, like GJK, relies on a support mapping function to iteratively find the correct
answer using the two penetrating contact points of two convex shapes
Unlike GJK, MPR does not provide the shortest distance between separated
shapes
MPR can easily be combined with one another using operations such as sweep,
shrink-wrap and affine transformation (example usage: simple continuous nonrotational collision detection)
Figure 1a. Here we see a convex shape, representing BA, and the origin. Our
objective is to determine if the origin lies within BA, given only the support
mapping of BA to represent the surface.
Figure 1b. We obtain a point that we know lies somewhere deep within BA. We
can obtain such a point by subtracting any point deep within A from any point deep
within B. The geometric centers of A and B are excellent choices.
Figure 1c. We construct a normal that originates at the interior point and points
towards the origin. We then find the support point in the direction of this ray.
Figure 1d. We construct a ray that is perpendicular to the line between the support
just discovered and the interior point. There are two choices for this ray, one for
each side of the line segment. We choose the ray that lies on the same side of the
segment as the origin. We use this ray to find a second support point on the
surface of BA.
Figure 1e. We now have three points, which form an angle. The origin lies
somewhere within this angle.
Figure 1f. We create a line segment between the two support points. This line
segment is called a portal, because the origin ray must pass through the line
segment on its way to the origin.
Source: http://xenocollide.snethen.com/mpr2d.html
Figure 1f: If the origin lies on the same side of the portal as the interior point, then it
lies within the dotted triangle, and must therefore lie within BA. When this is the
case, we terminate with a hit. In this example, the point lies on the outside of the
portal, so the algorithm continues.
Figure 1g: We construct a normal perpendicular to the portal, pointing away from
the interior. We use this normal to obtain a third support point on the surface of B
A. If the origin lies outside of the support line formed by the point and the normal,
we know that the origin lies outside of BA. In this case, the point lies on the inside
of the support line, so the algorithm continues.
Figure 1h: The three support points form a triangle. We know the origin ray passes
through the interior edge of this triangle, because it is a portal. It therefore must exit
through one of the outer edges. To determine which edge it passes through, we
construct a segment between the new support point and the interior point. If the
origin lies on one side of the segment, the origin ray must pass through the outer
edge that lies on the same side of the segment. If the origin lies on the other side,
the origin ray must pass through the other outer edge.
Figure 1i: The outer edge that passes the test becomes the new portal, and we
discard the unused support point. The algorithm continues from the beginning of
phase 2, using the new portal. In the second iteration of our example, the origin lies
on the inside of the new portal, so we will terminate with a hit.
Source: http://xenocollide.snethen.com/mpr2d.html
Source: http://xenocollide.snethen.com/mpr2d.html
Separating axis test and NOT Separating axis theorem, it is NOT a theorem, it is a test
method.
KRAFT SATs implementation uses a gauss-map optimized and temporal optimized SAT variant
SAT can tell us things like:
Axis of minimum penetration
Minimum penetration distance
Touching features
Temporal coherence
Store the result from last frame
If a separating axis in the last frame was detected, then we could try this first in the current
frame. The chances are high, that it will still be a separating axis.
If a overlap in the last frame was detected, then we could try to rebuild the contact manifold
from the last features again. The chances are high, that they still realize the contact manifold,
so that we can skip the whole SAT.
For details for a Gauss-Map-optimized SAT, which is much faster than the naive SAT, see
http://box2d.org/files/GDC2013/DGregorius_GDC2013.zip
Discrete solver
When
CCD is on
Solver
Inspired by Box2D
In turn inspired by position based dynamics
Can be called Nonlinear Gauss-Seidel
Uses a Baumgarte-like parameter for position correction of unilateral constraints to avoid
overshoot
Like Box2D
Discrete solver
When
CCD is on
A simple scenario
Source: http://box2d.org/files/GDC2013/ErinCatto_GDC2013.zip
Source: http://box2d.org/files/GDC2013/ErinCatto_GDC2013.zip
Simple solutions
Solution #1
Solution #2
Look for potential contact points in the future and add additional constraints to the
contact solver.
Restitution needs special handling
Speculative constraints may be invalid and cause ghost collisions where an object may
appear to hit an invisible wall
More constraints to solve
Better solutions
Solution #3 and #4
Solution #1
Solution #2
Solution #3
Accurate
O(N^2) cost in 2D
O(N^3) cost in 3D
Solution #4
Time of impact via Conservative Advancement
Works by considering the distance between two moving shapes
If the distance is non-zero then the shapes can move by some amount
without driving the distance to zero
The distance and closest points can be calculated using GJK
Iterative algorithm
Marches time forward until there is a hit or a miss
Each iteration requires a new GJK call, so that is the main cost of the
algorithm
Hundreds of calls to GJK would certainly lead to frame spikes
Solution #5
Time of impact via Bilateral Advancement
Invented by Erin Catto
A enhancement to Conservative Advancement
Allows the use of standard root finding algorithms
Critical for producing reliable performance
Uses closest features
Not brute force, its an iterative algorithm
For more details see http://box2d.org/files/GDC2013/ErinCatto_GDC2013.zip
References
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
http://www.cas.mcmaster.ca/~carette/SE3GB3/2006/notes/gjk1_pres.pdf
http://xenocollide.snethen.com/ (MPR)
http://box2d.org/files/GDC2015/DirkGregorius_Contacts.pdf
http://box2d.org/files/GDC2014/DirkGregorius_ImplementingQuickHull.pdf
http://box2d.org/files/GDC2013/DGregorius_GDC2013.zip
http://box2d.org/files/GDC2013/ErinCatto_GDC2013.zip
http://box2d.org/files/GDC2011/GDC2011_Catto_Erin_Soft_Constraints.pdf
http://box2d.org/files/GDC2010/GDC2010_Catto_Erin_GJK.pdf
http://www.bulletphysics.org/Bullet/phpBB3/viewforum.php?f=19
http://twvideo01.ubm-us.net/o1/vault/gdc2013/slides/822403Gregorius_Dirk_TheSeparatingAxisTest.pdf