CS 480/680: GAME ENGINE PROGRAMMING COLLISION DETECTION 1/31/2013 - - PowerPoint PPT Presentation

cs 480 680 game engine programming collision detection
SMART_READER_LITE
LIVE PREVIEW

CS 480/680: GAME ENGINE PROGRAMMING COLLISION DETECTION 1/31/2013 - - PowerPoint PPT Presentation

CS 480/680: GAME ENGINE PROGRAMMING COLLISION DETECTION 1/31/2013 Santiago Ontan santi@cs.drexel.edu https://www.cs.drexel.edu/~santi/teaching/2013/CS480-680/intro.html Outline Student Presentations Collision Detection Basics


slide-1
SLIDE 1

CS 480/680: GAME ENGINE PROGRAMMING COLLISION DETECTION

1/31/2013 Santiago Ontañón santi@cs.drexel.edu https://www.cs.drexel.edu/~santi/teaching/2013/CS480-680/intro.html

slide-2
SLIDE 2

Outline

  • Student Presentations
  • Collision Detection Basics
  • Collision Shapes
  • Collision Detection Algorithms
  • Optimizations
  • Project Discussion
slide-3
SLIDE 3

Outline

  • Student Presentations
  • Collision Detection Basics
  • Collision Shapes
  • Collision Detection Algorithms
  • Optimizations
  • Project Discussion
slide-4
SLIDE 4

Student Presentations

  • Amar Patel:
  • “Advanced Wall Building for RTS Games”
  • Josep Valls:
  • “Procedural Modeling of Cities”
  • Stephen Ramusivich:
  • “Overview of the OGRE Game Engine”
slide-5
SLIDE 5

Outline

  • Student Presentations
  • Collision Detection Basics
  • Collision Shapes
  • Collision Detection Algorithms
  • Optimizations
  • Project Discussion
slide-6
SLIDE 6

Collision Detection Problems

Why is collision detection important? http://www.youtube.com/watch?v=mYq9emwQzZU http://www.youtube.com/watch?v=ER05CrlzdC0 http://www.youtube.com/watch?v=uP47Jro2oAM http://www.youtube.com/watch?v=cCtBaFcMZ44 http://www.youtube.com/watch?v=VWypWK91T5U

slide-7
SLIDE 7

Game Engine Architecture

HARDWARE DRIVERS OS SDKs Platform Independence Layer Utility Layer Resource Management Game Engine Functionalities Game Specific Game Engine Dependencies

slide-8
SLIDE 8

Game Engine Architecture

Rendering Engine Animation Engine Collisions Physics Audio Subsystem Online Multiplayer Profiling & Debugging Gameplay Foundations (Game Loop) Artificial Intelligence Scripting

slide-9
SLIDE 9

Collision Detection Basics

  • Why?
  • Detect contact between game objects (moving objects, or objects

vs background)

  • How?
  • Each game object (including background) associated with a

collision shape (simpler geometry than the one used for rendering)

  • Calculate collision between collision shapes
  • Typical functionalities:
  • Boolean collision checks
  • Collision volume / area
slide-10
SLIDE 10

Collision Shapes

  • Geometry used for

rendering:

  • Geometry used for

collision detection:

slide-11
SLIDE 11

Implications for Game State

  • You need two separate representations of game objects
  • Visual representation: used for rendering
  • Collision representation: used for determining the behavior of the game
  • bject in game world.
  • Games with simple shapes (e.g. Super Mario), can merge the

two:

  • Visual representation: Sprite (bitmap + hotspot), Transform
  • Collision representation: the bounding box of the sprite + the same

transform as the visual representation

  • In general, you need the two. Even the collision skeletons and

the rendering skeletons might be different, if the rendering ones are too complex. But they need to be kept in sync.

slide-12
SLIDE 12

Collision World

  • For complex games, it is recommended that the collision

detection module maintains its own internal state in something called the “Collision World”

Rendering Engine Animation Engine Collisions Physics Gameplay Foundations (Game Loop) Game State Collision World

slide-13
SLIDE 13

Collision World

  • For complex games, it is recommended that the collision

detection module maintains its own internal state in something called the “Collision World”

Rendering Engine Animation Engine Collisions Physics Gameplay Foundations (Game Loop) Game State Collision World

This is the regular repository for Game Objects, with their game play features, rendering geometry, etc. This is one only contains the collision geometry.

slide-14
SLIDE 14

Collision World

  • From a programming point of view, the Collision Module API

