Skip to content

Commit ea855e3

Browse files
author
psilo
committed
Update of readme.md
1 parent bbaca34 commit ea855e3

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,58 @@ If you want to contribute to our repository (push, open pull requests), please u
4848
This class implements a collision-grid.
4949
When doing game development you've all come across a point when you'd like to do some collision-checks and that's usually the time when you realize that just checking all sprites against each other just doesn't cut it.
5050
The problem is that the number of checks grow very fast (N² for N sprites) when the number of your sprites grow.
51+
52+
So you somehow have to narrow down your collision-candidates.
53+
This piece of software does that for you. It does not do collision checking itself. It just tells you if a sprite is near enough to a second one to maybe collide which allows you to do a collision test for those two, or three, or five...
54+
55+
The first thing is: You have to set-up a grid. Usually you'd take your game-coordinate-system's bounds.
56+
Then you have to tell the grid how many cells it has by setting the number of cells on the X and Y axis.
57+
58+
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.
59+
60+
### Parameters
61+
The first parameter is your item. The grid is generic and there are no constraints for that.
62+
The second parameter is always one of the following:
63+
| Parameter | Description | Info |
64+
|:----------|:------------|:-----|
65+
|Point|This is an int-point.|By specifying this you tell the grid you mean the cell at exactly this position.|
66+
|Vector2|This is a float-vector.|By specifying this you tell the grid that you mean the cell that contains these game-coordinates.|
67+
|Rectangle|This is a basic int-rectangle. It is not rotated and therefore axis-alinged. So it's an Axis-Aligned-Bounding-Box or AABB.|By specifying this you give the grid a rectangle in the cell-coordinate-system (0-numberOfCellsX, 0-numberOfCellsY).|
68+
|Rect|This is a special parameter in our utility-package. It's essentially a Rectangle, but with all float parameters.|By specifying this you give the grid a rectangle in the game-coordinate-system.|
69+
70+
All rectangles this grid works with are axis-aligned.
5171

72+
You're free to remove them at any time by using one of the remove-methods `Remove(Point/Vector2/Rectangle/Rect)`.
73+
74+
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.
75+
5276
![Position Test][testposition]
5377

5478
![Rectangle Test][testrectangle]
5579

5680
#### Example
5781

82+
Set up the collision-grid:
5883
```csharp
84+
public CollisionGrid<Sprite> Grid;
5985

86+
public DrawableGrid(Game game, SpriteBatch spriteBatch, float width, float height, int numberOfCellsX, int numberOfCellsY) : base(game)
87+
{
88+
Grid = new CollisionGrid<Sprite>(width, height, numberOfCellsX, numberOfCellsY);
89+
}
90+
```
91+
Place your sprites on to the grid in your update-method:
92+
```csharp
93+
public override void Update(GameTime gameTime)
94+
{
95+
base.Update(gameTime);
96+
foreach (Sprite s in sprites)
97+
{
98+
Grid.Move(s, s.Position);
99+
}
100+
}
60101
```
102+
The move-method adds an item on the given position (cell that encapsulates the given game-coordinate)
61103

62104
[homepage]: http://www.unterrainer.info
63105
[coding]: http://www.unterrainer.info/Home/Coding

Test/DrawableGrid.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.Xna.Framework;
1818
using Microsoft.Xna.Framework.Graphics;
1919
using Utilities;
20+
using Utilities.Geometry;
2021
using Utilities.Randomizing;
2122

2223
namespace Test
@@ -29,7 +30,7 @@ internal class DrawableGrid : DrawableGameComponent
2930
public Vector2 Position { get; set; }
3031
public SpriteBatch SpriteBatch { get; set; }
3132

32-
private List<Sprite> sprites = new List<Sprite>();
33+
private readonly List<Sprite> sprites = new List<Sprite>();
3334
private readonly float width;
3435
private readonly float height;
3536

@@ -66,7 +67,7 @@ public override void Update(GameTime gameTime)
6667
base.Update(gameTime);
6768
foreach (Sprite s in sprites)
6869
{
69-
Grid.Move(s, s.Position);
70+
Grid.Move(s, s.Position);Grid.rem
7071
}
7172
}
7273

0 commit comments

Comments
 (0)