-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySpaceTree.java
235 lines (204 loc) · 7.39 KB
/
BinarySpaceTree.java
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
234
235
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package me.martin.radev.game.virtualcommando.structures;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import me.martin.radev.game.virtualcommando.game.interaction.CollisionDetection;
import me.martin.radev.game.virtualcommando.geometry.entity.Rectangle;
import me.martin.radev.game.virtualcommando.geometry.entity.Vector2D;
import me.martin.radev.game.virtualcommando.view.graphics.entity.GraphicalObject;
/**
* A BinarySpaceTree (BST) uses the technique called binary space partitioning
* to partition the space into subspaces. In the lowest subspace (leaf node in the tree), there is a set of static objects on the map. This drastically reduces
* the number of objects on the map to be checked for collisions. It is important
* as well how big should be the smallest subspace and also the proper way of partitioning.
* @author Marto
*/
public class BinarySpaceTree {
private final int minimumWidth;
private final int minimumHeight;
private PartitionSpace root;
/**
* Creates a BST with a totalWidth and totalHeight.
* minimumWidth and minimumHeight represent the width and height border for
* smallest leaf node.
* @param totalWidth
* @param totalHeight
* @param minimumWidth
* @param minimumHeight
*/
public BinarySpaceTree(int totalWidth, int totalHeight, int minimumWidth, int minimumHeight) {
this.minimumWidth = minimumWidth;
this.minimumHeight = minimumHeight;
root = new PartitionSpace(new Vector2D(0, 0),
new Vector2D(totalWidth, totalHeight));
}
/**
* returns the minimum height
* @return
*/
public int getMinimumHeight() {
return minimumHeight;
}
/**
* returns the minimum width
* @return
*/
public int getMinimumWidth() {
return minimumWidth;
}
/**
* returns the root of the tree. The tree is the biggest {@link PartitionSpace}
* @return
*/
public PartitionSpace getRoot() {
return root;
}
/**
* adds an object to the binary space tree
* @param object
*/
public void addElement(GraphicalObject object) {
addElementRecursively(root, object);
}
private void addElementRecursively(PartitionSpace tree, GraphicalObject object) {
if (tree.leftSubspace == null) {
tree.addObject(object);
} else {
if (CollisionDetection.doCollide(tree.leftSubspace.getBox(), object.getBody())) {
addElementRecursively(tree.leftSubspace, object);
}
if (CollisionDetection.doCollide(tree.rightSubspace.getBox(), object.getBody())) {
addElementRecursively(tree.rightSubspace, object);
}
}
}
/**
* returns a {@link Collection} of objects which are possible to
* collide with the object.
* @param object
* @return
*/
public Collection<GraphicalObject> getObjectsInClosingArea(GraphicalObject object) {
return getObjectsInClosingAreaRecursively(root, object);
}
private Set<GraphicalObject> getObjectsInClosingAreaRecursively(PartitionSpace tree, GraphicalObject object) {
if (tree.leftSubspace == null) {
return new HashSet<>(tree.getObjects());
} else {
Set<GraphicalObject> A = null, B = null;
if (CollisionDetection.doCollide(tree.leftSubspace.getBox(), object.getBody())) {
A = getObjectsInClosingAreaRecursively(tree.leftSubspace, object);
}
if (CollisionDetection.doCollide(tree.rightSubspace.getBox(), object.getBody())) {
B = getObjectsInClosingAreaRecursively(tree.rightSubspace, object);
}
if (A != null && B != null) {
A.addAll(B);
return A;
} else if (A != null) {
return A;
} else if (B != null) {
return B;
} else {
System.out.println("ERROR");
System.out.println(object.getBody().getCenter());
}
}
return null;
}
/**
*
*/
public class PartitionSpace {
private Rectangle box;
private PartitionSpace leftSubspace;
private PartitionSpace rightSubspace;
private List<GraphicalObject> objects;
/**
* Creates a {@link PartitionSpace} with a bottom left corner and a
* top right corner.
* @param bottomLeft
* @param topRight
*/
public PartitionSpace(Vector2D bottomLeft, Vector2D topRight) {
box = new Rectangle(bottomLeft, topRight);
this.leftSubspace = this.rightSubspace = null;
partition();
if (this.leftSubspace == null) objects = new LinkedList<>();
}
/**
* returns the width of the space
* @return
*/
public int getWidth() {
return (int)(box.getWidth());
}
/**
* returns the height of the space
* @return
*/
public int getHeight() {
return (int)(box.getHeight());
}
private void partition() {
if (this.getWidth() <= BinarySpaceTree.this.getMinimumWidth()
&& this.getHeight() <= BinarySpaceTree.this.getMinimumHeight()) {
return;
}
if (this.getWidth() < this.getHeight()) {
double minY = (box.getBottomLeftCorner().getY() + box.getTopRightCorner().getY())/2d;
leftSubspace =
new PartitionSpace(box.getBottomLeftCorner(), new Vector2D(box.getTopRightCorner().getX(), minY));
rightSubspace =
new PartitionSpace(new Vector2D(box.getBottomLeftCorner().getX(), minY), box.getTopRightCorner());
} else {
double minX = (box.getBottomLeftCorner().getX() + box.getTopRightCorner().getX())/2d;
leftSubspace =
new PartitionSpace(box.getBottomLeftCorner(), new Vector2D(minX, box.getTopRightCorner().getY()));
rightSubspace =
new PartitionSpace(new Vector2D(minX, box.getBottomLeftCorner().getY()), box.getTopRightCorner());
}
}
/**
* returns the objects in the space
* @return
*/
public List<GraphicalObject> getObjects() {
return objects;
}
/**
* adds an object to the list of objects
* @param object
*/
public void addObject(GraphicalObject object) {
objects.add(object);
}
/**
* returns left subspace
* @return
*/
public PartitionSpace getLeftSubspace() {
return leftSubspace;
}
/**
* returns right subspace
* @return
*/
public PartitionSpace getRightSubspace() {
return rightSubspace;
}
/**
* returns the space as a {@link Rectangle} object
* @return
*/
public Rectangle getBox() {
return box;
}
}
}