should be something like this:

  • Constructor:
  • collisionWorld(…);
  • Adding removing objects:
  • int addObject(CollisionShape cs, Transform t);
  • removeObject(int ID);
  • Updating objects:
  • void updateObjectTransform(int ID, Transform t);
  • Collision Queries:
  • bool collision(int ID1, int ID2);
  • List<CollisionData> checkForAnyCollisions();
  • etc. (whatever functionalities you want to offer)
slide-15
SLIDE 15

Collision World

  • From a programming point of view, the Collision Module API

should be something like this:

  • Constructor:
  • collisionWorld(…);
  • Adding removing objects:
  • int addObject(CollisionShape cs, Transform t);
  • removeObject(int ID);
  • Updating objects:
  • void updateObjectTransform(int ID, Transform t);
  • Collision Detection:
  • bool collision(int ID1, int ID2);
  • List<CollisionData> checkForAnyCollisions();
  • etc. (whatever functionalities you want to offer)

Some game engines, simply have a method called “cycle()”, that checks for all the collisions, and let you specify “callbacks” to each object, so that if a collision happens, the callback gets executed. This is more efficient, but might be more complex to use.

slide-16
SLIDE 16

Collision Detection Output

  • The most basic test: Boolean intersection test
  • We might want more:
  • Exact area of volume of intersection might not be needed in a

game, but…

  • Point(s) of contact: approximate center of the collision volume
  • Uses:
  • Rigid body dynamics
  • bullet marks in walls (need point of contact)
  • Separation vector: vector along the approximate shortest

dimension of the volume of contact, along which we can slide the

  • bjects to make them not collide
  • Uses:
  • Separate objects before rendering, to avoid overlap (used in sport games)
slide-17
SLIDE 17

Outline

  • Student Presentations
  • Collision Detection Basics
  • Collision Shapes
  • Collision Detection Algorithms
  • Optimizations
  • Project Discussion
slide-18
SLIDE 18

Collision Shapes

  • Spheres
  • Capsules
  • Bounding Boxes
  • Discrete Oriented Polytopes (DOP)
  • Arbitrary convex volumes
  • “Poly mesh”
  • Compound shapes
slide-19
SLIDE 19

Collision Shapes: Spheres

  • Spheres (or circles in 2D) are the most simple collision

shapes

  • Defined by just 4 numbers (3 in 2D):
  • Center (x,y,z)
  • Radius (r)
  • Very quick simple collision check

(x,y,z)

slide-20
SLIDE 20

Collision Shapes: Capsules

  • Capsules are the next easiest shape to perform collisions,

and also very easy to represent

  • 2 points and a radius:
  • x1,y1,z1
  • X2,y2,z2
  • r
  • Easier to represent and to collide than:
  • Cylinders
  • Boxes
  • Commonly used for body parts

r (x1,y1,z1) (x2,y2,z2)

slide-21
SLIDE 21

Collision Shapes: Bounding Boxes

  • Smallest box (cuboid) that encloses a given figure
  • 2 types commonly used:
  • AABB (Axis-Aligned Bounding Boxes):
  • Fastest to perform collision detection
  • Might misfit some shapes
  • OBB (Oriented Bounding Boxes)
  • Relatively fast collision detection
  • Better fit for some shapes
slide-22
SLIDE 22

Collision Shapes: Bounding Boxes

  • Axis-Aligned Bounding Boxes (AABB)
  • Represented by just 2 points
  • Recomputed if object rotates
  • Oriented Bounding Boxes (OBB)
  • Represented by 3 half-radiuses and a transform
  • Very commonly used

(x1,y1,z1) (x2,y2,z2) r1 r2

slide-23
SLIDE 23

Collision Shapes: k-DOPs

  • Discrete Oriented Polytopes
  • Given k vectors (each determining an orientation)
  • Move a plane perpendicular to each vector as close as

possible to the object at hand

  • Ex, k = 6:
slide-24
SLIDE 24

Collision Shapes: k-DOPs

  • Discrete Oriented Polytopes
  • Given k vectors (each determining an orientation)
  • Move a plane perpendicular to each vector as close as

possible to the object at hand

  • Ex, k = 6:
slide-25
SLIDE 25

Collision Shapes: k-DOPs

  • Discrete Oriented Polytopes
  • Given k vectors (each determining an orientation)
  • Move a plane perpendicular to each vector as close as

possible to the object at hand

  • Ex, k = 6:
slide-26
SLIDE 26

Collision Shapes: k-DOPs

  • Discrete Oriented Polytopes
  • Given k vectors (each determining an orientation)
  • Move a plane perpendicular to each vector as close as

possible to the object at hand

  • Ex, k = 6:
slide-27
SLIDE 27

Collision Shapes: k-DOPs

  • Discrete Oriented Polytopes
  • Given k vectors (each determining an orientation)
  • Move a plane perpendicular to each vector as close as

