CSE 381 – Advanced Game Programming Quickhull and GJK.

37
CSE 381 – Advanced Game Programming Quickhull and GJK

Transcript of CSE 381 – Advanced Game Programming Quickhull and GJK.

Page 1: CSE 381 – Advanced Game Programming Quickhull and GJK.

CSE 381 – Advanced Game ProgrammingQuickhull and GJK

Page 2: CSE 381 – Advanced Game Programming Quickhull 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

Page 3: CSE 381 – Advanced Game Programming Quickhull and GJK.

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…)

Page 4: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 5: CSE 381 – Advanced Game Programming Quickhull and GJK.

Types of Collisions

Object versus plane (navigation or culling)

Object versus object (general collision)

Linear component versus object (picking)

Page 6: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 7: CSE 381 – Advanced Game Programming Quickhull and GJK.

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.

Page 8: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 9: CSE 381 – Advanced Game Programming Quickhull and GJK.

Collision Detection Game Physics

This is a huge topic

Game physics is rapidly changing

Page 10: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 11: CSE 381 – Advanced Game Programming Quickhull and GJK.

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)

Page 12: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 13: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 14: CSE 381 – Advanced Game Programming Quickhull and GJK.

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)

Page 15: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 16: CSE 381 – Advanced Game Programming Quickhull and GJK.

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]

Page 17: CSE 381 – Advanced Game Programming Quickhull and GJK.

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)

Page 18: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 19: CSE 381 – Advanced Game Programming Quickhull and GJK.

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.

Page 20: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 21: CSE 381 – Advanced Game Programming Quickhull and GJK.

Octree

Source: http://en.wikipedia.org/wiki/Image:Octree2.png

Page 22: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 23: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 24: CSE 381 – Advanced Game Programming Quickhull and GJK.

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]

Page 25: CSE 381 – Advanced Game Programming Quickhull and GJK.

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)

Page 26: CSE 381 – Advanced Game Programming Quickhull and GJK.

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)

Page 27: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 28: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 29: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 30: CSE 381 – Advanced Game Programming Quickhull and GJK.

Visualizing Minkowski Sums

• Results in swept shapes

ABA B

Page 31: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 32: CSE 381 – Advanced Game Programming Quickhull and GJK.

Visualizing Minkowski Differences

• Results in swept shapes

AB

A B

Page 33: CSE 381 – Advanced Game Programming Quickhull and GJK.

An Interesting Detail

• For Minkowski Difference A B

• If A and B are intersecting, A B contains origin

AB

A B

Page 34: CSE 381 – Advanced Game Programming Quickhull and GJK.

Now Let’s Get Back To GJK

• Assume we have two shapes, A & B

• We want to know if A & B intersect

Page 35: CSE 381 – Advanced Game Programming Quickhull and GJK.

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

Page 36: CSE 381 – Advanced Game Programming Quickhull and GJK.

Visualizing GJK Miss (no collision)

Note: I’ve simplified things a bit for rendering

Page 37: CSE 381 – Advanced Game Programming Quickhull and GJK.

Visualizing GJK Hit (collision)