CSE 381 – Advanced Game Programming Quickhull and GJK.
Transcript of CSE 381 – Advanced Game Programming Quickhull and GJK.
CSE 381 – Advanced Game ProgrammingQuickhull and GJK
What is collision detection?
Determining if, when, & where two objects intersect
What objects?Linear components, planes, triangles, rectangles, oriented boxes,
spheres, capsules, lozenges, cylinders, ellipsoids, etc.
Hugely important part of a 3D game engine. Why?done continuously every frame
involves huge amounts of computations
Entire textbooks are written on this subject aloneReal-Time Collision Detection by Christer Ericson
Collision Detection in Interactive 3D Environments by Gino van den Bergen
Collision Detection & Graphics
Work together in a game engine
Should be developed together
Both share:geometric data
timing elements
So, pool resources for their algorithmse.g., scene graphs, spatial partitioning (octrees, bsps…)
Game Engine Collision Detection
2 phasesBroad phase: determine which pairs of shapes need to be
tested for collision
Narrow phase: determine collision results for each pair identified in broad phase
All game objects maintain collision setsdata for collisions calculations
i.e., bounding volumes
Types of Collisions
Object versus plane (navigation or culling)
Object versus object (general collision)
Linear component versus object (picking)
Imagine a complex world
Hundreds of interior structures, each with rooms
Complex external terrain environment
Water with underwater structures (interiors)
Rather than testing these items serially, we can do so hierarchicallymuch faster
continuously reduce the problem
How should collision data be organized?
One of the most important questions in designing a collision system
Game world might contain a large number of interacting objectsan exhaustive comparison is too exhaustive
Objects should be organized into collision groupse.g., rooms, partitions, etc.
Rooms as collision groups
Scene graphs & collision sets are not static, they change with the gameas players, NPCs, or other game objects move/change
When a player enters a room, the scene graph is reconfiguredplayer is now part of room collision group
Only objects moving within a room are tested against one another for collisions
Collision Detection Game Physics
This is a huge topic
Game physics is rapidly changing
Collision Detection & Response
Collision Detectiondetecting what game objects are colliding with each
other
Collision Responseproviding a programmed response to collisions that
fits with the game’s design & custom laws of physics
Static vs. Dynamic Objects
Static objects never move
Dynamic objects move
Collisions may be between:Static objects & dynamic objects (fast & easy)
Dynamic objects & dynamic objects (harder)
What types of collisions might we care about?
Main character and static objects:terrain, floor, tiles, walls, furniture, buildings, game
objects (i.e. power-ups, ammo), etc.
Main character & dynamic objects:enemies, projectiles (i.e. bullets, arrows, etc.), particles
(expensive), etc.
Other dynamic objects and:other static objectsother dynamic objects
Collisions in Pairs
In collision detection, we always compare pairs of objects. Easier to:understand
design & implement solutions
A naïve approach:one pair at a time, compare all game objects in a game
world against all other game objects in a game world
Collision Detection Calculations
What data are we looking for?1. Do these two objects potentially collide?
2. Do these two objects collide?
3. When did these two objects collide?
4. Where did these two objects collide?where on geometry of objects, points of
impact
These 4 questions get progressively:more computationally expensive to implement
more complex to implement (more math)
Phases of Collision DetectionSpatial Partitioning Algorithms
problem reduction
only perform additional phases on pairs of object on same “islands”
Broad Phase – early rejection tests
Do the coarse Bounding Volumes of two objects collide?
Narrow Phase
What are the contact points on object geometries?
Done down to the last triangle in 3D games
Bounding Volumes
The base geometry used for all collision tests
instead of the shape’s geometry, which is too expensive
Properties of desirable BVs:
inexpensive intersection tests
tight fitting
inexpensive to compute
easy to rotate and transform
use little memory
Ref: [1]
Assumptions you might make
All collideable game objects:have rectangular Bounding Volumes
don’t rotate, or
if they do rotate, we don’t care about them colliding with stuff
Thus:no polytope collision detection equations
no rotation equations
this makes life much easier (no narrow phase)
Common Bounding Volumes
Note that the space craft has been rotated
Ref: [1], Figure 4.2
This semester, we like AABBs and/or Spheresaxis-aligned bounding boxes
Spatial Partitioning
• First, reduce the problem• only perform additional phases on pairs of object on same
“islands”
Common Solutions:Octree Algorithms (quadtrees for 2D)
also: Portals (ala Quake), BSP trees (Binary Space Partitions), Spatial Hashing, etc.
OctreesUsed to divide a 3D world into “islands”
2D divided via Quadtrees
Why?
to make rendering more efficient
to make collision detection more efficient
What would be the data for these nodes?
region coordinates for cell
though a smart implementation might eliminate this too
AABBs
Octree
Source: http://en.wikipedia.org/wiki/Image:Octree2.png
Octree Drawbacks
Objects cross islandsoctree has to be constantly updated
not so bad
Objects straddle islandscollision detection may involve objects from multiple
islands
can be a headache
Broad Phase
Do the coarse AABBs of two objects collide?
Common solution:
separating axis algorithms
including temporal coherence
For efficiencies use:
Sweep & Prune
an extension of separating axis, more efficient for many elements
Narrow Phase
What are the contact points on object geometries?for 3D might be convex hulls
Two Steps1.determine potentially colliding primitives (ex:
triangles) of a pair of objectsAABB tree algorithms
2.determine contact between primitivesGJK algorithms
Ref[3]
What’s a convex set?
• What does convex mean?
• Convex Test:– For all the shape’s vertices:
• Make a line between each pair
• Is entire line inside object?– If yes, it is convex
– If no, it is not (i.e., has dent)
Convex Set vs. Hull
• Convex Set – a set of points representing a convex shape
• Convex Hull– a minimal convex set– minimally snugly fits points (rubber band analogy)
Quickhull
• Algorithm for making a convex hull from a points set
1) Find min and max points on all axes to form minimal bounding box
2) Min/Max points represent first approximation
3) Exclude all points within the hull
4)For each line in approximation, find point furthest outside polytope
5)Add those points to hull in between the points that formed the line
6)If any points remain to consider, go back to step 3
GJK
• Gilbert – Johnson – Keerthi distance algorithm
• Used to determine the minimum distance between convex sets
• Why?– to see if they are colliding– to see where they are colliding
Minkowski Sum
• What’s that?– A mathematical operation performed on 2 shapes
• A B = All Points in A + All Points in B
• How many points do we end up with?– A * B
Visualizing Minkowski Sums
• Results in swept shapes
ABA B
Minkowski Difference
• The Opposite of a Minkowski Sum
• A B = All Points in A – All Points in B
• How many points do we end up with?– A * B
Visualizing Minkowski Differences
• Results in swept shapes
AB
A B
An Interesting Detail
• For Minkowski Difference A B
• If A and B are intersecting, A B contains origin
AB
A B
Now Let’s Get Back To GJK
• Assume we have two shapes, A & B
• We want to know if A & B intersect
GJK for A & B Collision Test
1. Calculate Minkowski Difference
2. Select any Minkowski Difference point
3. Start at that point, and try to get to origin by visiting other points.
– remember, this is a convex set
4. Build polytope using 4 points– iteratively update to find next one
5. If any polytope along way contains origin, no collision
6. If polytope never contains origin, no collision
Visualizing GJK Miss (no collision)
Note: I’ve simplified things a bit for rendering
Visualizing GJK Hit (collision)