possible to the object at hand

  • Ex, k = 6:
slide-28
SLIDE 28

Collision Shapes: k-DOPs

  • Notice that OBB are a particular case with just 4 vectors

(in 2D) and 6 vectors (in 3D), which are aligned with the 3 axis.

  • Ex, 4-DOP:
slide-29
SLIDE 29

Collision Shapes: Arbitrary Convex Shapes

  • A 3D shape defined by an artist by directly creating

triangles (e.g. in Maya)

  • If it is convex, it is equivalent to a k-DOP
  • It can be converted automatically: the normal of each

triangle define the k vectors

slide-30
SLIDE 30

Collision Shapes: Meshes

  • Arbitrary triangle meshes
  • This are the most complex, slowest collision shapes
  • To check collision:
  • Check each individual triangle for collision!
  • Only collision with the surface
  • Since it might not even be a closed shape!
slide-31
SLIDE 31

Collision Shapes: Compound Shapes

  • Sometimes it’s easier to represent a complex shape as a

composition of simpler shapes, than going for the full triangle mesh.

  • Think of compound shapes as skeletons: basic collision

shapes connected together.

  • Allow many optimizations (later)
slide-32
SLIDE 32

Outline

  • Student Presentations
  • Collision Detection Basics
  • Collision Shapes
  • Collision Detection Algorithms
  • Optimizations
  • Project Discussion
slide-33
SLIDE 33

Collision Testing

  • The collision module will support a subset of the previous

shapes.

  • For each pair of shapes, a different collision detection

algorithm is used.

  • Typically a matrix like this is specified:

Sphere OBB Mesh Sphere supported supported Boolean check only OBB supported supported no Mesh Boolean check only no no

slide-34
SLIDE 34

Collision Testing: Sphere-Sphere

  • Easiest possible check:
  • Sphere: (x,y,z,r)

bool collision(s1, s2) {

dx = s1.x – s2.x dy = s1.y – s2.y dz = s1.z – s2.z d = sqrt(dx*dx + dy*dy + dz*dz) return (d < s1.r + s2.r)

}

slide-35
SLIDE 35

Collision Testing: Sphere-Sphere

  • Easiest possible check:
  • Sphere: (x,y,z,r)

bool collision(s1, s2) {

dx = s1.x – s2.x dy = s1.y – s2.y dz = s1.z – s2.z d = sqrt(dx*dx + dy*dy + dz*dz) return (d < s1.r + s2.r)

}

If we need to provide a separation vector, the vector would just be the

  • ne connecting the two centers, and

the amount we need to move a sphere to avoid the contact is: s1.r + s2.r - d

slide-36
SLIDE 36

Collision Testing: Bounding Boxes

  • Separating Axis Theorem
  • Already covered last week in a student presentation, so,

I’ll just remind the main idea:

slide-37
SLIDE 37

Collision Testing: Bounding Boxes

  • Separating Axis Theorem
  • Already covered last week in a student presentation, so,

I’ll just remind the main idea:

If we can find an axis, where the projections of the shapes are disjoint, then the objects are not colliding

slide-38
SLIDE 38

Collision Testing: Bounding Boxes

  • Separating Axis Theorem
  • Already covered last week in a student presentation, so,

I’ll just remind the main idea:

If we can find an axis, where the projections of the shapes are disjoint, then the objects are not colliding In general, this is not helpful, since finding that axis might be very hard. But for the case of boxes, we can proof that you only need to check axis aligned with each of the faces and edges of the boxes!!

slide-39
SLIDE 39

Collision Testing: The GJK Algorithm

  • If many different shapes are supported, we need special

algorithms for each combination of shapes

  • This increases the complexity of the collision detection module
  • The GJK (Gilbert, Johnson, Keerthi) algorithm solves this

problem for any convex shape

Convex Concave

slide-40
SLIDE 40

Collision Testing: The GJK Algorithm

  • Main idea:
  • “Minkowski difference”
  • Two convex shapes collide only if their Minkowski difference

contains the origin.

A - B A B

slide-41
SLIDE 41

Collision Testing: The GJK Algorithm

  • Main idea:
  • “Minkowski difference”
  • Two convex shapes collide only if their Minkowski difference

contains the origin.

A - B

Minkowski difference is the shape that results from taking each pair of points a in A and b in B, and computing: a – b Result looks like sweeping B around the perimeter of A (or the other way around)

A B

slide-42
SLIDE 42

Collision Testing: The GJK Algorithm

  • Main idea:
  • “Minkowski difference”
  • Two convex shapes collide only if their Minkowski difference

contains the origin.

A B A - B

