-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoundingBox.java
154 lines (131 loc) · 2.73 KB
/
BoundingBox.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
public class BoundingBox
{
float [] cornerx;
float [] cornerz;
float [] x;
float [] z;
float rotation;
public BoundingBox(float centerx, float centerz, float length, float width)
{
float l = length/2f;
float w = width/2f;
cornerx = new float[4];
cornerz = new float[4];
x = new float[4];
z = new float[4];
cornerx[0] = centerx - l;
cornerz[0] = centerz + w;
cornerx[1] = centerx + l;
cornerz[1] = centerz + w;
cornerx[2] = centerx + l;
cornerz[2] = centerz - w;
cornerx[3] = centerx - l;
cornerz[3] = centerz - w;
//setBounds(new Position(centerx, centerz, 0));
}
public void setBounds(Position pos)
{
//
rotation *= Math.PI/180f;
for(int i = 0; i < 4; i++)
{
x[i] = pos.X() - (float)Math.cos(rotation)*(cornerx[i]) + (float)Math.sin(rotation)*(cornerz[i]);
z[i] = pos.Z() + (float)Math.sin(rotation)*(cornerx[i]) + (float)Math.cos(rotation)*(cornerz[i]);
}
this.rotation = pos.R();
}
public boolean collisionCheck(BoundingBox other)
{
for(int i = 0; i < 4; i++)
{
if(separatingAxis(other, i))
{
return false;
}
}
return true;
}
public boolean separatingAxis(BoundingBox other, int side)
{
float xAxis;
float zAxis;
if(side < 2)
{
xAxis = x[side + 1] - x[side];
zAxis = z[side + 1] - z[side];
}
else
{
xAxis = x[side - 1] - x[side - 2];
zAxis = z[side - 1] - z[side - 2];
}
float magnitude = (float)Math.sqrt(xAxis*xAxis + zAxis*zAxis);
xAxis /= magnitude;
zAxis /= magnitude;
float myEnd1 = x[side]*xAxis + z[side]*zAxis;
float myEnd2 = x[(side + 2) % 4]*xAxis + z[(side + 2) % 4]*zAxis;
float myMin;
float myMax;
if(myEnd1 > myEnd2)
{
myMin = myEnd2;
myMax = myEnd1;
}
else
{
myMin = myEnd1;
myMax = myEnd2;
}
float otherMin = 100;
float otherMax = -100;
float proj;
for(int i = 0; i < 4; i++)
{
if(side < 2)
{
proj = other.x[i]*xAxis + other.z[i]*zAxis;
}
else
{
proj =x[i]*xAxis + z[i]*zAxis;
}
if(proj < otherMin)
{
otherMin = proj;
}
if(proj > otherMax)
{
otherMax = proj;
}
}
if(((myMax <= otherMin) || (otherMax <= myMin) || (myMin >= otherMax) || (otherMin >= myMax)))
{
return true;
}
return false;
}
public boolean isPointInsideBox(float pointx, float pointz)
{
for(int i = 1; i < 4; i += 2)
{
float sidex = x[i] - x[0];
float sidez = z[i] - z[0];
float distx = pointx - x[0];
float distz = pointz - z[0];
float dot1 = distx*sidex + distz*sidez;
float dot2 = sidex*sidex + sidez*sidez;
if((dot1 > 0 && dot1 < dot2))
{
if(i == 3)
{
return true;
}
}
else
{
break;
}
}
return false;
}
}