Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic...

129
Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone! Welcome to my talk on dynamic bounding volume hierarchies. 1

Transcript of Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic...

Page 1: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Dynamic Bounding Volume Hierarchies

Erin Catto, Blizzard Entertainment

Hello everyone! Welcome to my talk on dynamic bounding volume hierarchies.

1

Page 2: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

This is one of my favorite Overwatch maps: BlizzardWorld. This is the spawn area inside the Hearthstone Tavern.

2

Page 3: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

All the objects in the map, the floors, the walls, the chairs are objects that are enclosed in axis aligned bounding boxes. This is done to accelerate collision detection.

3

Page 4: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Even the balloons and their strings have separating bounding boxes.

4

Page 5: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

There are almost 9000 separate collision objects in the editor. Green boxes are static objects, blue kinematic, and red dynamic.

5

Page 6: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Here is a zoomed out view of all the bounding boxes in the map.

6

Page 7: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Axis Aligned Bounding Box (AABB)

struct AABB{

Vec3 lowerBound;Vec3 upperBound;

};

lower bound

upper bound

x

y

Here is the definition of an a bounding box that I will use.

7

Page 8: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

AABB Union(AABB A, AABB B){

AABB C;C.lowerBound = Min(A.lowerBound, B.lowerBound);C.upperBound = Max(A.upperBound, B.upperBound);return C;

}

Union of two AABBs

𝐶 = 𝐴 ∪ 𝐵

A

B

C

Given two bounding boxes we can compute the union with min and max operations. These can be made efficient using SIMD.

Notice the cup notation. You will see it again.

8

Page 9: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Surface area of an AABB

float Area(AABB A){

Vec3 d = A.upperBound – A.lowerBound;return 2.0f * (d.x * d.y + d.y * d.z + d.z * d.x);

}

𝑆𝐴(𝐴)

I will also need to compute the surface area. Notice the SA notation. I will use that as well.

9

Page 10: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

A game world with several geometric objects

Game worlds often have many objects. Players, rigid bodies, wall, floors, etc.

This is an abstract example of a game world with several geometric objects.

10

Page 11: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

A ray-cast against the game world

Often in games we need to ray cast against the scene. To shoot a weapon, check visibility, find the ground, etc.

11

Page 12: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

hit point

normal vector

Typically we want the hit point, normal vector, and some way of identifying the object that was hit. Detailed ray cast results are not in the scope of this presentation.

However, I am going to discuss ways to make ray casting faster.

12

Page 13: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

p1

p2bool BruteForceRayCast(Vec3 p1, Vec3 p2){

for (Object object : objects){

bool hit = RayCast(object, p1, p2);if (hit){

return true;}

}return false;

}

The simplest way to perform the ray cast is to check each object. Brute force can be slow if there are many objects. Sometimes cache friendly is not enough.

13

Page 14: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Enclose each object with an AABB

Suppose the shapes are complex. Then it might be worth checking whether the ray intersects the AABB before testing the more complex object. Also the AABB test can be faster because we don’t need the hit point or normal. We just a need to know if the ray overlaps the AABB.

14

Page 15: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

bool RayCastTestAABB(Vec3 p1, Vec3 p2){

for (Object object : objects){

if (TestOverlap(object.aabb, p1, p2) == false){

continue;}

bool hit = RayCast(object, p1, p2);if (hit){

return true;}

}return false;

}

Here is the algorithm for going over every object, but first testing if the ray overlaps the bounding box. Again, I don’t have time to get into the details of the detailed ray cast or the bounding box overlap test.

15

Page 16: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Two Level AABB Hierarchy

group 3

group 2

group 1

Can quickly rule out groups 1 and 3

We can try grouping some objects together inside larger bounding boxes. Then we can skip whole groups in many cases.

16

Page 17: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Bounding Volume Hierarchy (BVH)

The bounding volumes are AABBs

Other shapes are possible

Once you use more than one level of bounding boxes, you might as well make a whole tree. This is a bounding volume hierarchy. Here the bounding volumes are AABBs. Other shapes are possible, such as bounding spheres.

17

Page 18: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

binary tree representation

: internal node

: leaf node

32

1

A B

H

G

54 76

98C D E F

I

J K10

𝑁𝑖𝑛𝑡𝑒𝑟𝑛𝑎𝑙 = 𝑁𝑙𝑒𝑎𝑣𝑒𝑠 − 1

