![Page 1: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/1.jpg)
Game Engineering: 3DCS420-2016S-11
Spatial Data Structures
David Galles
Department of Computer Science
University of San Francisco
![Page 2: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/2.jpg)
11-0: First-Pass Collision
We now know how to to pairwise collision
Fast, less accurate AABB collision
Slower separating axis with arbitrary boundingshapes
Which elements do we compare?
![Page 3: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/3.jpg)
11-1: First-Pass Collision
Which elements do we compare?
Brute forceforeach (WorldObject o in mElements)
foreach (WorldObject other in mElements)
if (collide(o, other))
handle collision
Error!
![Page 4: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/4.jpg)
11-2: First-Pass Collision
Which elements do we compare?
Brute forceforeach (WorldObject o in mElements)
foreach (WorldObject other in mElements)
if (other != o && collide(o, other))
handle collision
Correct, can make it slightly more efficient ...
![Page 5: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/5.jpg)
11-3: First-Pass Collision
Which elements do we compare?
Brute forcefor (i = 0; i < mElements.Count; i++)
for (k = i+1; k < mElements.Count; k++)
if collide(mElements[i], mElements[k])
handle collision
Running time still Θ(n2), not practical for largen.
![Page 6: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/6.jpg)
11-4: First-Pass Collision
How can we do better?
![Page 7: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/7.jpg)
11-5: Grid
Separate world into a grid
Each grid element stores a list of all elementsat that grid location
To do collision, you only need to look at theelements in the same grid location
![Page 8: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/8.jpg)
11-6: Grid
Problems with this approach?
![Page 9: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/9.jpg)
11-7: Grid
Elements can overlap grid entries.
![Page 10: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/10.jpg)
11-8: Grid
If:
Each world element is smaller than a gridsquare
Each world element is stored in the grid squarethat contains the center of the object
Given an object o in our world, how do wedetermine which elements to we need to checkagainst o for intersection / collision?
![Page 11: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/11.jpg)
11-9: Grid
![Page 12: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/12.jpg)
11-10: Grid
![Page 13: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/13.jpg)
11-11: Grid
![Page 14: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/14.jpg)
11-12: Grid
![Page 15: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/15.jpg)
11-13: Grid
Object o is stored at grid location [x, y]
Need to consider 9 grid locations forintersection[x− 1, y − 1], [x, y − 1], [x + 1, y − 1][x− 1, y], [x, y], [x + 1, y][x− 1, y + 1], [x, y + 1], [x + 1, y + 1]
![Page 16: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/16.jpg)
11-14: Grid
Implementation Details
Pick a grid size, make all grid elements thesame size
2D array of lists, each list stores elements inthat grid element
Finding the grid location of an object is easy(how would you do it?)
As objects move around in the world, may needto change grid locations
![Page 17: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/17.jpg)
11-15: Grid
Implementation Details
Don’t want to be allocating / deallocating all ofthe time
Lists should be lightweight (arrays are likely agood idea) – If you have enough memory, eachcell location can have a fixed array size. (Mayneed to deal with overflow – either per-grid cell,or as a global overflow list)
If arrays are unordered, adding and removingelements is fast (how?)
![Page 18: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/18.jpg)
11-16: Grid
Implementation Details
Don’t want to be allocating / deallocating all ofthe time if you don’t need to
Moving an element from one grid cell to anothercan be done quickly, assuming each grid celldoesn’t hold too many elements (how wouldyou do it?)
![Page 19: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/19.jpg)
11-17: Grid
Problems with this method?
![Page 20: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/20.jpg)
11-18: Grid
Problems with this method?
Large objects require large grid sizes, mayneed to check large number of elements to docollision
If large elements are static (big platforms,etc) we can special case them – split theminto smaller objects, place them in severalgrid locations, etc
Big, sparse worlds require large (mostly empty)grids
Solution: Use non-uniform grid sizes, dependentupon actual objects in our world.
![Page 21: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/21.jpg)
11-19: Quadtree
![Page 22: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/22.jpg)
11-20: Quadtree
Tree data structrue
Root of the tree represents the entire world
Four subtrees, one for each quadrant of theworld
Each quadrant can be divided into 4 as well
![Page 23: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/23.jpg)
11-21: Quadtree
![Page 24: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/24.jpg)
11-22: Quadtree
![Page 25: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/25.jpg)
11-23: Quadtree
How would you extend this to 3D?
![Page 26: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/26.jpg)
11-24: Octree
How would you extend this to 3D?
Region of space is a cube instead of a square
Divide cube into 8 subcubes
Result is an Octree
![Page 27: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/27.jpg)
11-25: Octree
![Page 28: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/28.jpg)
11-26: Quadtree
Back to Quadtrees:
Many different variants of Quadtrees
We will be covering a specific varient thatstores AABB regions.
Operations will be inserting an AABB, finding allelements that intersect a given AABB, andmoving / resizing an AABB
![Page 29: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/29.jpg)
11-27: Quadtree
Quadtree definition:
Each non-empty node in the quadtree stores:List of all regions stored at that nodeFour children, which divide the region into 4equal quadrants
![Page 30: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/30.jpg)
11-28: Quadtree
Regions are stored in the lowest possible node inwhich they can be completely contained (up to amaxumum tree depth)
![Page 31: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/31.jpg)
11-29: Quadtree
![Page 32: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/32.jpg)
11-30: Quadtree
![Page 33: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/33.jpg)
11-31: Quadtree
![Page 34: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/34.jpg)
11-32: Quadtree
![Page 35: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/35.jpg)
11-33: Quadtree
![Page 36: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/36.jpg)
11-34: Quadtree
![Page 37: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/37.jpg)
11-35: Quadtree
![Page 38: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/38.jpg)
11-36: Quadtree
![Page 39: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/39.jpg)
11-37: Quadtree
![Page 40: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/40.jpg)
11-38: Quadtree
Maximum Depth
We may specify a maximum depth for ourquadtree
Prevents creating a huge number ofintermediate nodes for very small objects
We would need to tune the maximum depth forour particular application
![Page 41: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/41.jpg)
11-39: Quadtree
Maximum Depth = 3
Inserting Element
![Page 42: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/42.jpg)
11-40: Quadtree
Maximum Depth = 3Doesn’t overlap, but at maximum depth
![Page 43: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/43.jpg)
11-41: Quadtree
No Maximum Depth
![Page 44: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/44.jpg)
11-42: Quadtree
Implementation: Each Quadtree node stores:
Area covered by this node (Rectangle, requires2 points (or 4 numbers))
List of all elements stored in this node
Four child pointers (any of which could be null)
Parent pointerDon’t need for inserting / finding elementsCome in handy for moving, resizing elements
![Page 45: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/45.jpg)
11-43: Quadtree
Parent Pointer
Child Pointers
Area of tree(Rectange)
List of regions
![Page 46: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/46.jpg)
11-44: Quadtree
public class WorldElem
{
public Rectangle AABB { get { ... } }
// Other stuff ...
}
public class QuadtreeNode
{
public Rectangle Area { get; set; }
public ArrayList<WorldElem> mWorldElems;
public QuadtreeNode Parent { get; set; }
public QuadtreeNode UpperLeft { get; set; }
public QuadtreeNode UpperRight { get; set; }
public QuadtreeNode LowerLeft { get; set; }
public QuadtreeNode LowerRight { get; set; }
}
![Page 47: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/47.jpg)
11-45: Intersecting
How would you create a list of all elements thatintersect a particular region?
void Intersect(QuadtreeNode t, Rectangle region,
ref ArrayList<WorldElem> intersecting)
{
...
}
![Page 48: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/48.jpg)
11-46: Intersecting
Quadtrees are recursive data structures,manipulate them with recurisve functions
Base case for finding intersections?
Recursive case for finding intersection?
![Page 49: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/49.jpg)
11-47: Intersecting
Base case for finding intersections:
Empty tree – no intersections
Recursive case for finding intersection:
Everything at the root node that intersects thequery region
Plus result of recursive call to each of the 4quadrants that intersect the query region
![Page 50: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/50.jpg)
11-48: Quadtree
void Intersect(QuadtreeNode t, Rectangle region,
ref ArrayList<WorldElem> intersecting)
{
if (t != null)
{
foreach (WorldElem elem in t.mWorldElems)
{
if elem overlaps with region
intersecting.Add(elem);
}
if (region overlaps upper left quadrant of t.Area)
Intersect(t.UpperLeft, region, ref intersecting)
if (region overlaps upper right quadrant of t.Area)
Intersect(t.UpperRight, region, ref intersecting)
if (region overlaps lower left quadrant of t.Area)
Intersect(t.LowerLeft, region, ref intersecting)
if (region overlaps lower right quadrant of t.Area)
Intersect(t.LowerRight, region, ref intersecting)
}
}
![Page 51: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/51.jpg)
11-49: Quadtree
How do you insert something into a quadtree?
Base case / Recursive case
![Page 52: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/52.jpg)
11-50: Quadtree
Wha is the base case – when is it easy to insert anelement?
Empty tree is not a base case! (why not?)
![Page 53: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/53.jpg)
11-51: Quadtree
Wha is the base case – when is it easy to insert anelement?
Empty tree is not a base case! (why not?)If the tree is empty, may still need to build agreat number of tree nodes
![Page 54: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/54.jpg)
11-52: Quadtree
![Page 55: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/55.jpg)
11-53: Quadtree
Inserting
![Page 56: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/56.jpg)
11-54: Quadtree
![Page 57: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/57.jpg)
11-55: Quadtree Insertion
Base case:
![Page 58: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/58.jpg)
11-56: Quadtree Insertion
Base case:
Object you are inserting does not fit completelyin one of the subquadrents of the current node,or we are already at maximum depth
Add object to the root list
![Page 59: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/59.jpg)
11-57: Quadtree Insertion
Recursive Case:
![Page 60: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/60.jpg)
11-58: Quadtree Insertion
Recursive Case:
Object you are inserting does fit completely inone of the subquadrents of the current node,not at maximum depth
Add object to the appropriate subtree
May need to create a new subtree
![Page 61: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/61.jpg)
11-59: Quadtree Moving
How can you move an element stored in aquadtree?
(That is, when you move an element, how canyou efficiently update its position in thequadtree?)
Assume that you have:
A pointer to the object whose AABB haschanged
A poiner to the quadtree node where thiselement lives
![Page 62: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/62.jpg)
11-60: Quadtree Moving
If the element is still in the correct location, donothing
How do you know that the element is currentlyin the correct location?
![Page 63: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/63.jpg)
11-61: Quadtree Moving
If the element is still in the correct location, donothing
How do you know that the element is currentlyin the correct location?
The AABB of the relocated node still fitswithin the region of the node where it livesThe AABB of the relocated node does not fitcompletely within the region of any of the 4quadrants (or the current node is already atthe maximum depth)
![Page 64: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/64.jpg)
11-62: Quadtree Moving
If the element is not at the correct location, wherecould it be, relative to the node where it currentlylives?
![Page 65: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/65.jpg)
11-63: Quadtree Moving
![Page 66: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/66.jpg)
11-64: Quadtree Moving
![Page 67: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/67.jpg)
11-65: Quadtree Moving
![Page 68: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/68.jpg)
11-66: Quadtree Moving
![Page 69: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/69.jpg)
11-67: Quadtree Moving
![Page 70: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/70.jpg)
11-68: Quadtree Moving
![Page 71: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/71.jpg)
11-69: Quadtree Moving
When an element has moved ...
![Page 72: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/72.jpg)
11-70: Quadtree Moving
When an element e has moved from the note t:
Remove element from the list at node t
While t doesn’t contain et = t.parent
While e fits completely within one of the 4quadants of t:
t = t.quadrant // quadrant that e fitscompletely inside
Insert e in list at t
![Page 73: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/73.jpg)
11-71: Quadtree Moving
When an element e has moved from the note t:
Remove element from the list at node t
While t doesn’t contain et = t.parent
Insert e into tree rooted at t
![Page 74: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/74.jpg)
11-72: Quadtree Moving
What if the AABB for an object doesn’t just move,but changes
Object rotates, for insance
![Page 75: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/75.jpg)
11-73: Quadtree Moving
What if the AABB for an object doesn’t just move,but changes
Object rotates, for insance
Exact same code will work
Move up until you reach a node that completelycontains the object
Move down until you reach a node that justbarely contains the object (won’t fit in anychildren of node)
![Page 76: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/76.jpg)
11-74: Quadtree Moving
Moving nodes may create empty subtrees
Tiny object moves across the entire world
Should we clean up the tree, removing unusednodes?
![Page 77: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/77.jpg)
11-75: Quadtree Moving
Moving nodes may create empty subtrees
Tiny object moves across the entire world
Should we clean up the tree, removing unusednodes?
It depends!
![Page 78: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/78.jpg)
11-76: Quadtree Memory
If we don’t prune the tree, we could run out ofspace
Small object moving all over the world
If we do prune the tree, lots of calls to a memorymanager
small object moving around in a smallish region
![Page 79: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/79.jpg)
11-77: Quadtree Memory
If we never remove empty subtrees when elementsmove, eventually the tree will be complete
No more calls to new
Could also “Prefill” the tree
Problems with this method?
![Page 80: Game Engineering: 3Dgalles/cs420/lecture/SpatialDataStructures3D.pdf · 11-42: Quadtree Implementation: Each Quadtree node stores: Area covered by this node (Rectangle, requires 2](https://reader034.vdocuments.mx/reader034/viewer/2022042410/5f27da69db41485c1d18590d/html5/thumbnails/80.jpg)
11-78: Quadtree Memory
If we never remove empty subtrees when elementsmove, eventually the tree will be complete
No more calls to new
Could also “Prefill” the tree
Problems with this method?
Could use too much memory
Solution: Limit depth of the tree