Skip to content

Commit e6cca86

Browse files
committed
Update README.md
1 parent c204b24 commit e6cca86

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

spatial-partition/README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ tags:
1010
---
1111

1212
## Intent
13-
As explained in the book [Game Programming Patterns](http://gameprogrammingpatterns.com/spatial-partition.html) by Bob Nystrom, spatial partition pattern helps to
1413

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.
1617

1718
## 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:
1924

2025
```java
2126
public void handleMeLee(Unit units[], int numUnits) {
@@ -32,21 +37,33 @@ public void handleMeLee(Unit units[], int numUnits) {
3237
}
3338
```
3439

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:
3750

3851
* Grid
3952
* Quad tree
40-
* k-d tree
53+
* K-d tree
4154
* BSP
4255
* Boundary volume hierarchy
4356

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.
4560

4661
## Class diagram
62+
4763
![alt text](./etc/spatial-partition.urm.png "Spatial Partition pattern class diagram")
4864

4965
## Applicability
66+
5067
This pattern can be used:
5168

5269
* When you need to keep track of a large number of objects' positions, which are getting updated every frame.

spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionBubbles.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package com.iluwatar.spatialpartition;
2525

2626
import java.util.ArrayList;
27-
import java.util.Collection;
2827
import java.util.Hashtable;
2928

3029
/**

0 commit comments

Comments
 (0)