I’m using a binary tree for the BVH. This is efficient and easy to implement.

The tree consists of internal nodes and leaf nodes. The leaf nodes are the collision objects and the internal nodes only exist to accelerate collision queries.

Recall that the number of internal nodes can be computed from the number of leaf nodes, regardless of the tree structure.

18

Page 19: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

struct Node{

AABB box;int objectIndex;int parentIndex;int child1;int child2;bool isLeaf;

};

struct Tree{

Node* nodes;int nodeCount;int rootIndex;

};

The code for the binary tree consists of a node structure and a tree structure. The node has an object index that relates it to the game object it contains.

19

Page 20: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

bool TreeRayCast(Tree tree, Vec3 p1, Vec3 p2){

Stack<int> stack;Push(stack, tree.rootIndex);while (IsEmpty(stack) == false){

int index = Pop(stack);if (TestOverlap(tree.nodes[index].box, p1, p2) == false){

continue;}

if (nodes[index].isLeaf){

int objectIndex = tree.nodes[index].objectIndex;if (RayCast(objects[objectIndex], p1, p2)){

return true;}

}else{

Push(stack, tree.nodes[index].child1);Push(stack, tree.nodes[index].child2);

}}return false;

}

Here’s an example of ray casting against a binary tree BVH. This code uses a local stack instead of using functional recursion. Children get pushed onto the stack until no more candidates are left.

This is just an example. It leaves out several important optimizations.

20

Page 21: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Equivalent Trees

Order of children doesn’t matter

23

1

AB

H

G

5 476

98 C DE F

I

J K10

32

1

A B

H

G

54 76

98C D E F

I

J K10

The binary tree can take a number of forms, depending on the how the BVH is built. For a BVH the ordering of nodes is not relevant. We can swap left and right children at any level and still have the same BVH.

For example, these two trees are equivalent. This is different than other binary trees, such as Red Black trees, where the order matters.

21

Page 22: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Building a BVH

• Bottom Up

• Top Down

• Incremental

Bounding volume hierarchies can be built automatically using many different algorithms. These can be broadly classified as bottom up, top down, or incremental. Each approach can be useful, depending on the situation.

22

Page 23: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Bottom Up(amalgamation)

23

Page 24: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Bottom Up

B

K

A

D

G

H

F

E

C

J

I

Start with many loose leaf nodes.

24

Page 25: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Bottom Up

A B

D

C

E

F

G

H I

JK

Hide the shapes, just consider the AABBs

Let’s hide the underlying shapes for clarity.

25

Page 26: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

1

A B

Bottom Up

A B

D

C

E

F

G

H I

JK

Merge two AABBs that are close

𝑛𝑜𝑑𝑒1 = 𝐴 ∪ 𝐵

Combine two of the nodes under a single internal node.

26

Page 27: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

1

A B

Bottom Up

D

C

E

F

G

H I

JK

1

Remove A and B from the working set

Now drop A and B from the working set and replace them with box 1.

27

Page 28: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

2

H I

Bottom Up

D

C

E

F

G

H I

JK

1

1

A B

Combine two more.

28

Page 29: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

1

A B

2

H I

Bottom Up

2

D

C

E

F

GJ

K

1

Drop H and I from the working set and introduce box 2.

29

Page 30: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

1

A B

3

C D

4

E F 5

J K

2

H I

Bottom Up

2

D

C

E

F

GJ

K

1

And so on.

30

Page 31: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Bottom Up

3

4

5

2

G

1

1

A B

3

C D

4

E F 5

J K

2

H I

And so on.

31

Page 32: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

1

A B

3

C D

4

E F

7

G 5

J K

2

H I

6

Bottom Up

3

4

5

2

G

1

Keep combining top level nodes.

32

Page 33: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Bottom Up

6

7

4

2

1

A B

3

C D

4

E F

7

G 5

J K

2

H I

6

Keep combining top level nodes.

33

Page 34: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

1

A B

3

C D

4

E F

7

G 5

J K

2

H I

9

6

8

Bottom Up

6

7

4

2

Each time nodes are combined, there are less top level nodes.

34

Page 35: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

9

8

Bottom Up

1

A B

3

C D

4

E F

7

G 5

J K

2

H I

9

