forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aabb.go
250 lines (224 loc) · 7.31 KB
/
aabb.go
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package physics
import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/go-gl/mathgl/mgl64"
)
// AABB represents an Axis Aligned Bounding Box in a 3D space. It is defined as two Vec3s, of which one is the
// minimum and one is the maximum.
type AABB struct {
min, max mgl64.Vec3
}
// NewAABB creates a new axis aligned bounding box with the minimum and maximum coordinates provided.
func NewAABB(min, max mgl64.Vec3) AABB {
return AABB{min: min, max: max}
}
// Grow grows the bounding box in all directions by x and returns the new bounding box.
func (aabb AABB) Grow(x float64) AABB {
add := mgl64.Vec3{x, x, x}
return AABB{min: aabb.min.Sub(add), max: aabb.max.Add(add)}
}
// GrowVec3 grows the AABB on all axes as represented by the Vec3 passed. The vec values are subtracted from
// the minimum values of the AABB and added to the maximum values of the AABB.
func (aabb AABB) GrowVec3(vec mgl64.Vec3) AABB {
return AABB{min: aabb.min.Sub(vec), max: aabb.max.Add(vec)}
}
// Min returns the minimum coordinate of the bounding box.
func (aabb AABB) Min() mgl64.Vec3 {
return aabb.min
}
// Max returns the maximum coordinate of the bounding box.
func (aabb AABB) Max() mgl64.Vec3 {
return aabb.max
}
// Width returns the width of the AABB.
func (aabb AABB) Width() float64 {
return aabb.max[0] - aabb.min[0]
}
// Length returns the length of the AABB.
func (aabb AABB) Length() float64 {
return aabb.max[2] - aabb.min[2]
}
// Height returns the height of the AABB.
func (aabb AABB) Height() float64 {
return aabb.max[1] - aabb.min[1]
}
// Extend expands the AABB on all axes as represented by the Vec3 passed. Negative coordinates result in an
// expansion towards the negative axis, and vice versa for positive coordinates.
func (aabb AABB) Extend(vec mgl64.Vec3) AABB {
if vec[0] < 0 {
aabb.min[0] += vec[0]
} else if vec[0] > 0 {
aabb.max[0] += vec[0]
}
if vec[1] < 0 {
aabb.min[1] += vec[1]
} else if vec[1] > 0 {
aabb.max[1] += vec[1]
}
if vec[2] < 0 {
aabb.min[2] += vec[2]
} else if vec[2] > 0 {
aabb.max[2] += vec[2]
}
return aabb
}
// ExtendTowards extends the bounding box by x in a given direction.
func (aabb AABB) ExtendTowards(f cube.Face, x float64) AABB {
switch f {
case cube.FaceDown:
aabb.max[1] -= x
case cube.FaceUp:
aabb.min[1] += x
case cube.FaceNorth:
aabb.min[2] -= x
case cube.FaceSouth:
aabb.max[2] += x
case cube.FaceWest:
aabb.min[0] -= x
case cube.FaceEast:
aabb.max[0] += x
}
return aabb
}
// Stretch stretches the bounding box by x in a given axis.
func (aabb AABB) Stretch(a cube.Axis, x float64) AABB {
switch a {
case cube.Y:
aabb.min[1] -= x
aabb.max[1] += x
case cube.Z:
aabb.min[2] -= x
aabb.max[2] += x
case cube.X:
aabb.min[0] -= x
aabb.max[0] += x
}
return aabb
}
// Translate moves the entire AABB with the Vec3 given. The (minimum and maximum) x, y and z coordinates are
// moved by those in the Vec3 passed.
func (aabb AABB) Translate(vec mgl64.Vec3) AABB {
return NewAABB(aabb.min.Add(vec), aabb.max.Add(vec))
}
// IntersectsWith checks if the AABB intersects with another AABB, returning true if this is the case.
func (aabb AABB) IntersectsWith(other AABB) bool {
if other.max[0]-aabb.min[0] > 1e-5 && aabb.max[0]-other.min[0] > 1e-5 {
if other.max[1]-aabb.min[1] > 1e-5 && aabb.max[1]-other.min[1] > 1e-5 {
return other.max[2]-aabb.min[2] > 1e-5 && aabb.max[2]-other.min[2] > 1e-5
}
}
return false
}
// AnyIntersections checks if any of boxes1 have intersections with any of boxes2 and returns true if this
// happens to be the case.
func AnyIntersections(boxes []AABB, search AABB) bool {
for _, box := range boxes {
if box.IntersectsWith(search) {
return true
}
}
return false
}
// Vec3Within checks if the AABB has a Vec3 within it, returning true if it does.
func (aabb AABB) Vec3Within(vec mgl64.Vec3) bool {
if vec[0] <= aabb.min[0] || vec[0] >= aabb.max[0] {
return false
}
if vec[2] <= aabb.min[2] || vec[2] >= aabb.max[2] {
return false
}
return vec[1] > aabb.min[1] && vec[1] < aabb.max[1]
}
// Vec3WithinYZ checks if the AABB has a Vec3 within its Y and Z bounds, returning true if it does.
func (aabb AABB) Vec3WithinYZ(vec mgl64.Vec3) bool {
if vec[2] < aabb.min[2] || vec[2] > aabb.max[2] {
return false
}
return vec[1] >= aabb.min[1] && vec[1] <= aabb.max[1]
}
// Vec3WithinXZ checks if the AABB has a Vec3 within its X and Z bounds, returning true if it does.
func (aabb AABB) Vec3WithinXZ(vec mgl64.Vec3) bool {
if vec[0] < aabb.min[0] || vec[0] > aabb.max[0] {
return false
}
return vec[2] >= aabb.min[2] && vec[2] <= aabb.max[2]
}
// Vec3WithinXY checks if the AABB has a Vec3 within its X and Y bounds, returning true if it does.
func (aabb AABB) Vec3WithinXY(vec mgl64.Vec3) bool {
if vec[0] < aabb.min[0] || vec[0] > aabb.max[0] {
return false
}
return vec[1] >= aabb.min[1] && vec[1] <= aabb.max[1]
}
// CalculateXOffset calculates the offset on the X axis between two bounding boxes, returning a delta always
// smaller than or equal to deltaX if deltaX is bigger than 0, or always bigger than or equal to deltaX if it
// is smaller than 0.
func (aabb AABB) CalculateXOffset(nearby AABB, deltaX float64) float64 {
// Bail out if not within the same Y/Z plane.
if aabb.max[1] <= nearby.min[1] || aabb.min[1] >= nearby.max[1] {
return deltaX
} else if aabb.max[2] <= nearby.min[2] || aabb.min[2] >= nearby.max[2] {
return deltaX
}
if deltaX > 0 && aabb.max[0] <= nearby.min[0] {
difference := nearby.min[0] - aabb.max[0]
if difference < deltaX {
deltaX = difference
}
}
if deltaX < 0 && aabb.min[0] >= nearby.max[0] {
difference := nearby.max[0] - aabb.min[0]
if difference > deltaX {
deltaX = difference
}
}
return deltaX
}
// CalculateYOffset calculates the offset on the Y axis between two bounding boxes, returning a delta always
// smaller than or equal to deltaY if deltaY is bigger than 0, or always bigger than or equal to deltaY if it
// is smaller than 0.
func (aabb AABB) CalculateYOffset(nearby AABB, deltaY float64) float64 {
// Bail out if not within the same X/Z plane.
if aabb.max[0] <= nearby.min[0] || aabb.min[0] >= nearby.max[0] {
return deltaY
} else if aabb.max[2] <= nearby.min[2] || aabb.min[2] >= nearby.max[2] {
return deltaY
}
if deltaY > 0 && aabb.max[1] <= nearby.min[1] {
difference := nearby.min[1] - aabb.max[1]
if difference < deltaY {
deltaY = difference
}
}
if deltaY < 0 && aabb.min[1] >= nearby.max[1] {
difference := nearby.max[1] - aabb.min[1]
if difference > deltaY {
deltaY = difference
}
}
return deltaY
}
// CalculateZOffset calculates the offset on the Z axis between two bounding boxes, returning a delta always
// smaller than or equal to deltaZ if deltaZ is bigger than 0, or always bigger than or equal to deltaZ if it
// is smaller than 0.
func (aabb AABB) CalculateZOffset(nearby AABB, deltaZ float64) float64 {
// Bail out if not within the same X/Y plane.
if aabb.max[0] <= nearby.min[0] || aabb.min[0] >= nearby.max[0] {
return deltaZ
} else if aabb.max[1] <= nearby.min[1] || aabb.min[1] >= nearby.max[1] {
return deltaZ
}
if deltaZ > 0 && aabb.max[2] <= nearby.min[2] {
difference := nearby.min[2] - aabb.max[2]
if difference < deltaZ {
deltaZ = difference
}
}
if deltaZ < 0 && aabb.min[2] >= nearby.max[2] {
difference := nearby.max[2] - aabb.min[2]
if difference > deltaZ {
deltaZ = difference
}
}
return deltaZ
}