CIS 350 – I Game Programming Instructor: Rolf Lakaemper

53
CIS 350 – I Game Programming Instructor: Rolf Lakaemper

description

CIS 350 – I Game Programming Instructor: Rolf Lakaemper. Introduction To Collision Detection. Parts of these slides are based on www2.informatik.uni-wuerzburg.de/ mitarbeiter/holger/lehre/osss02/schmidt/vortrag.pdf by Jakob Schmidt. What ?. The problem: - PowerPoint PPT Presentation

Transcript of CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Page 1: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

CIS 350 – I

Game Programming

Instructor: Rolf Lakaemper

Page 2: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Parts of these slides are based onwww2.informatik.uni-wuerzburg.de/ mitarbeiter/holger/lehre/osss02/schmidt/vortrag.pdf

by Jakob Schmidt

Introduction To

Collision Detection

Page 3: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

What ?

The problem:

The search for intersecting planes of different 3D models in a scene.

Collision Detection is an important problem in fields like computer

animation, virtual reality and game programming.

Page 4: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Intro

The problem can be defined as

if, where and when

two objects intersect.

Page 5: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Intro

This introduction will deal with the basic problem:

IF

two (stationary) objects intersect.

Page 6: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Intro

The simple solution:

Pairwise collision check of all polygons the objects are made

of.

Page 7: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Intro

Problem: • complexity O(n²)

• not acceptable for reasonable number n of polygons

• not applicable for realtime application

Page 8: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

Part 1: Bounding Volumes

Reduce complexity of collision computation by substitution of the (complex) original object

with a simpler object containing the original one.

Page 9: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

The original objects can only intersect if the simpler ones do. Or better: if the simpler

objects do NOT intersect, the original objects won’t either.

Page 10: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

How to choose BVs ?

• Object approximation behavior (‘Fill efficiency’)• Computational simplicity• Behavior on (non linear !) transformation (incl. deformation) • Memory efficiency

Page 11: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

Different BVs used in game programming:

• Axes Aligned Bounding Boxes (AABB)• Oriented Bounding Boxes (OBB)

•Spheres• k-Discrete Oriented Polytopes (k DOP)

AABB OBBSphere k-DOP

Page 12: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

Axes Aligned Bounding Box (AABB)

• Align axes to the coordinate system

• Simple to create

• Computationally efficient

• Unsatisfying fill efficiency

• Not invariant to basic transformations, e.g. rotation

Page 13: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

Axes Aligned Bounding Box (AABB)

Collision test: project BBs onto coordinate axes. If they overlap on each axis, the objects collide.

Page 14: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

Oriented Bounding Box (OBB)

Align box to object such that it fits optimally in terms of fill efficiency

Computationally expensiveInvariant to rotationComplex intersection check

Page 15: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

The overlap test is based on the

Separating Axes Theorem(S. Gottschalk. Separating axis theorem. Technical Report TR96-024,Department

of Computer Science, UNC Chapel Hill, 1996)

Two convex polytopes are disjoint iff there exists a separating axis orthogonal to a face of either polytope or orthogonal to an edge

from each polytope.

Page 16: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

Each box has 3 unique face orientations, and 3 unique edge directions. This leads to 15

potential separating axes to test (3 faces from one box, 3 faces from the other box, and 9

pairwise combinations of edges).

Page 17: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

Sphere

• Relatively complex to compute• Bad fill efficiency• Simple overlap test• invariant to rotation

Page 18: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

K-DOP

•Easy to compute•Good fill efficiency•Simple overlap test

•Not invariant to rotation

Page 19: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

k-DOP is considered to be a trade off between AABBs and OBBs.

Its collision check is a general version of the AABB collision check, having k/2

directions

Page 20: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

k-DOPs are used e.g. in the game

‘Cell Damage’ (XBOX, Pseudo

Interactive, 2002)

Page 21: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

How to Compute and Store k-DOPs:

k-directions

k-directions Bi define halfplanes (they are the normals to the halfplanes) , the

intersection of these halfplanes defines the k-DOP bounding volume.

Halfplane Hi = {x | Bi x – di <= 0}

