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: README.md
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,25 +11,25 @@ In order to get help with basic GIT commands you may try [the GIT cheat-sheet][c
11
11
This repository located on our [homepage][homepage] is private since this is the master- and release-branch. You may clone it, but it will be read-only.
12
12
If you want to contribute to our repository (push, open pull requests), please use the copy on github located here: [the public github repository][github]
When doing game development you've probably come across a point when you'd liked to do some collision-checks and that's usually the time when you've realize that just checking all sprites against each other doesn't cut it.
18
18
The problem with that brute-force-approach is, that the number of checks grow very fast (N² for N sprites) when the number of your sprites increase.
19
-
19
+
20
20
So you somehow have to narrow down your collision-candidates.
21
21
This piece of software does that for you. It does not do the collision checks themself. It just tells you if a sprite may be near enough to a second one to maybe collide with it, which allows you to do a collision test for those two, or three, or five sprites instead of the whole bunch.
22
22
## What It Really Does...
23
23
...is a simple trade-off.
24
24
You may query it about the sprites around your coordinate or rectangle, but your sprites have to register with it and update their position/AABB on every update.
25
25
But all in all this is a lot faster than a simple brute-force check.
26
-
26
+
27
27
## Getting Started
28
28
The first thing is: You have to set-up a grid. Usually you'd take your game-coordinate-system's bounds.
29
29
Then you have to tell the grid how many cells it has by setting the number of cells on the X and Y axis.
30
-
30
+
31
31
Then you may add your sprites or other objects to the grid by calling `Add(object, Point/Vector2/Rectangle/Rect)` or `Move(object, Point/Vector2/Rectangle/Rect)`. Move removes the item first if it was present on the grid.
32
-
32
+
33
33
### Parameters
34
34
The first parameter is your item. The grid is generic and there are no constraints for that.
35
35
The second parameter is always one of the following:
@@ -47,21 +47,21 @@ Then you may add your sprites or other objects to the grid by calling `Add(objec
47
47
#### Rect
48
48
This is a special parameter in our utility-package. It's essentially a Rectangle, but with all float parameters.
49
49
By specifying this you give the grid a rectangle in the game-coordinate-system.
50
-
50
+
51
51
All rectangles this grid works with are axis-aligned.
52
52
53
53
You're free to remove them at any time by using one of the remove-methods `Remove(Point/Vector2/Rectangle/Rect)`.
54
-
54
+
55
55
The method `Get(Point/Vector2/Rectangle/Rect)` returns an array of your items with all of them that the grid has encountered in the area you've specified when calling `Get`. If it doesn't find any it returns an empty array.
56
-
56
+
57
57
If you add your sprites by their position, then this is what the grid will basically do:
58
58
![Position Test][testposition]
59
-
59
+
60
60
If you add your sprites by their AABBs, then this is what the grid will basically do:
61
61
![Rectangle Test][testrectangle]
62
62
63
63
#### Example
64
-
64
+
65
65
Set up the collision-grid:
66
66
```csharp
67
67
publicCollisionGrid<Sprite>Grid;
@@ -92,7 +92,7 @@ To achieve the output in the second one, just change the Move-line to the follow
92
92
Grid.Move(s, s.getAABB());
93
93
```
94
94
Whereas of course your sprite has to return the axis-aligned-bounding-box as a Rect.
95
-
95
+
96
96
Please don't forget to clean up afterwards. There are a few data-structures the grid has to dispose of:
Maybe you've heard of such a data-structure that does essentially exactly the same things as this grid with one major difference:
107
-
107
+
108
108
The QuadTree divides the space all by itself, dynamically whenever you add new items.
109
109
It doesn't need a fixed uniform grid, but divides space unevenly and only when another partition is needed.
110
110
And that's good and bad at the same time.
111
111
The good thing is that it can cope with unevenly distributed items very well.
112
112
The bad thing is that it costs a lot more time (the updating of this data-structure); At least when compared to this grid here.
113
113
[Here's a very good implementation of a QuadTree on GitHub with an excellent explanation what it exactly does.][quadtree]
114
-
114
+
115
115
The good news about the QuadTree is that it's exactly what you're looking for if you are thinking...
116
116
> Oh! Nice thing this grid. But the space I have to check is REALLY BIG and my sprites are very unevenly distributed. Most of the time they are clustered with much space in between those clusters. So my cells become way too big to segment those clusters in a helpful way.
117
117
118
118
...when reading the explanation of the CollisionGrid.
0 commit comments