-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpatialHash.cs
More file actions
136 lines (119 loc) · 3.33 KB
/
SpatialHash.cs
File metadata and controls
136 lines (119 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
struct Pair
{
public int x;
public int y;
public override bool Equals(object obj)
{
return obj is Pair pair &&
x == pair.x &&
y == pair.y;
}
public override int GetHashCode()
{
return HashCode.Combine(x, y);
}
public static bool operator ==(Pair left, Pair right)
{
return left.Equals(right);
}
public static bool operator !=(Pair left, Pair right)
{
return !(left == right);
}
}
public struct CollisionPair
{
public Movement m1;
public Movement m2;
}
//public class SpatialHash : MonoBehaviour
public struct SpatialHash
{
private Dictionary<Pair, List<Movement>> buckets;
private int cellsize;
// Start is called before the first frame update
public void Initialize()
{
buckets = new Dictionary<Pair, List<Movement>>();
cellsize = 1;
}
public void AddObject(Movement m)
{
foreach (Pair p in ObjectExists(m))
{
PrivateAdd(p, m);
}
}
public void RemoveObject(Movement m)
{
foreach (Pair p in ObjectExists(m))
{
PrivateRemove(p, m);
}
}
public void Clear()
{
buckets.Clear();
}
public List<CollisionPair> PotentialCollisions() // return references?
{
List<CollisionPair> sets = new();
if (!buckets.Any())
{
return sets;
}
foreach (KeyValuePair<Pair, List<Movement>> e in buckets)
{
if (e.Value.Count > 1)
{
for (int i = 0; i < e.Value.Count - 1; i++)
{
for (int j = i + 1; j < e.Value.Count; j++)
{
CollisionPair c;
c.m1 = e.Value[i];
c.m2 = e.Value[j];
sets.Add(c);
}
}
}
}
return sets;
}
// all cells where the object exists
private List<Pair> ObjectExists(Movement m)
{
List<Pair> list = new();
Pair topleft, topright, bottomleft, bottomright;
topleft.x = (int)Math.Floor((m.GetX() - 0.5) / cellsize); topleft.y = (int)Math.Floor((m.GetZ() + 0.5) / cellsize); // potential confusion: ycoord of Pair is z of movement
topright.x = (int)Math.Floor((m.GetX() + 0.5) / cellsize); topright.y = (int)Math.Floor((m.GetZ() + 0.5) / cellsize);
bottomleft.x = (int)Math.Floor((m.GetX() - 0.5) / cellsize); bottomleft.y = (int)Math.Floor((m.GetZ() - 0.5) / cellsize);
bottomright.x = (int)Math.Floor((m.GetX() + 0.5) / cellsize); bottomright.y = (int)Math.Floor((m.GetZ() - 0.5) / cellsize);
list.Add(topleft);
list.Add(topright);
list.Add(bottomleft);
list.Add(bottomright);
return list;
}
private void PrivateAdd(Pair p, Movement m)
{
if (!buckets.ContainsKey(p))
{
buckets.Add(p, new List<Movement>());
}
buckets[p].Add(m);
}
private void PrivateRemove(Pair p, Movement m)
{
buckets[p].Remove(m);
if (!buckets[p].Any())
{
buckets.Remove(p);
}
}
}