Bounding Volumes

Page 22: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

3D Example: UNREAL-Engine

Page 23: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

2D Example for halfplanes defining a k-DOP

Normal vector Bi

Page 24: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

Again: halfplane Hi = {x | Bi x – di <= 0}

•If the directions Bi are predefined, only the distances di must be stored to specify the halfplane Hi . This is one scalar value per direction.

•If the directions are NOT predefined, Bi

and di must be stored (3D case: 4 values)

Page 25: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

How to compute di :

Hessian Normal Form ( Bx – d = 0 with d = Bp) with

unit vectorof a plane automatically gives

distance d if a single point p on the plane is known.

Page 26: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

Compute distances of all vertices to plane Bx = 0, i.e. multiply (dot product) each

vertex with the unit normal vector B

Bx = 0

Unit vector B

Page 27: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

di is the minimum distance of the object to the plane Bx = 0

Bx = 0di

Page 28: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

Collision

Given: k non kollinear directions Bi andV = set of vertices of object.

Compute di = min{Bi v| v in V } and Di = max{Bi v| v in V}. di and Di define an interval on the axis given by Bi .

This is the interval needed for the collision detection !

Page 29: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

Collision

B1

Plane defined by B1 Interval [d1, D1] defined by B1

d1

D1

Page 30: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Bounding Volumes

Part 2: Collision on different scales:

Hierarchies

Page 31: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

Idea:

To achieve higher exactness in collision detection, build a

multiscale BV representation of the object

Page 32: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

Page 33: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

Use the hierarchy from coarse to fine resolution to exclude non intersecting

objects

Page 34: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

The hierarchy is stored in a tree, named by the underlying BV scheme:

AABB – treeOBB – tree

Sphere – treekDOP – tree

Page 35: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

Sphere Trees are used for example in

“Gran Tourismo”

Page 36: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

Simple example:

• Binary tree• Each node contains all primitives of its subtree• Leaves contain single primitive

Page 37: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

Page 38: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

Page 39: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

Recursive Collision Detection

)

Returns TRUE if BBs overlap.How could this be improvedto give a precise overlap test ?

Page 40: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

Page 41: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

How to create a hierarchy tree

Top down:

• Use single BV covering whole object• Split BV

• Continue recursively until each BV contains a single primitive

Page 42: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

Bottom up:

• Start with BV for each primitive• Merge

Page 43: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

Example for top down using OBBs :

Page 44: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Hierarchies

Comparison AABB / OBB

Page 45: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Multiple Objects

Part 3: Collision between

Multiple Objects

Page 46: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Multiple Objects

Virtual environment usually consists of more than 2 objects. Pairwise detailed collision

between all objects is too slow. Solution again:

1. Exclude non colliding objects2. Check collision between remaining

objects

Page 47: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Multiple Objects

Methods to exclude non colliding objects:

1.Grid MethodOr

2. Sort and Sweep (AABB)

Page 48: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Multiple Objects

Grid Method:

Create 3d grid volume overlay

Only check collision between objects sharing at least one cell

Page 49: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Multiple Objects

2D example

Page 50: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Multiple Objects

Sort and Sweep

• Create single AABB for each object

• Project BVs onto coordinate axes

• Create a sorted list of start and endpoints for each coordinate axis, hence store the

intervals created by each object

(Cont’d)

Page 51: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Multiple Objects

• Traverse each list• If startpoint of object i is hit, insert i into

‘active list’• If endpoint of object i is hit, remove i from

‘active list’• If 2 objects i1,i2 are active at the same time

they overlap in the dimension processed• Objects overlapping in all single dimensions

overlap in world

Page 52: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Multiple Objects

S3S1E3S2E1E2

1

2

3

s3

e3

e1

s1

s2

e2

e2e1s2 e3s1 s3

S1S2E1S3E2E3

X Y

OVERLAP 1,2

Page 53: CIS 350 – I Game Programming Instructor: Rolf Lakaemper

Multiple Objects

Note: sort and sweep for a single step is relatively expensive.

Since not all objects are transformed for the next frame, the list is not created newly for each frame, but updated.