6

8

Bottom up gets very fast at the upper levels of the tree.

35

Page 36: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

1

A B

3

C D

4

E F

7

G 5

J K

2

H I

9

6

8

Bottom Up

10

9

8

Until there is only a single top level node. Then the tree is complete.

36

Page 37: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Done!

1

A

10

B

3

C D

4

E F

7

G 5

J K

2

H I

9

6

8

Bottom Up

10

Now the tree is built.

37

Page 38: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Top Down(divide and conquer)

38

Page 39: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Top Down

B

K

A

D

G

H

F

E

C

I

J

Start with many loose leaf nodes.

39

Page 40: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Top Down

A B

D

C

E

F

G

H I

JK

Start with many loose leaf nodes.

40

Page 41: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

A,B,C,D,E,F,G,H,I,J,K

1

Top Down

A B

D

C

E

F

G

H I

JK

Wrap all boxes under a single root box. There are too many children, so we need to split them up.

41

Page 42: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

A,B,C,D,E F,G,H,I,J,K

32

1

Top Down

A B

D

C

E

F

G

H I

JK

Divide the children to two groups. There are still too many children. We want one object per leaf.

42

Page 43: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

4

A B

7

C,D,E F,J,K

6

G,H,I

3

5

2

1

Top Down

A B

D

C

E

F

G

H I

JK

And divide their children into two groups.

43

Page 44: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

10

4

A B 8

C D

7

E F

6

G9

J K H I

3

5

2

1

Done!

Top Down

A B

D

C

E

F

G

H I

JK

And keep dividing until every child has a single object.

44

Page 45: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Incremental(dynamic)

45

Page 46: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

J

J

Incremental

An incremental build adds one AABB to the tree at a time.

46

Page 47: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

C

J

C J

1

As objects are added, they are inserted into the tree.

47

Page 48: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

F

C

J

C 2

F J

1

Visually you can start to guess what choices the insertion algorithm should make.

48

Page 49: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

F

E

C

J

C E

2

F J

3

1

When a leaf is inserted, the code needs to find a sibling for it. The sibling can be a leaf node or an internal node.

49

Page 50: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

A

F

E

C

J

4

A C

E

2

F J

3

1

50

Page 51: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

A

H

F

E

C

J

4

A C

E H

2

5

F J

3

1

And so on …

51

Page 52: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Dynamic AABB Tree

• Moving objects

• Object creation and destruction

• Streaming

52

Page 53: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Insertion AlgorithmKey algorithm for dynamic bounding volume hierarchies

The key algorithm for dynamic bounding volume hierarchies is the algorithm for inserting leaves. So I’m going to spend a lot of time on this. Leaf removal is straight forward and is not covered.

53

Page 54: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

void InsertLeaf(Tree tree, int objectIndex, AABB box){

int leafIndex = AllocateLeafNode(tree, objectIndex, box);if (tree.nodeCount == 1){

tree.rootIndex = leafIndex;return;

}

// Stage 1: find the best sibling for the new leaf

// Stage 2: create a new parent

// Stage 3: walk back up the tree refitting AABBs}

Here is the structure of the insertion algorithm.

54

Page 55: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

// Stage 1: find the best sibling for the new leaf

int bestSibling = 0;for (int i = 0; i < m_nodeCount; ++i){

bestSibling = PickBest(bestSibling, i);}

TBD

Stage 1 descends the tree, looking for the best option for a sibling. I’ll be talking a lot about how to find the best sibling.

55

Page 56: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

// Stage 2: create a new parentint oldParent = tree.nodes[sibling].parentIndex;int newParent = AllocateInternalNode(tree);tree.nodes[newParent].parentIndex = oldParent;tree.nodes[newParent].box = Union(box, tree.nodes[sibling].box);

if (oldParent != nullIndex){

// The sibling was not the rootif (tree.nodes[oldParent].child1 == sibling){

tree.nodes[oldParent].child1 = newParent;}else{

tree.nodes[oldParent].child2 = newParent;}

tree.nodes[newParent].child1 = sibling;tree.nodes[newParent].child2 = leafIndex;tree.nodes[sibling].parentIndex = newParent;tree.nodes[leafIndex].parentIndex = newParent;

}else{

// The sibling was the roottree.nodes[newParent].child1 = sibling;tree.nodes[newParent].child2 = leafIndex;tree.nodes[sibling].parentIndex = newParent;tree.nodes[leafIndex].parentIndex = newParent;tree.rootIndex = newParent;

}