Distance of A – B from

  • rigin is equivalent to the

minimum distance between A and B If they intersect, there will be at least one point a in A and one b in B which are the same, and thus a – b is the origin of coordinates.

slide-43
SLIDE 43

Collision Testing: The GJK Algorithm

  • GJK algorithm is an algorithm to check whether a point is inside of a convex hull:

1.

Initialize a set Q with up to d+1 points from C (in d dimensions)

2.

Compute point P of closest to the origin in CH(Q)

3.

If P is the origin; return 0

4.

Reduce Q to the smallest subset Q’ of Q, such that P in CH(Q’)

5.

Let V=SC(–P) be a supporting point in direction –P

6.

If V no more extreme in direction –P than P itself; return ||P||

7.

Add V to Q. Go to step 2

  • Full algorithm at:

http://realtimecollisiondetection.net/pubs/SIGGRAPH04_Ericson_GJK_notes.pdf

  • Following slides from Christer Ericson. Originals at:

http://realtimecollisiondetection.net/pubs/ SIGGRAPH04_Ericson_the_GJK_algorithm.ppt

slide-44
SLIDE 44

Collision Testing: The GJK Algorithm

  • GJK algorithm is an algorithm to check whether a point is inside of a convex hull:

1.

Initialize a set Q with up to d+1 points from C (in d dimensions)

2.

Compute point P of closest to the origin in CH(Q)

3.

If P is the origin; return 0

4.

Reduce Q to the smallest subset Q’ of Q, such that P in CH(Q’)

5.

Let V=SC(–P) be a supporting point in direction –P

6.

If V no more extreme in direction –P than P itself; return ||P||

7.

Add V to Q. Go to step 2

  • Full algorithm at:

http://realtimecollisiondetection.net/pubs/SIGGRAPH04_Ericson_GJK_notes.pdf

  • Following slides from Christer Ericson. Originals at:

http://realtimecollisiondetection.net/pubs/ SIGGRAPH04_Ericson_the_GJK_algorithm.ppt

The most extreme point of a shape in a given direction:

d C

slide-45
SLIDE 45

GJK example 1(10)

C

INPUT: Convex polyhedron C given as the convex hull of a set of points

slide-46
SLIDE 46

1.

Initialize the simplex set Q with up to d+1 points from C (in d dimensions)

GJK example 2(10)

Q

{ }

1 2

, , Q Q Q Q =

C

1

Q

2

Q

slide-47
SLIDE 47

GJK example 3(10)

{ }

1 2

, , Q Q Q Q =

1

Q Q

2

Q P

2. Compute point P of minimum norm in CH(Q)

slide-48
SLIDE 48

GJK example 4(10)

{ }

1 2

, Q Q Q =

1

Q Q P

3.

If P is the origin, exit; return 0

4.

Reduce Q to the smallest subset Q’ of Q, such that P in CH(Q’)

2

Q

slide-49
SLIDE 49

GJK example 5(10)

{ }

1 2

, Q Q Q =

1

Q

2

Q P

5.

Let V=SC(–P) be a supporting point in direction –P

( )

C

V S P = −

slide-50
SLIDE 50

GJK example 6(10)

1

Q

{ }

1 2

, , Q Q Q V =

2

Q V

6.

If V no more extreme in direction –P than P itself, exit; return ||P||

7.

Add V to Q. Go to step 2

slide-51
SLIDE 51

GJK example 7(10)

{ }

1 2

, , Q Q Q V =

1

Q

2

Q V P

2. Compute point P of minimum norm in CH(Q)

slide-52
SLIDE 52

GJK example 8(10)

{ }

2,

Q Q V =

2

Q V P

3.

If P is the origin, exit; return 0

4.

Reduce Q to the smallest subset Q’ of Q, such that P in CH(Q’)

slide-53
SLIDE 53

GJK example 9(10)

2

' ( )

C

S Q V P = − =

{ }

2,

Q Q V =

V P

5.

Let V=SC(–P) be a supporting point in direction –P

slide-54
SLIDE 54

GJK example 10(10)

V P

DONE!

{ }

2,

Q Q V =

6.

If V no more extreme in direction –P than P itself, exit; return ||P||

2

Q

slide-55
SLIDE 55

Moving Bodies

  • Or “bullet through paper” effect:

Frame 1 Frame 2 Frame 3 no collision no collision no collision

If objects move too fast for the size of the

  • bjects in the world, they might go “through”
  • ther objects, without any collision being

detected.

slide-56
SLIDE 56

Moving Bodies: Swept Shapes

  • Swept shape: a new shape formed by the motion of a

