collision detection in computer games - kth...the brute-force way is doing collision test for all...

51
Self-Introduction Collision Detection Collision Detection in Computer Games Fangkai Yang 1 1 Computational Science and Technology KTH Royal Institute of Technology Fangkai Yang Collision Detection in Computer Games

Upload: others

Post on 10-Apr-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Collision Detection in Computer Games

Fangkai Yang1

1Computational Science and TechnologyKTH Royal Institute of Technology

Fangkai Yang Collision Detection in Computer Games

Page 2: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Outline

1 Self-IntroductionWho is Fangkai?

2 Collision DetectionLet’s Play a Game!Collision Detection

Fangkai Yang Collision Detection in Computer Games

Page 3: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Who is Fangkai?

Outline

1 Self-IntroductionWho is Fangkai?

2 Collision DetectionLet’s Play a Game!Collision Detection

Fangkai Yang Collision Detection in Computer Games

Page 4: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Who is Fangkai?

Who is Fangkai?

DefinitionFangkai Yang is a crazy Gamer, Mathematician andProgrammer.

PhD candidate in Computer Science.Research on Crowd-simulation andGame AI.Game developer: Just Cause 3(Avalanche Studios), War Rage(NetEase Games).

Fangkai Yang Collision Detection in Computer Games

Page 5: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Collision Detection: A Gentle Introduction

Collision detection concerns the detection of collisions betweenobjects in the virtual environment. Primarily employed to stopobjects moving through each other and the environment.

Collision Detection is everywhere in computer games: betweencharacters and characters, between characters and terrain, etc.

Remember:Physical laws do not exist in the virtual world by default.Must computationally model and program them.

Fangkai Yang Collision Detection in Computer Games

Page 6: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Dayz Standalone

What my friends think I was playing:

Fangkai Yang Collision Detection in Computer Games

Page 7: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Dayz Standalone

What I was actually playing:

Fangkai Yang Collision Detection in Computer Games

Page 8: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Dayz Standalone

What I was actually playing:

Fangkai Yang Collision Detection in Computer Games

Page 9: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

FIFA

What my friends think I was playing:

Fangkai Yang Collision Detection in Computer Games

Page 10: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

FIFA

What I was actually playing:

Fangkai Yang Collision Detection in Computer Games

Page 11: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Basic Geometry Types

Rigid-bodiesHard objects (ideal solid bodies)Constant distance between vertices in the meshConvex vs. concave

Soft-bodiesCloth, for exampleMore complicated to simulateSoft-bodies like cloth may collide with themselves

Fangkai Yang Collision Detection in Computer Games

Page 12: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Basic Geometry Types

Fangkai Yang Collision Detection in Computer Games

Page 13: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Basic Geometry Types

https://www.youtube.com/watch?v=M5Ygpxfmcg8

Fangkai Yang Collision Detection in Computer Games

Page 14: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Outline

1 Self-IntroductionWho is Fangkai?

2 Collision DetectionLet’s Play a Game!Collision Detection

Fangkai Yang Collision Detection in Computer Games

Page 15: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

King of Fighter 97

I want a volunteer!

Fangkai Yang Collision Detection in Computer Games

Page 16: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Question:How to test if a character has been hit?

Fangkai Yang Collision Detection in Computer Games

Page 17: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Outline

1 Self-IntroductionWho is Fangkai?

2 Collision DetectionLet’s Play a Game!Collision Detection

Fangkai Yang Collision Detection in Computer Games

Page 18: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Collision Detection

DefinitionCollision detection refers the detection of the intersection of twoor more objects.

Figure: Collision detection in Street Fighter

Fangkai Yang Collision Detection in Computer Games

Page 19: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Figure: Overlaps of characters.

Fangkai Yang Collision Detection in Computer Games

Page 20: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

How about collision detection in 3D games?

Fangkai Yang Collision Detection in Computer Games

Page 21: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Penetration

When collision is detected, the penetration follows.

Fangkai Yang Collision Detection in Computer Games

Page 22: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Two forms of collision detection:Continuous: very expensive. Simulate solid objects in reallife.Discrete: objects will end up with penetrating each other.

Fangkai Yang Collision Detection in Computer Games

Page 23: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Backtracking

Fangkai Yang Collision Detection in Computer Games

Page 24: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Backtracking

Like Tom Cruise in Edge of Tomorrow, it turns time backwardsand fix the penetration that occurred in the last frame.

Fangkai Yang Collision Detection in Computer Games

Page 25: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Bounding Volume

DefinitionBounding volume for a set of objects is a closed volume thatcompletely contains the union of the objects in the set.

(a) In the game scenario. (b) In the physics engine.

Fangkai Yang Collision Detection in Computer Games

Page 26: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Bounding Volume types

Bounding Box: Axis Aligned Bounding Box (AABB),Oriented Bounding Box (OBB), etc. For objects that restupon other, such as a car resting on the ground, using abounding box is a better choice.Bounding Sphere: They are very quick to test for collisionwith each other.

Fangkai Yang Collision Detection in Computer Games

Page 27: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Sphere Collision Detection

Circles are colliding if D <= (R1 + R2)d is the distance between the circle centersR1 and R2 are the radii of both circles respectively

Fangkai Yang Collision Detection in Computer Games

Page 28: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

AABB Collision Detection

