-
Notifications
You must be signed in to change notification settings - Fork 18
/
boundsAABB.go
274 lines (218 loc) · 7.61 KB
/
boundsAABB.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package tetra3d
import "github.com/solarlune/tetra3d/math32"
// BoundingAABB represents a 3D AABB (Axis-Aligned Bounding Box), a 3D cube of varying width, height, and depth that cannot rotate.
// The primary purpose of a BoundingAABB is, like the other Bounding* Nodes, to perform intersection testing between itself and other
// BoundingObject Nodes.
type BoundingAABB struct {
*Node
internalSize Vector3
Dimensions Dimensions // Dimensions represents the size of the AABB after transformation.
}
// NewBoundingAABB returns a new BoundingAABB Node.
func NewBoundingAABB(name string, width, height, depth float32) *BoundingAABB {
min := float32(0.0001)
if width <= 0 {
width = min
}
if height <= 0 {
height = min
}
if depth <= 0 {
depth = min
}
bounds := &BoundingAABB{
Node: NewNode(name),
internalSize: Vector3{width, height, depth},
}
bounds.Node.onTransformUpdate = bounds.updateSize
bounds.updateSize()
bounds.owner = bounds
return bounds
}
// updateSize updates the BoundingAABB's external Dimensions property to reflect its size after reposition, rotation, or resizing.
// This is be called automatically internally as necessary after the node's transform is updated.
func (box *BoundingAABB) updateSize() {
_, s, r := box.Node.Transform().Decompose()
corners := [][]float32{
{1, 1, 1},
{1, -1, 1},
{-1, 1, 1},
{-1, -1, 1},
{1, 1, -1},
{1, -1, -1},
{-1, 1, -1},
{-1, -1, -1},
}
dimensions := NewEmptyDimensions()
for _, c := range corners {
position := r.MultVec(Vector3{
box.internalSize.X * c[0] * s.X / 2,
box.internalSize.Y * c[1] * s.Y / 2,
box.internalSize.Z * c[2] * s.Z / 2,
})
if dimensions.Min.X > position.X {
dimensions.Min.X = position.X
}
if dimensions.Min.Y > position.Y {
dimensions.Min.Y = position.Y
}
if dimensions.Min.Z > position.Z {
dimensions.Min.Z = position.Z
}
if dimensions.Max.X < position.X {
dimensions.Max.X = position.X
}
if dimensions.Max.Y < position.Y {
dimensions.Max.Y = position.Y
}
if dimensions.Max.Z < position.Z {
dimensions.Max.Z = position.Z
}
}
box.Dimensions = dimensions
}
// SetDimensions sets the BoundingAABB's internal dimensions (prior to resizing or rotating the Node).
func (box *BoundingAABB) SetDimensions(newWidth, newHeight, newDepth float32) {
min := float32(0.00001)
if newWidth <= 0 {
newWidth = min
}
if newHeight <= 0 {
newHeight = min
}
if newDepth <= 0 {
newDepth = min
}
if box.internalSize.X != newWidth || box.internalSize.Y != newHeight || box.internalSize.Z != newDepth {
box.internalSize.X = newWidth
box.internalSize.Y = newHeight
box.internalSize.Z = newDepth
box.updateSize()
}
}
// Clone returns a new BoundingAABB.
func (box *BoundingAABB) Clone() INode {
clone := NewBoundingAABB(box.name, box.internalSize.X, box.internalSize.Y, box.internalSize.Z)
clone.Node = box.Node.clone(clone).(*Node)
clone.Node.onTransformUpdate = clone.updateSize
if clone.Callbacks() != nil && clone.Callbacks().OnClone != nil {
clone.Callbacks().OnClone(clone)
}
return clone
}
// ClosestPoint returns the closest point, to the point given, on the inside or surface of the BoundingAABB
// in world space.
func (box *BoundingAABB) ClosestPoint(point Vector3) Vector3 {
out := point
pos := box.WorldPosition()
half := box.Dimensions.Size().Scale(0.5)
if out.X > pos.X+half.X {
out.X = pos.X + half.X
} else if out.X < pos.X-half.X {
out.X = pos.X - half.X
}
if out.Y > pos.Y+half.Y {
out.Y = pos.Y + half.Y
} else if out.Y < pos.Y-half.Y {
out.Y = pos.Y - half.Y
}
if out.Z > pos.Z+half.Z {
out.Z = pos.Z + half.Z
} else if out.Z < pos.Z-half.Z {
out.Z = pos.Z - half.Z
}
return out
}
// normalFromContactPoint guesses which normal to return for an AABB given an MTV vector. Basically, if you have an MTV vector indicating a sphere, for example,
// moves up by 0.1 when colliding with an AABB, it must be colliding with the top, and so the returned normal would be [0, 1, 0].
func (box *BoundingAABB) normalFromContactPoint(contactPoint Vector3) Vector3 {
if contactPoint.Equals(box.WorldPosition()) {
return Vector3{}
}
p := contactPoint.Sub(box.WorldPosition())
d := Vector3{
box.Dimensions.Width() / 2,
box.Dimensions.Height() / 2,
box.Dimensions.Depth() / 2,
}
nx := p.X / d.X
ny := p.Y / d.Y
nz := p.Z / d.Z
if math32.Abs(nx) > math32.Abs(ny) && math32.Abs(nx) > math32.Abs(nz) {
return Vector3{nx, 0, 0}.Unit()
} else if math32.Abs(ny) > math32.Abs(nx) && math32.Abs(ny) > math32.Abs(nz) {
return Vector3{0, ny, 0}.Unit()
}
return Vector3{0, 0, nz}.Unit()
}
// Colliding returns true if the BoundingAABB collides with another IBoundingObject.
func (box *BoundingAABB) Colliding(other IBoundingObject) bool {
return box.Collision(other) != nil
}
// ContainsAABB returns if the calling BoundingAABB contains the provided other BoundingAABB.
func (box *BoundingAABB) ContainsAABB(other *BoundingAABB) bool {
mePos := box.WorldPosition()
meMin := mePos.Sub(box.Dimensions.Center())
meMax := mePos.Add(box.Dimensions.Center())
otherPos := other.WorldPosition()
otherMin := otherPos.Sub(other.Dimensions.Center())
otherMax := otherPos.Add(other.Dimensions.Center())
return otherMin.X > meMin.X && otherMin.Y > meMin.Y && otherMin.Z > meMin.Z && otherMax.X < meMax.X && otherMax.Y < meMax.Y && otherMax.Z < meMax.Z
}
// Collision returns the Collision between the BoundingAABB and the other IBoundingObject. If
// there is no intersection, the function returns nil. (Note that BoundingAABB > BoundingTriangles collision
// is buggy at the moment.)
func (box *BoundingAABB) Collision(other IBoundingObject) *Collision {
if other == box || other == nil {
return nil
}
switch otherBounds := other.(type) {
case *BoundingAABB:
return btAABBAABB(box, otherBounds)
case *BoundingSphere:
intersection := btSphereAABB(otherBounds, box)
if intersection != nil {
for _, inter := range intersection.Intersections {
inter.MTV = inter.MTV.Invert()
inter.Normal = inter.Normal.Invert()
}
intersection.BoundingObject = otherBounds
}
return intersection
case *BoundingTriangles:
return btAABBTriangles(box, otherBounds)
case *BoundingCapsule:
intersection := btCapsuleAABB(otherBounds, box)
if intersection != nil {
for _, inter := range intersection.Intersections {
inter.MTV = inter.MTV.Invert()
inter.Normal = inter.Normal.Invert()
}
intersection.BoundingObject = otherBounds
}
return intersection
}
panic("Unimplemented bounds type")
}
// CollisionTest performs a collision test using the provided collision test settings structure.
// Collisions reported will be sorted in distance from closest to furthest.
// The function will return if a collision was found with the sphere at the settings specified.
func (box *BoundingAABB) CollisionTest(settings CollisionTestSettings) bool {
return commonCollisionTest(box, settings)
}
func (box *BoundingAABB) PointInside(point Vector3) bool {
position := box.WorldPosition()
min := box.Dimensions.Min.Add(position)
max := box.Dimensions.Max.Add(position)
margin := float32(0.01)
if point.X >= min.X-margin && point.X <= max.X+margin &&
point.Y >= min.Y-margin && point.Y <= max.Y+margin &&
point.Z >= min.Z-margin && point.Z <= max.Z+margin {
return true
}
return false
}
// Type returns the NodeType for this object.
func (box *BoundingAABB) Type() NodeType {
return NodeTypeBoundingAABB
}