Stage 2 deals with all the details of modify the tree after a sibling has been chosen. Edge cases must be handled.

56

Page 57: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

// Stage 3: walk back up the tree refitting AABBsint index = tree.nodes[leafIndex].parentIndex;while (index != nullIndex){

int child1 = tree.nodes[index].child1;int child2 = tree.nodes[index].child2;

tree.nodes[index].box = Union(tree.nodes[child1].box, tree.nodes[child2].box);index = tree.nodes[index].parentIndex;

}

Stage 3 adjusts the AABBs of the new leaf’s ancestors. This is called refitting.

57

Page 58: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Stage 1: Look for best sibling

H found to be the best sibling for new leaf L

32

1

A B

H

G

54 76

98C D E F

I

J K10

L

Here is what a sibling search might look like in Stage 1.

58

Page 59: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Stage 2: Create new internal node

Create internal node 11, parent for H and L

32

1

A B

H

G

54 76

98C D E F

I

J K10

L

11

Stage 2 handles the creation of a new node and hooking the nodes together.

59

Page 60: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Stage 3: Refit ancestor AABBs

Ensure all nodes enclose their children

32

1

A B

H

G

54 76

98C D E F

I

J K10

L

11

Stage 3 walks back up the tree and refits the parent bounding boxes.

60

Page 61: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Building a better tree

• Bottom Up• merge AABBs with closest centers

• new node with minimum surface area/volume

• Top Down• spatial median split

• minimize volume/area of children

• Incremental• choose sibling with closest center

• new node with minimum surface area/volume

Up until now the trees in my examples have been build somewhat arbitrarily. An BVH builder should have rules to yield an efficient tree.

Here are some rules that can be found in practice.

61

Page 62: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Top Down Examplemedian split versus surface area

Some choices are better than others. Here is an example of a top down build. I’m going to compare two splitting choices. The median split and a split that tries to minimize surface area.

62

Page 63: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

spatial median split minimize node surface area

Top Down

Here are two possible rules for a top down split.

Which do you think is better?

63

Page 64: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

spatial median split minimize node surface area

Top Down

64

Page 65: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

spatial median split minimize node surface area

Top Down

Wrap AABBs around the objects on both sides of the split.

65

Page 66: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

spatial median split minimize node surface area

Top Down

more empty space

Minimizing surface area creates more empty space. This leads to more early outs for ray casts.

66

Page 67: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

The Surface Area Heuristic

67

Page 68: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

The probability of a ray hitting a convex object is proportional to the surface area

Surface Area Heuristic

The surface area heuristic is a powerful metric that can be used to drive the construction of a BVH. The idea is that the probability of a ray hitting an object is proportional to the surface are of the object.

68

Page 69: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

The probability of a ray hitting a convex object is proportional to the surface area

Surface Area Heuristic

We can use this to build good BVHs

69

Page 70: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Cost function of a tree

𝐶 𝑇 =

𝑖∈𝑁𝑜𝑑𝑒𝑠

𝑆𝐴(𝑖)

float ComputeCost(Tree tree){

float cost = 0.0f;for (int i = 0; i < tree.nodeCount; ++i){

cost += Area(tree.nodes[i].box);}return cost;

}

Using the surface area function we can compute a cost metric for any tree.

70

Page 71: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

How shall we compare trees?

versusCB

A

C

B

A

leaves have the same area

D E F G

D E

F

G

We want to a way to compare trees using the surface area heuristic. That way we can see which one is better.

71

Page 72: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

How shall we compare trees?

leaves have the same area

roots have the same areaversusCB

A

C

B

A

D E F G

D E

F

G

Many trees can be built from the same set of leaves. The surface area of the leaves is the same and the surface area of the root node is the same. Only the surface area of the internal nodes varies.

72

Page 73: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

How shall we compare trees?

leaves have the same area

𝑆𝐴 𝐶 = 𝑆𝐴(𝐹 ∪ 𝐺) 𝑆𝐴 𝐶 = 𝑆𝐴(𝐵 ∪ 𝐺)

roots have the same area

C has a different area!

versusCB

A

C

B

A

D E F G

D E

F

G

