You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spatial-partition/README.md
+24-7Lines changed: 24 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -10,12 +10,17 @@ tags:
10
10
---
11
11
12
12
## Intent
13
-
As explained in the book [Game Programming Patterns](http://gameprogrammingpatterns.com/spatial-partition.html) by Bob Nystrom, spatial partition pattern helps to
14
13
15
-
> efficiently locate objects by storing them in a data structure organized by their positions.
14
+
As explained in the book [Game Programming Patterns](http://gameprogrammingpatterns.com/spatial-partition.html)
15
+
by Bob Nystrom, spatial partition pattern helps to efficiently locate objects by storing them in a
16
+
data structure organized by their positions.
16
17
17
18
## Explanation
18
-
Say, you are building a war game with hundreds, or maybe even thousands of players, who are clashing on the battle field. Each player's position is getting updated every frame. The simple way to handle all interactions taking place on the field is to check each player's position against every other player's position:
19
+
20
+
Say, you are building a war game with hundreds, or maybe even thousands of players, who are clashing
21
+
on the battle field. Each player's position is getting updated every frame. The simple way to handle
22
+
all interactions taking place on the field is to check each player's position against every other
23
+
player's position:
19
24
20
25
```java
21
26
publicvoid handleMeLee(Unit units[], int numUnits) {
@@ -32,21 +37,33 @@ public void handleMeLee(Unit units[], int numUnits) {
32
37
}
33
38
```
34
39
35
-
This will include a lot of unnecessary checks between players which are too far apart to have any influence on each other. The nested loops gives this operation an O(n^2) complexity, which has to be performed every frame since many of the objects on the field may be moving each frame.
36
-
The idea behind the Spatial Partition design pattern is to enable quick location of objects using a data structure that is organised by their positions, so when performing an operation like the one above, every object's position need not be checked against all other objects' positions. The data structure can be used to store moving and static objects, though in order to keep track of the moving objects, their positions will have to be reset each time they move. This would mean having to create a new instance of the data structure each time an object moves, which would use up additional memory. The common data structures used for this design pattern are:
40
+
This will include a lot of unnecessary checks between players which are too far apart to have any
41
+
influence on each other. The nested loops gives this operation an O(n^2) complexity, which has to be
42
+
performed every frame since many of the objects on the field may be moving each frame. The idea
43
+
behind the Spatial Partition design pattern is to enable quick location of objects using a data
44
+
structure that is organised by their positions, so when performing an operation like the one above,
45
+
every object's position need not be checked against all other objects' positions. The data structure
46
+
can be used to store moving and static objects, though in order to keep track of the moving objects,
47
+
their positions will have to be reset each time they move. This would mean having to create a new
48
+
instance of the data structure each time an object moves, which would use up additional memory. The
49
+
common data structures used for this design pattern are:
37
50
38
51
* Grid
39
52
* Quad tree
40
-
*k-d tree
53
+
*K-d tree
41
54
* BSP
42
55
* Boundary volume hierarchy
43
56
44
-
In our implementation, we use the Quadtree data structure which will reduce the time complexity of finding the objects within a certain range from O(n^2) to O(nlogn), decreasing the computations required significantly in case of large number of objects.
57
+
In our implementation, we use the Quadtree data structure which will reduce the time complexity of
58
+
finding the objects within a certain range from O(n^2) to O(nlogn), decreasing the computations
59
+
required significantly in case of large number of objects.
45
60
46
61
## Class diagram
62
+
47
63

48
64
49
65
## Applicability
66
+
50
67
This pattern can be used:
51
68
52
69
* When you need to keep track of a large number of objects' positions, which are getting updated every frame.
0 commit comments