-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quadtree.hx
233 lines (201 loc) · 5.73 KB
/
Quadtree.hx
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package;
import flash.display.Sprite;
/**
* ...
* @author Henry D. Fernández B.
*/
class Quadtree
{
private static var MAX_OBJECTS : Int = 2;
private static var MAX_LEVELS : Int = 3;
private var level : Int;
private var objects : Array<PhysicsObject>;
private var bounds : flash.geom.Rectangle;
private var nodes : Array<Quadtree>;
private var parent : Quadtree;
public function new(level : Int,bounds : flash.geom.Rectangle, parent : Quadtree)
{
this.level = level;
this.bounds = bounds;
objects = new Array<PhysicsObject>();
nodes = [null, null, null, null];
this.parent = parent;
}
public function Initialize() : Void
{
nodes = [null,null,null,null];
}
/*
* Clears the quadtree
*/
public function Clear() : Void
{
//Clean
for (o in objects)
objects.remove(o);
//objects.splice(0,objects.length);
for (n in nodes)
{
if (n != null)
{
n.Clear();
nodes.remove(n);
//n = null;
}
}
}
/*
* Splits the node into 4 subnodes
*/
private function Split() : Void
{
var subWidth, subHeight, x, y : Float;
subWidth = bounds.width / 2;
subHeight = bounds.height / 2;
x = bounds.x;
y = bounds.y;
nodes[0] = new Quadtree(level+1, new flash.geom.Rectangle(x + subWidth, y, subWidth, subHeight),this);
nodes[1] = new Quadtree(level+1, new flash.geom.Rectangle(x, y, subWidth, subHeight),this);
nodes[2] = new Quadtree(level+1, new flash.geom.Rectangle(x, y + subHeight, subWidth, subHeight),this);
nodes[3] = new Quadtree(level+1, new flash.geom.Rectangle(x + subWidth, y + subHeight, subWidth, subHeight),this);
}
/*
* Determine which node the object belongs to. -1 means
* object cannot completely fit within a child node and is part
* of the parent node
*/
private function GetIndices(gameObj : PhysicsObject) : Array<Int>
{
var indices : Array<Int>;
var verticalMidpoint, horizontalMidpoint : Float;
var topQuadrant, bottomQuadrant : Bool;
var pRect : flash.geom.Rectangle;
pRect = new flash.geom.Rectangle(gameObj.GetPosition().x, gameObj.GetPosition().y, gameObj.GetW(), gameObj.GetH());
indices = new Array<Int>();
verticalMidpoint = bounds.x + (bounds.width / 2);
horizontalMidpoint = bounds.y + (bounds.height / 2);
//Top-Right quadrant: 0
if (pRect.x + (pRect.width / 2) >= verticalMidpoint && pRect.x - (pRect.width / 2) <= verticalMidpoint + (bounds.width / 2))
{
if (pRect.y + (pRect.height / 2) >= horizontalMidpoint - (bounds.height / 2) && pRect.y - (pRect.height / 2) <= horizontalMidpoint)
indices.push(0);
}
//Top-Left quadrant: 1
if (pRect.x + (pRect.width / 2) >= verticalMidpoint - (bounds.width/2) && pRect.x - (pRect.width / 2) <= verticalMidpoint)
{
if (pRect.y + (pRect.height / 2) >= horizontalMidpoint - (bounds.height / 2) && pRect.y - (pRect.height / 2) <= horizontalMidpoint)
indices.push(1);
}
//Bottom-Left quadrant: 2
if (pRect.x + (pRect.width / 2) >= verticalMidpoint - (bounds.width/2) && pRect.x - (pRect.width / 2) <= verticalMidpoint)
{
if (pRect.y + (pRect.height / 2) >= horizontalMidpoint && pRect.y - (pRect.height / 2) <= horizontalMidpoint + (bounds.height / 2))
indices.push(2);
}
//Bottom-Right quadrant: 3
if (pRect.x + pRect.width / 2 >= verticalMidpoint && pRect.x - pRect.width / 2 <= verticalMidpoint + bounds.width / 2)
{
if (pRect.y + (pRect.height / 2) >= horizontalMidpoint && pRect.y - (pRect.height / 2) <= horizontalMidpoint + (bounds.height / 2))
indices.push(3);
}
if (indices.length <= 0)
indices.push( -1);
return indices;
}
/*
* Insert the object into the quadtree. If the node
* exceeds the capacity, it will split and add all
* objects to their corresponding nodes.
*/
public function Insert(gameObj : PhysicsObject) : Void
{
var indices : Array<Int>;
var i : Int;
var obj : GameObject;
if (nodes[0] != null)
{
indices = GetIndices(gameObj);
for (ii in indices)
{
if (ii != -1)
{
nodes[ii].Insert(gameObj);
}
}
return;
}
objects.push(gameObj);
if (objects.length > MAX_OBJECTS && level < MAX_LEVELS)
{
if (nodes[0] == null)
Split();
i = 0;
while(i < objects.length)
{
obj = objects[i];
indices = GetIndices(obj);
for (ii in indices)
{
if (ii != -1)
{
nodes[ii].Insert(obj);
objects.remove(obj);
}
else
i++;
}
}
}
}
/*
* Return all objects that could collide with the given object
*/
public function Retrieve(returnObjects : List<PhysicsObject>,gameObj : PhysicsObject) : List<PhysicsObject>
{
var indices : Array<Int>;
var exists : Bool;
indices = GetIndices(gameObj);
//It allows objects collide check collision with every child in the tree when they are in the border
for (i in indices)
{
if (nodes[0] != null && i != -1)
nodes[i].Retrieve(returnObjects, gameObj);
for (o in objects)
{
if (gameObj != o && gameObj.CheckQuadtreeCondition(o))
{
exists = false;
trace(returnObjects.length);
for (r in returnObjects)
{
if (r == o)
{
exists = true;
break;
}
}
if(!exists)
returnObjects.add(o);
}
}
}
return returnObjects;
}
public function Draw(container : Sprite) : Void
{
if (container != null)
{
container.graphics.lineStyle( 2, 0xffffff, 0.5);
container.graphics.moveTo(bounds.x + bounds.width/2,bounds.y);
container.graphics.lineTo(bounds.x + bounds.width / 2, bounds.y + bounds.height);
container.graphics.lineStyle( 2, 0xffffff, 0.5);
container.graphics.moveTo(bounds.x,bounds.y + bounds.height/2);
container.graphics.lineTo(bounds.x + bounds.width, bounds.y + bounds.height / 2);
}
for (n in nodes)
{
if(n!=null)
n.Draw(container);
}
}
}