Many trees can be built from the same set of leaves. The surface area of the leaves is the same and the surface area of the root node is the same. Only the surface area of the internal nodes varies.

73

Page 74: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Revised cost function

𝐶 𝑇 =

𝑖∈𝐼𝑛𝑛𝑒𝑟 𝑁𝑜𝑑𝑒𝑠

𝑆𝐴(𝑖)

Leaf area doesn’t matter

float ComputeCost(Tree tree){

float cost = 0.0f;for (int i = 0; i < tree.nodeCount; ++i){

if (tree.nodes[i].isLeaf == false){

cost += Area(tree.nodes[i].box);}

}return cost;

}

The cost of a tree is the total surface area of the internal nodes. This gives us an objective way to compare the quality of two trees.

74

Page 75: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Is the SAH good?

1. Can be applied to top down, bottom up, and incremental

2. Correlates well with ray cast performance

3. Long ray cast performance is crucial

4. Objective metric for comparing trees

75

Page 76: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Is the SAH good?

So yeah, pretty good!

1. Can be applied to top down, bottom up, and incremental

2. Correlates well with ray cast performance

3. Long ray cast performance is crucial

4. Objective metric for comparing trees

76

Page 77: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

SAH insertion cost

𝐶𝐻 = 𝑆𝐴 11 + ∆𝑆𝐴 10 + ∆𝑆𝐴 8+ ∆𝑆𝐴 7 + ∆𝑆𝐴 3 + ∆𝑆𝐴 1

The cost of choosing H as the sibling for L

∆𝑆𝐴 𝑛𝑜𝑑𝑒 = 𝑆𝐴 𝑛𝑜𝑑𝑒 ∪ 𝐿 − 𝑆𝐴(𝑛𝑜𝑑𝑒)new node

32

1

A B

H

G

54 76

98C D E F

I

J K10

L

11

Going back to the earlier example where I inserted L into the tree and created node 11.SAH gives us a way to compute the cost of inserting L. The cost is the area of the new parent node 11 plus the increased surface area of all the ancestors.This is the surface area added to the tree.

77

Page 78: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Global search for optimum

Sibling choices:• internal nodes 1-10• leaf nodes A-K

2N-1 choices

32

1

A B

H

G

54 76

98C D E F

I

J K10

L

Every node in the tree is a potential sibling for leaf node L. Each choice adds a different surface area to the tree.

I would like to find the sibling that adds the least surface area to tree.

78

Page 79: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Global search for optimum

Sibling choices:• internal nodes 1-10• leaf nodes A-K

Expensive!

2N-1 choices

32

1

A B

H

G

54 76

98C D E F

I

J K10

L

Unfortunately it is expensive to evaluate the cost of every potential sibling.

79

Page 80: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Branch and Bound AlgorithmOptimize the global search

Branch and bound is a powerful algorithm that makes the global search faster.

80

Page 81: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Main idea of branch and bound

• Search through tree recursively

• Skip sub-trees that cannot possibly be better

81

Page 82: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Branch and Bound

Recurse through tree, looking for lowest cost sibling S for L

Use a priority queue Q to explore best candidates first

Initialize:

𝐶𝑏𝑒𝑠𝑡 = 𝑆𝐴(1 ∪ 𝐿)

𝑆𝑏𝑒𝑠𝑡 = 1

𝑄 = {1}

32

1

A B

H

G

54 76

98C D E F

I

J K10

L

Here is a example of how branch and bound works.

82

Page 83: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Branch and Bound

Suppose the search reaches node 7

32

1

A B

H

G

54 76

98C D E F

I

J K10

L

Suppose we are exploring this tree, looking for the best sibling. We find our way to node 7 and want to determine if node 7 has the best cost. We also want to determine if it is worthwhile to explore the children of node 7.

83

Page 84: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Branch and Bound

𝐶7 = 𝑆𝐴 𝐿 ∪ 7 + ∆𝑆𝐴 3 + ∆𝑆𝐴(1)

direct cost inherited cost

Cost for choosing node 7

𝐿 ∪ 7

tentative new parent node for L and 7

32

1

A B

H

G

54 76

98C D E F

I

J K10

L

The cost of node 7 is the sum of the direct cost and the inherited cost.

The direct cost is the surface area of the new internal node that will be created for the siblings.The inherited cost is the increased surface area caused by refitting the ancestor’s boxes.