Recall: Faces of an AABB are aligned with the coordinatesaxes.To see if A and B overlap, separating axes test can beused along the x,y and z axes.This is faster, as only need to search x,y and z axes.

Fangkai Yang Collision Detection in Computer Games

Page 29: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Some Problems

Naive collision detection approaches may simply test if twoobjects are overlapping at every time-step.Problem: quickly moving objects can pass right througheach other without detection

(b) First test. (c) Second test.

Fangkai Yang Collision Detection in Computer Games

Page 30: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Swept Volume

The AABB in the game bounds not the object, but the SweptVolume of the object from one frame to the next.

Fangkai Yang Collision Detection in Computer Games

Page 31: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Broad Phase and Narrow Phase

Most efficient implementations use a two-phase approach: abroad phase followed by a narrow phase.

Broad phase: collision tests are based on boundingvolumes only. Quickly prune away pairs of objects that donot collide with each other. The output is the potentiallycolliding set of pairs of objects.Narrow phase: collision tests are exact−they usuallycompute exact contact points and normals−thus muchmore costly, but performed for only the pairs of thepotentially colliding set.

Fangkai Yang Collision Detection in Computer Games

Page 32: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Broad Phase

Figure: Collision detection between AABBs (Axis Aligned BoundingBox).

Fangkai Yang Collision Detection in Computer Games

Page 33: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Broad Phase

The broad phase collision detection helps to filtrate thepotentially colliding object sets.The brute-force way is doing collision test for all pairs ofobjects, and the complexity is O(n2).

(a) Sweep and prune. (b) Spatial subdivision.

Fangkai Yang Collision Detection in Computer Games

Page 34: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Narrow Phase

Separating Axis Tests

Many algorithms are based on the separating hyperplanetheorem

Figure: Two disjoint convex sets are separable by a hyperplane.

Separation w.r.t. a plane P => separation of the orthogonalprojections onto any line L parallel to plane normal

Fangkai Yang Collision Detection in Computer Games

Page 35: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Separating Axis Tests

Think of it like this: do a twist to simplify calculations.

Separating axis is a line for which the projection intervals do notoverlap.

Fangkai Yang Collision Detection in Computer Games

Page 36: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Separating Axis Tests

Separated if Amax < Bmin or Bmax < Amin

Question:Which axes to be tested?

Fangkai Yang Collision Detection in Computer Games

Page 37: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Separating Axis Tests

Axes to test:

Fangkai Yang Collision Detection in Computer Games

Page 38: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Convex versus Convex Collision Detection

One of the most commonly used algorithm in game engine isGJK (Gilbert-Johnson-Keerthi) algorithm. This algorithm relieson a support function to iteratively get closer simplices to thesolution using Minkowski difference.

(a) Two shapes collide on one vertex. (b) No collision.

Fangkai Yang Collision Detection in Computer Games

Page 39: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

GJK

Given the two polyhedra:Computes the distance d between themCan also return the closest pair of points on each polygons

Fangkai Yang Collision Detection in Computer Games

Page 40: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Support Mapping Function

Supporting point. This is an extreme point on the polygonfor a given direction d.The support mapping function returns this point

Fangkai Yang Collision Detection in Computer Games

Page 41: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

The first simplex is built using what is called a support function.Support function returns one point inside the Minkowskidifference given two sets KA and KB.

Figure: The first two vertices in the first simplex.

Fangkai Yang Collision Detection in Computer Games

Page 42: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

An Example

Fangkai Yang Collision Detection in Computer Games

Page 43: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

An Example

Select direction d = (1,0):p1 = (9,9);p2 = (5,7);p3 = p1− p2 = (4,2);

Fangkai Yang Collision Detection in Computer Games

Page 44: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

An Example

Select direction d = (−1,0):p1 = (4,5);p2 = (12,7);p3 = p1− p2 = (−8,−2);

Fangkai Yang Collision Detection in Computer Games

Page 45: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

An Example

Select direction d = (0,1):p1 = (4,11);p2 = (10,2);p3 = p1− p2 = (−6,9);

Fangkai Yang Collision Detection in Computer Games

Page 46: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

An Example

The Simplex doesn’t contain the origin, but we know that thetwo shapes are intersecting. The problem here is that our firstguess (at choosing directions) didn’t yield a Simplex thatenclosed the origin.

Fangkai Yang Collision Detection in Computer Games

Page 47: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

An Example

Select direction d = (0,−1):p1 = (4,5);p2 = (5,7);p3 = p1− p2 = (−1,−2);

Fangkai Yang Collision Detection in Computer Games

Page 48: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

An Example

Now we contain the origin and can determine that there is acollision

Fangkai Yang Collision Detection in Computer Games

Page 49: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

An Example

Fangkai Yang Collision Detection in Computer Games

Page 50: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

(a) The first iteration. (b) New simplex doesnot contain the origin.

(c) The seconditeration.

(d) New simplexcontains the origin.

Fangkai Yang Collision Detection in Computer Games

Page 51: Collision Detection in Computer Games - KTH...The brute-force way is doing collision test for all pairs of objects, and the complexity is O(n2). (a) Sweep and prune. (b) Spatial subdivision

Self-IntroductionCollision Detection

Let’s Play a Game!Collision Detection

Thank you for Listening and Sleeping!

Fangkai Yang Collision Detection in Computer Games