Skip to content

Commit 36fe705

Browse files
author
psilo
committed
Initial commit.
1 parent ce3b31e commit 36fe705

25 files changed

+191342
-0
lines changed

CollisionGrid.sln

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.30110.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CollisionGrid", "CollisionGrid\CollisionGrid.csproj", "{B055EF56-7264-4723-8B3F-05D1550C331C}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{37B14616-5CCE-4239-BE27-41D25E0718D4}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{B055EF56-7264-4723-8B3F-05D1550C331C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{B055EF56-7264-4723-8B3F-05D1550C331C}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{B055EF56-7264-4723-8B3F-05D1550C331C}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{B055EF56-7264-4723-8B3F-05D1550C331C}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{37B14616-5CCE-4239-BE27-41D25E0718D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{37B14616-5CCE-4239-BE27-41D25E0718D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{37B14616-5CCE-4239-BE27-41D25E0718D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{37B14616-5CCE-4239-BE27-41D25E0718D4}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
EndGlobal
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// ***************************************************************************
2+
// Copyright (c) 2016 by Unterrainer Informatik OG.
3+
// This source is licensed to Unterrainer Informatik OG.
4+
// All rights reserved.
5+
//
6+
// In other words:
7+
// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR
8+
// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS
9+
// PERMISSION OF Unterrainer Informatik OG.
10+
// ---------------------------------------------------------------------------
11+
// Programmer: G U,
12+
// Created: 2016-03-30
13+
// ***************************************************************************
14+
15+
using System.Collections.Generic;
16+
using Microsoft.Xna.Framework;
17+
18+
namespace CollisionGrid
19+
{
20+
public partial class CollisionGrid<T>
21+
{
22+
public T[] Get(Point cell)
23+
{
24+
lock (lockObject)
25+
{
26+
return grid[Clamp(cell)].ToArray();
27+
}
28+
}
29+
30+
/// <summary>
31+
/// Adds a given item to a given cell.
32+
/// If the cell already contains the item, it is not added a second time.
33+
/// </summary>
34+
/// <param name="item">The item to add</param>
35+
/// <param name="cell">The cell to add the item to</param>
36+
public void Add(T item, Point cell)
37+
{
38+
lock (lockObject)
39+
{
40+
Point c = Clamp(cell);
41+
AddToGrid(item, c);
42+
AddToItems(item, c);
43+
}
44+
}
45+
46+
private void AddToGrid(T item, Point cell)
47+
{
48+
List<T> l = grid[cell];
49+
if (!l.Contains(item))
50+
{
51+
l.Add(item);
52+
}
53+
}
54+
55+
private void AddToItems(T item, Point cell)
56+
{
57+
List<Point> pl;
58+
items.TryGetValue(item, out pl);
59+
if (pl == null)
60+
{
61+
if (listOfPointQueue.Count > 0)
62+
{
63+
pl = listOfPointQueue.Dequeue();
64+
}
65+
else
66+
{
67+
pl = new List<Point>();
68+
}
69+
pl.Add(cell);
70+
items.Add(item, pl);
71+
}
72+
else
73+
{
74+
if (!pl.Contains(cell))
75+
{
76+
pl.Add(cell);
77+
}
78+
}
79+
}
80+
81+
/// <summary>
82+
/// Removes all items from the given cell.
83+
/// If the items don't occupy another cell, they are removed as well.
84+
/// </summary>
85+
/// <param name="cell">The cell to remove items from</param>
86+
public void Remove(Point cell)
87+
{
88+
lock (lockObject)
89+
{
90+
Point c = Clamp(cell);
91+
List<T> l = grid[c];
92+
93+
foreach (T i in l)
94+
{
95+
List<Point> pl;
96+
items.TryGetValue(i, out pl);
97+
if (pl != null)
98+
{
99+
pl.Remove(c);
100+
if (pl.Count == 0)
101+
{
102+
listOfPointQueue.Enqueue(pl);
103+
items.Remove(i);
104+
}
105+
}
106+
}
107+
108+
l.Clear();
109+
}
110+
}
111+
112+
/// <summary>
113+
/// Removes all occurrences of the given item and re-adds it at the new given cell.
114+
/// If the item hasn't been in the grid before, this will just add it.
115+
/// </summary>
116+
/// <param name="item">The item to move</param>
117+
/// <param name="cell">The cell to move it to</param>
118+
public void Move(T item, Point cell)
119+
{
120+
lock (lockObject)
121+
{
122+
Remove(item);
123+
Add(item, cell);
124+
}
125+
}
126+
}
127+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// ***************************************************************************
2+
// Copyright (c) 2016 by Unterrainer Informatik OG.
3+
// This source is licensed to Unterrainer Informatik OG.
4+
// All rights reserved.
5+
//
6+
// In other words:
7+
// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR
8+
// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS
9+
// PERMISSION OF Unterrainer Informatik OG.
10+
// ---------------------------------------------------------------------------
11+
// Programmer: G U,
12+
// Created: 2016-04-01
13+
// ***************************************************************************
14+
15+
using Utilities.Geometry;
16+
17+
namespace CollisionGrid
18+
{
19+
public partial class CollisionGrid<T>
20+
{
21+
public T[] Get(Rect aabb)
22+
{
23+
lock (lockObject)
24+
{
25+
return Get(Rectangle(aabb));
26+
}
27+
}
28+
29+
/// <summary>
30+
/// Adds a given item to the cells that are hit by the given Axis-Aligned-Bounding-Box.
31+
/// If the cell already contains the item, it is not added a second time.
32+
/// </summary>
33+
/// <param name="item">The item to add</param>
34+
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in float-grid-coordinates.</param>
35+
public void Add(T item, Rect aabb)
36+
{
37+
lock (lockObject)
38+
{
39+
Add(item, Rectangle(aabb));
40+
}
41+
}
42+
43+
/// <summary>
44+
/// Removes all items from the cells that are hit by the given Axis-Aligned-Bounding-Box.
45+
/// If the items don't occupy another cell, they are removed as well.
46+
/// </summary>
47+
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in float-grid-coordinates.</param>
48+
public void Remove(Rect aabb)
49+
{
50+
lock (lockObject)
51+
{
52+
Remove(Rectangle(aabb));
53+
}
54+
}
55+
56+
/// <summary>
57+
/// Removes all occurrences of the given item and re-adds it at the new cells that are hit by the given Axis-Aligned-Bounding-Box.
58+
/// If the item hasn't been in the grid before, this will just add it.
59+
/// </summary>
60+
/// <param name="item">The item to move</param>
61+
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in float-grid-coordinates.</param>
62+
public void Move(T item, Rect aabb)
63+
{
64+
lock (lockObject)
65+
{
66+
Move(item, Rectangle(aabb));
67+
}
68+
}
69+
}
70+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// ***************************************************************************
2+
// Copyright (c) 2016 by Unterrainer Informatik OG.
3+
// This source is licensed to Unterrainer Informatik OG.
4+
// All rights reserved.
5+
//
6+
// In other words:
7+
// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR
8+
// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS
9+
// PERMISSION OF Unterrainer Informatik OG.
10+
// ---------------------------------------------------------------------------
11+
// Programmer: G U,
12+
// Created: 2016-04-01
13+
// ***************************************************************************
14+
15+
using System.Collections.Generic;
16+
using Microsoft.Xna.Framework;
17+
18+
namespace CollisionGrid
19+
{
20+
public partial class CollisionGrid<T>
21+
{
22+
private readonly List<Point> lop = new List<Point>();
23+
private readonly List<T> result = new List<T>();
24+
25+
private void FillList(Rectangle aabb)
26+
{
27+
Rectangle r = Clamp(aabb);
28+
lop.Clear();
29+
for (int y = 0; y < r.Size.Y; y++)
30+
{
31+
for (int x = 0; x < r.Size.X; x++)
32+
{
33+
lop.Add(new Point(r.X + x, r.Y + y));
34+
}
35+
}
36+
}
37+
38+
public T[] Get(Rectangle aabb)
39+
{
40+
lock (lockObject)
41+
{
42+
FillList(aabb);
43+
result.Clear();
44+
foreach (Point p in lop)
45+
{
46+
result.AddRange(Get(p));
47+
}
48+
return result.ToArray();
49+
}
50+
}
51+
52+
/// <summary>
53+
/// Adds a given item to the cells that are hit by the given Axis-Aligned-Bounding-Box.
54+
/// If the cell already contains the item, it is not added a second time.
55+
/// </summary>
56+
/// <param name="item">The item to add</param>
57+
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in int-cell-coordinates.</param>
58+
public void Add(T item, Rectangle aabb)
59+
{
60+
lock (lockObject)
61+
{
62+
FillList(aabb);
63+
foreach (Point p in lop)
64+
{
65+
Add(item, p);
66+
}
67+
}
68+
}
69+
70+
/// <summary>
71+
/// Removes all items from the cells that are hit by the given Axis-Aligned-Bounding-Box.
72+
/// If the items don't occupy another cell, they are removed as well.
73+
/// </summary>
74+
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in int-cell-coordinates.</param>
75+
public void Remove(Rectangle aabb)
76+
{
77+
lock (lockObject)
78+
{
79+
FillList(aabb);
80+
foreach (Point p in lop)
81+
{
82+
Remove(p);
83+
}
84+
}
85+
}
86+
87+
/// <summary>
88+
/// Removes all occurrences of the given item and re-adds it at the new cells that are hit by the given Axis-Aligned-Bounding-Box.
89+
/// If the item hasn't been in the grid before, this will just add it.
90+
/// </summary>
91+
/// <param name="item">The item to move</param>
92+
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in int-cell-coordinates.</param>
93+
public void Move(T item, Rectangle aabb)
94+
{
95+
lock (lockObject)
96+
{
97+
Remove(item);
98+
FillList(aabb);
99+
foreach (Point p in lop)
100+
{
101+
Add(item, p);
102+
}
103+
}
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)