84

Page 85: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Branch and Bound

𝑖𝑓 𝐶7 < 𝐶𝑏𝑒𝑠𝑡 𝑡ℎ𝑒𝑛 𝐶𝑏𝑒𝑠𝑡 = 𝐶7

𝐶7 = 𝑆𝐴 𝐿 ∪ 7 + ∆𝑆𝐴 3 + ∆𝑆𝐴(1)

Cost for choosing node 7

𝐿 ∪ 7

tentative new parent node for L and 7

32

1

A B

H

G

54 76

98C D E F

I

J K10

L

If the cost of node 7 is better than the best cost then update the best cost.

85

Page 86: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Branch and Bound

𝐶𝑙𝑜𝑤 = 𝑆𝐴 𝐿 + ∆𝑆𝐴 7 + ∆𝑆𝐴 3 + ∆𝑆𝐴(1)

lower bound cost for nodes 8 and 9

Consider pushing 8 and 9 onto the queue𝐿

smallest possible parent node for L and any node

32

1

A B

H

G

54 76

98C D E F

I

J K10

L

Is it worthwhile to explore the sub-tree of node 7?

A lower bound for children of node 7 is the surface area of Q plus the inherited cost (including 7).

86

Page 87: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Branch and Bound

𝑖𝑓 𝐶𝑙𝑜𝑤 < 𝐶𝑏𝑒𝑠𝑡 𝑡ℎ𝑒𝑛 𝑝𝑢𝑠ℎ 8 𝑎𝑛𝑑 𝑝𝑢𝑠ℎ(9)

𝐶𝑙𝑜𝑤 = 𝑆𝐴 𝐿 + ∆𝑆𝐴 7 + ∆𝑆𝐴 3 + ∆𝑆𝐴(1)

𝐿

smallest possible parent node for L and any node

32

1

A B

H

G

54 76

98C D E F

I

J K10

L

If the lower bound cost for the children is lower than the best cost, then it is worth exploring those sub-trees and they are pushed onto the priority queue.

87

Page 88: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Branch and Bound

Otherwise we can prune the sub-tree at node 7 from the search

𝑖𝑓 𝐶𝑙𝑜𝑤 < 𝐶𝑏𝑒𝑠𝑡 𝑡ℎ𝑒𝑛 𝑝𝑢𝑠ℎ 8 𝑎𝑛𝑑 𝑝𝑢𝑠ℎ(9)

𝐶𝑙𝑜𝑤 = 𝑆𝐴 𝐿 + ∆𝑆𝐴 7 + ∆𝑆𝐴 3 + ∆𝑆𝐴(1)

𝐿

smallest possible parent node for L and any node

32

1

A B

H

G

54 76

98C D E F

I

J K10

L

Otherwise can prune the whole sub-tree rooted at node 7 from the search. This drastically improves performance.

88

Page 89: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Object Movement

89

Page 90: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Object movement strategies

• Refit ancestors

90

Page 91: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Object movement strategies

• Refit ancestors• leads to low quality trees

91

Page 92: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Object movement strategies

• Refit ancestors• leads to low quality trees

• Rebuild subtrees

92

Page 93: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Object movement strategies

• Refit ancestors• leads to low quality trees

• Rebuild subtrees• expensive (similar to garbage collection)

93

Page 94: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Object movement strategies

• Refit ancestors• leads to low quality trees

• Rebuild subtrees• expensive (similar to garbage collection)

• Remove/re-insert

94

Page 95: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Object movement strategies

• Refit ancestors• leads to low quality trees

• Rebuild subtrees• expensive (similar to garbage collection)

• Remove/re-insert• also expensive, but spread out

95

Page 96: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Object movement strategies

• Refit ancestors• leads to low quality trees

• Rebuild subtrees• expensive (similar to garbage collection)

• Remove/re-insert• also expensive, but spread out

choose remove/re-insert

96

Page 97: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Enlarged AABBs

• At 60Hz objects often don’t move far per frame

• So use an enlarged AABB in the BVH

• Only update a leaf if the tight fitting AABB moves outside of the enlarged AABB

A enlarged AABBtight AABB

There are many schemes for enlarging the AABB. Choose one that works well for your game.

97

Page 98: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Problem: sorted input