primitive shape

  • Some basic shapes generate easy swept shapes:
  • Spheres generate capsules
  • In general, a convex shape generates another convex

shape

slide-57
SLIDE 57

Moving Bodies: Swept Shapes

  • Problems:
  • Non rectilinear movement
  • Rotation

Non convex swept shapes!

slide-58
SLIDE 58

Moving Bodies: Swept Shapes

  • Problems:
  • Non rectilinear movement
  • Rotation

Non convex swept shapes!

Possible solution: use a smaller simulation time interval, and approximate with linear motion

slide-59
SLIDE 59

Moving Bodies

  • Swept shapes offer a quick solution that would work for

95% of the games

  • If more precision is required, there are specialized

algorithms:

  • Based on the Minkowski difference, we can calculate what is the

minimum distance between objects in the world, and calculate what is the maximum time step we can simulate to ensure no “bullet- through-paper” effect.

  • Specialized algorithms: Continuous Collision Detection (CCD)
  • See:

http://www.continuousphysics.com/ BulletContinuousCollisionDetection.pdf

slide-60
SLIDE 60

Outline

  • Student Presentations
  • Collision Detection Basics
  • Collision Shapes
  • Collision Detection Algorithms
  • Optimizations
  • Project Discussion
slide-61
SLIDE 61

Optimizations

  • Temporal coherency:
  • When using GJK algorithm we know the distance between two
  • bjects:
  • If they have not moved more than that amount in a series of frames, no

point to testing collision again.

  • Spatial Partitioning:
  • Divide the world using some hierarchical scheme (for example:
  • cttrees)
  • Each object belongs to some nodes of that partition.
  • Unless two objects belong to the same partition, no need to check

for collision

  • Instead of check for N*N tests (N is number of objects), just go over

the nodes in the octtree, and only check collision between the nodes present at each node.

slide-62
SLIDE 62

Spatial Partitioning Example

9 objects: 72 potential collisions (ignoring background)

slide-63
SLIDE 63

Spatial Partitioning Example

9 objects: 36 potential collisions (ignoring background) After partitioning in 4 areas, only 13 potential collisions 0 objects: 0 potential collisions 1 object: 0 potential collisions 3 objects: 3 potential collisions 5 objects: 10 potential collisions

slide-64
SLIDE 64

Spatial Partitioning Example

9 objects: 36 potential collisions (ignoring background) After partitioning in 4 areas, only 13 potential collisions 0 objects: 0 potential collisions 1 object: 0 potential collisions 3 objects: 3 potential collisions 5 objects: 10 potential collisions

Can be done recursively a few levels, to obtain drastic performance

  • gains. In 2D: quadtrees, in 3D:
  • cttrees (student presentation)
slide-65
SLIDE 65

Optimizations

  • Broad Phase, Midphase and Narrow Phase:
  • Broad phase:
  • Use space partition to determine potential collisions
  • Midphase:
  • Use bounding shapes (spheres, capsules, AABB) of compound shapes

to narrow down potential collisions (e.g. one big AABB for a whole character skeleton)

  • Narrow phase:
  • Check all the individual shapes that are still candidates
slide-66
SLIDE 66

Outline

  • Student Presentations
  • Collision Detection Basics
  • Collision Shapes
  • Collision Detection Algorithms
  • Optimizations
  • Project Discussion
slide-67
SLIDE 67

Libraries For Collision Detection

  • ODE (Open Dynamics Engine):
  • General physics simulation engine
  • Can be used also just for collision detection
  • SWIFT
  • Collision between convex shapes
  • RAPID
  • Collision between convex and non-convex shapes
slide-68
SLIDE 68

Project Topics

  • Compound shape collision detection (using AABB and

midphase narrow-phase detection)

  • Moving shapes collision detection (swept shapes, continuous

collision detection)

  • Collision detection amongst arbitrary convex shapes (GJK

algorithm)

  • Efficient collision detection allowing triangle meshes
  • Etc. (only basic collision between spheres (circles) and boxes

would be way too simple)

slide-69
SLIDE 69

Links to Game Videos

  • This week’s interesting games:
  • Crush: http://www.youtube.com/watch?v=FdGwPQs64Rg
slide-70
SLIDE 70

Remember that next week:

  • First Project Deliverable:
  • Groups
  • Topics
  • Specific algorithms
  • Specific libraries you will use
  • Specific structure of your game engine (not necessarily a class

diagram, see the diagrams shown in class)

  • Demo you will create
  • Submission procedure:
  • Email to (copy both):
  • Santiago Ontañón santi@cs.drexel.edu
  • Stephen Lombardi sal64@drexel.edu
  • Subject: CS480-680 Project Deliverable 1 Group #