GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665...

22
GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III

Transcript of GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665...

Page 1: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

GPU Broad Phase Collision DetectionGPU GraphicsGary J. Katz

University of Pennsylvania CIS 665

Adapted from articles taken from GPU Gems III

Page 2: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Basic Collision Detection Broad Phase

Reviews the whole simulation Looks at coarse view of objects (usually bounding boxes) Determines if objects may intersect Fast

Narrow Phase Reviews objects that may intersect (determined by broad phase) Looks at detailed view of objects Determines if objects actually intersect Slow

Page 3: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Current Broad Phase Methods Brute Force:

n objects need n(n-1)/2 collision tests Complexity = O(n2)

Sort and Sweep: Average Complexity = O(n log n) Worst-Case Complexity O(n2)

Spatial Subdivision: Average Complexity = O(n log n) Worst-Case Complexity O(n2)

Page 4: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Sort and Sweep Bounding volume

is projected onto x, y, z axis

Determine collision interval for each object [bi, ei]

Two objects who’s collision intervals do not overlap can not collide

O1

O3

O2

Sorting AxisB1 B3 E1 B2 E3 E2

Page 5: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Sort and Sweep

Sorted List: B1 B3 E1 B2 E3 E2

Active Objects:

Add an object i to the active objects list when Bi is reached and remove when Ei is reached

Check for intersection between object i and all other object in the active objects list at the time Bi is reached

Objects to compare against:1:2: 3:

1

1

3

3

2

O1

O3

O2

B1 B3E1 B2 E3 E2

Page 6: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Spatial Subdivision

1 2

3 4

5 6

7 8

Images from pg 699, 700 GPU Gems III

O1

O2

O3

O4

1 2 3 4

5 6 7 8

Example

Page 7: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Parallel Spatial Subdivision Complications:

1. Single object can be involved in multiple collision tests

2. Need to prevent multiple threads updating the state of an object at the same time

Ways to solve this?

Page 8: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Guaranteed Individual Collision Tests Prove: No two cells updated in parallel may

contain the same object that is being updated Constraints

1. Each cell is as large as the bounding volume of the largest object

2. Each cell processed in parallel must be separated by each other cell by at least one intervening cell

In 2d this takes _____ number of passes In 3d this takes _____ number of passes

4

8

Page 9: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Example of Parallel Spatial Subdivision

O1

O2

O3

O4

1 2 1 2

3 4 3 4O1

O2

O3

O4

1 2 1 2

3 4 3 4

Page 10: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Avoiding Extra Collision Testing1. Associate each object a set of control bits to

test where its centroid resides

2. Scale the bounding sphere of each object by sqrt(2) to ensure the grid cell is at least 1.5 times larger than the largest object

1 2 1 2

3 4 3 4

Case 1Case 2

Page 11: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Implementing in CUDA Store list of object IDs, cell IDs in device

memory Build the list of cell IDs from object’s

bounding boxes Sorting list from previous step Build an index table to traverse the sorted list Schedule pairs of objects for narrow phase

collision detection

Page 12: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

InitializationCell ID Array

OBJ 1 Cell ID 1OBJ 1 Cell ID 2OBJ 1 Cell ID 3OBJ 1 Cell ID 4OBJ 2 Cell ID 1OBJ 2 Cell ID 2OBJ 2 Cell ID 3OBJ 2 Cell ID 4...

Object ID Array

OBJ 1 ID, Control BitsOBJ 1 ID, Control BitsOBJ 1 ID, Control BitsOBJ 1 ID, Control BitsOBJ 2 ID, Control BitsOBJ 2 ID, Control BitsOBJ 2 ID, Control BitsOBJ 2 ID, Control Bits...

Page 13: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Construct the Cell ID ArrayHost Cells (H – Cells)

Contain the centroid of the object

Phantom Cells (P-Cells)Overlap with bounding volume but do not contain the centroid

H-Cell Hash = (pos.x / CELLSIZE) << XSHIFT) | (pos.y / CELLSIZE) << YSHIFT) | (pos.z / CELLSIZE) << ZSHIFT)

P P P

P H P

P P P

P-Cells – Test the 3d-1 cells surrounding the H cellThere can be as many as 2d-1 P cells

Page 14: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Sorting the Cell ID Array What we want:

Sorted by Cell ID H cells of an ID occur before P cells of an ID

Starting with a partial sort H cells are before P cells, but array is not sorted by Cell

ID Solution:

Radix Sort Radix Sort ensures identical cell IDs remain in the same

order as before sorting.

Page 15: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Sorting Cell Array

0100

0111

1112

1013

0214

021n

0200

1102

1003

0114

011n

0110

1002

021n

0210

0002

111n

0012

022n

1012

0112

0102

...

Cell ID Array

0002

011n

1013

0012

0200

1012

0100

0214

1102

0102

021n

1112

0111

0210

111n

0110

022n

111n

0112

1002

102n

0114

1003

1033

...

Sorted Cell ID Array

0111

1002

Invalid Cell

Home Cell

Phantom Cell

1033 Object ID

Cell ID

Legend

Page 16: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Spatial Subdivision

1 2

3 4

5 6

7 8

Images from pg 699, 700 GPU Gems III

O1

O2

O3

O4

1 2 3 4

5 6 7 8

Example

1. Assign to each cell the list of bounding volumes whose objects intersect with the cell

2. Perform Collision test only if both objects are in the cell and one has a centroid in the cell

Page 17: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Create the Collision Cell List Scan sorted cell ID array for changes of cell ID

Mark by end of the list of occupants of one cell and beginning of another

1. Count number of objects each collision cell contains and convert them into offsets using scan

2. Create entries for each collision cell in new array1. Start

2. Number of H occupants

3. Number of P occupants

Page 18: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Create Collision Cell List

0002

011n

1013

0012

0200

1012

0100

0214

1102

0102

021n

1112

0111

0210

111n

0110

022n

111n

0112

1002

102n

0114

1003

1033

...

Sorted Cell ID Array Cell Index & Size Array

21 1

41 4

102 1 ...

IDH P

ID = Cell index in sorted Cell ID ArrayH = Number of Home Cell IDsP = Number of Phantom Cell IDs

Page 19: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Traverse Collision Cell ListCell Index & Size Array

21 1

41 4

102 1 ...

161 1

191 1

Xp q

T 0 T 1 T 2 ...T 3 T 4 T n

Perform Collision Test Per Cell

Number of Collisions / Thread Array

0 1 0 ...2 1 …

Page 20: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Credits Based upon GPU Gems article Chapter 32 Chapter Author: Scott Le Grand This presentation was put together without the

approval of the author and should only be used for educational purposes

Page 21: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Backup

Page 22: GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III.

Spatial Subdivision Partition space

into uniform grid

Grid cell is at least as large as largest object

Each cell contains list of each object whose centroid is in the cell

Collision tests are performed between objects who are in same cell or adjacent cells

1 2

3 4

5 6

7 8

Images from pg 699, 700 GPU Gems III

O1

O2

O3

O4

Implementation:1. Create list of object IDs along

with hashing of cell IDs in which they reside

2. Sort list by cell ID3. Traverse swaths of identical cell

IDs4. Perform collision tests on all

objects that share same cell ID

1 2 3 4

5 6 7 8

Example