A problem remains. Sorted input can wreck the dynamic tree.

98

Page 99: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

Imagine we have several game objects in a row. They are inserted into the tree in sorted order. We cannot disallow this because this is what the game may need to do.

99

Page 100: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

A

Here’s how that process looks.

100

Page 101: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

A B

101

Page 102: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

A

B C

102

Page 103: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

A

B

C D

103

Page 104: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

A

B

C

D E

104

Page 105: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

A

B

C

D

E F

105

Page 106: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

A

B

C

D

E F

This is a linked-list!

In this case the incremental SAH fails to provide a good tree. This is nothing new, this was known by the guys who invented the SAH back in 1987 (Goldsmith and Salmon).

To be honest, it is hard to imagine a reasonable cost metric that wouldn’t fail.

106

Page 107: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Is sorted input an edge case?

107

Page 108: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

108

Page 109: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

109

Page 110: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

110

Page 111: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Tree rotationsRe-arranging a tree to reduce the SAH cost

Tree rotations are used for AVL trees to keep them balanced. They can also be used for bounding volume hierarchies to reduce the surface area and to mitigate the problems introduced by sorted input.

111

Page 112: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Rotate B and F

In the tree on the left, A has four grand children. We can swap B and F to reconfigure the tree.

This is a local operation. Node A may be the child of some other node. Also, D, E, F, and G may have children.This tree rotation does not affect the ancestors of A or the descendants of D, E, F, and G.

112

Page 113: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Only the surface areas of C differ

Rotate B and F

113

Page 114: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Which is better?

Rotate B and F

114

Page 115: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

𝑆𝐴(𝐶1) = 𝑆𝐴(𝐹 ∪ 𝐺) 𝑆𝐴 𝐶2 = 𝑆𝐴 𝐷 ∪ 𝐸 ∪ 𝐺

Rotate B and F

115

Page 116: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

DG

F

E

C1

C2

Rotate B and F

𝑆𝐴(𝐶2) < 𝑆𝐴(𝐶1)

DG

F

E

116

Page 117: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

𝐵 ⇔ 𝐹 𝐶 ⇔ 𝐸

𝐵 ⇔ 𝐺 𝐶 ⇔ 𝐷

Four possible rotations

117

Page 118: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Local optimization

Choose rotation that has the lowest surface area

118

Page 119: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Local optimization

Choose rotation that has the lowest surface area

All surface area changes are local

119

Page 120: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

Now I will show how to use the tree rotation to resolve the sorted input problem.

120

Page 121: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

A

121

Page 122: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

1

A B

122

Page 123: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

2A

B C

1

Can rotate A and C or A and BDoes not reduce the area of node 2

123

Page 124: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

2A

B

1

3

C D

124

Page 125: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

2A

B

1

3

C D

Rotate 3 and A

𝑆𝐴 2 = 𝑆𝐴(𝐵 ∪ 𝐶 ∪ 𝐷)

125

Page 126: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Sorted input

A B C D E F

2

AB

1

3

C D

Rotate 3 and A

𝑆𝐴 2 = 𝑆𝐴(𝐵 ∪ 𝐴)

126

Page 127: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

// Stage 3a: walk back up the tree refitting AABBs and applying rotationsint index = tree.nodes[leafIndex].parentIndex;while (index != nullIndex){

int child1 = tree.nodes[index].child1;int child2 = tree.nodes[index].child2;

tree.nodes[index].box = Union(tree.nodes[child1].box, tree.nodes[child2].box);

Rotate(index);

index = tree.nodes[index].parentIndex;}

127

Page 128: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

References

• J. Goldsmith (1987) - Automatic Creation of Object Hierarchies

• S. Omohundro (1989) - Five Balltree Construction Algorithms

• A. Kensler (2008) - Tree Rotations for Improving Bounding Volume Hierarchies

• J. Bittner (2015) - Incremental BVH Construction for Ray Tracing

• N. Presson, btDbvt, Bullet Physics Engine, 2008

128

Page 129: Dynamic Bounding Volume Hierarchiesbox2d.org/files/GDC2019/ErinCatto_DynamicBVH_Full.pdf · Dynamic Bounding Volume Hierarchies Erin Catto, Blizzard Entertainment Hello everyone!

Thanks!

• box2d.org

• github.com/erincatto

• @erin_catto

129