Skip to content

Commit e8eb4c4

Browse files
author
psilo
committed
Updated readme.md
1 parent 36fe705 commit e8eb4c4

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,66 @@
1-
# collisiongrid
1+
```
2+
/**************************************************************************
3+
*
4+
* by Unterrainer Informatik OG.
5+
* This is free and unencumbered software released into the public domain.
6+
* Anyone is free to copy, modify, publish, use, compile, sell, or
7+
* distribute this software, either in source code form or as a compiled
8+
* binary, for any purpose, commercial or non-commercial, and by any
9+
* means.
10+
*
11+
* In jurisdictions that recognize copyright laws, the author or authors
12+
* of this software dedicate any and all copyright interest in the
13+
* software to the public domain. We make this dedication for the benefit
14+
* of the public at large and to the detriment of our heirs and
15+
* successors. We intend this dedication to be an overt act of
16+
* relinquishment in perpetuity of all present and future rights to this
17+
* software under copyright law.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22+
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23+
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25+
* OTHER DEALINGS IN THE SOFTWARE.
26+
*
27+
* For more information, please refer to <http://unlicense.org>
28+
*
29+
* (In other words you may copy, use, change, redistribute and sell it without
30+
* any restrictions except for not suing me because it broke something.)
31+
*
32+
***************************************************************************/
33+
34+
```
35+
36+
# General
37+
38+
This section contains various useful projects that should help your development-process.
39+
40+
This section of our GIT repositories is free. You may copy, use or rewrite every single one of its contained projects to your hearts content.
41+
In order to get help with basic GIT commands you may try [the GIT cheat-sheet][coding] on our [homepage][homepage].
42+
43+
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.
44+
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]
45+
46+
# CollisionGrid
47+
48+
This class implements a collision-grid.
49+
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.
50+
The problem is that the number of checks grow very fast (N² for N sprites) when the number of your sprites grow.
51+
52+
![Position Test][testposition]
53+
54+
![Rectangle Test][testrectangle]
55+
56+
#### Example
57+
58+
```csharp
59+
60+
```
61+
62+
[homepage]: http://www.unterrainer.info
63+
[coding]: http://www.unterrainer.info/Home/Coding
64+
[github]: https://github.com/UnterrainerInformatik/collisiongrid
65+
[testrectangle]: https://github.com/UnterrainerInformatik/collisiongrid/testrectangle.gif
66+
[testposition]: https://github.com/UnterrainerInformatik/collisiongrid/testposition.gif

Test/DrawableGrid.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace Test
2323
{
2424
internal class DrawableGrid : DrawableGameComponent
2525
{
26+
private const int NUMBER_OF_SPRITES = 50;
27+
2628
public CollisionGrid<Sprite> Grid;
2729
public Vector2 Position { get; set; }
2830
public SpriteBatch SpriteBatch { get; set; }
@@ -43,13 +45,13 @@ public override void Initialize()
4345
{
4446
base.Initialize();
4547
IRandomNumberGenerator rand = RandomizerController.Randomizer;
46-
for (int i = 0; i < 50; i++)
48+
for (int i = 0; i < NUMBER_OF_SPRITES; i++)
4749
{
4850
Sprite s = new Sprite(Game, SpriteBatch, new Point((int)width, (int)height));
4951
s.Trajectory = new Vector2(rand.RandomBetween(-1f, 1f), rand.RandomBetween(-1f, 1f));
5052
s.Trajectory.Normalize();
5153
s.Position = new Vector2(rand.RandomBetween(0f, 700f), rand.RandomBetween(0f, 700f));
52-
s.Velocity = rand.RandomBetween(1f, 6f);
54+
s.Velocity = rand.RandomBetween(.1f, 4f);
5355
s.Width = rand.RandomBetween(5, 53);
5456
s.Height = rand.RandomBetween(5, 53);
5557

@@ -64,15 +66,15 @@ public override void Update(GameTime gameTime)
6466
base.Update(gameTime);
6567
foreach (Sprite s in sprites)
6668
{
67-
Grid.Move(s, s.GetAABB());
69+
Grid.Move(s, s.Position);
6870
}
6971
}
7072

7173
public override void Draw(GameTime gameTime)
7274
{
7375
if (Visible)
7476
{
75-
SpriteBatch.Begin();
77+
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
7678
Rectangle r = new Rectangle((int)Position.X, (int)Position.Y, (int)width, (int)height);
7779
SpriteBatch.DrawRectangle(r, Color.Yellow);
7880
for (int x = 0; x < Grid.NumberOfCellsX; x++)
@@ -81,9 +83,15 @@ public override void Draw(GameTime gameTime)
8183
{
8284
Rectangle cell = new Rectangle((int) (Position.X + x*Grid.CellWidth), (int) (Position.Y + y*Grid.CellHeight),
8385
(int) Grid.CellWidth, (int) Grid.CellHeight);
84-
if (Grid.Get(new Point(x, y)).Length > 0)
86+
int l = Grid.Get(new Point(x, y)).Length;
87+
if (l > 0)
8588
{
86-
SpriteBatch.FillRectangle(cell, Utils.SetTransparencyOnColor(Color.Red, .5f));
89+
float f = l/4f;
90+
if (f > 1)
91+
{
92+
f = 1;
93+
}
94+
SpriteBatch.FillRectangle(cell, Utils.SetTransparencyOnColor(Color.Red, f));
8795
}
8896
else
8997
{

testposition.gif

2.97 MB
Loading

testrectangle.gif

8.05 MB
Loading

0 commit comments

Comments
 (0)