-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcolliderAABB.go
More file actions
296 lines (238 loc) · 8.02 KB
/
Copy pathcolliderAABB.go
File metadata and controls
296 lines (238 loc) · 8.02 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package tetra3d
import (
"github.com/solarlune/tetra3d/math32"
)
// ColliderAABB 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 ColliderAABB is, like the other colliders, to perform intersection testing between itself and other
// Collider Nodes.
type ColliderAABB struct {
*Node
internalSize Vector3
dimensions Dimensions // Dimensions represents the size of the AABB after transformation.
}
// NewColliderAABB returns a new ColliderAABB Node.
func NewColliderAABB(name string, width, height, depth float32) *ColliderAABB {
min := float32(0.001)
if width <= min {
width = min
}
if height <= min {
height = min
}
if depth <= min {
depth = min
}
collider := &ColliderAABB{
Node: NewNode(name),
internalSize: Vector3{width, height, depth},
}
collider.Node.onTransformUpdate = collider.updateSize
collider.updateSize()
collider.owner = collider
return collider
}
func NewColliderFromDimensions(name string, dim Dimensions) *ColliderAABB {
collider := &ColliderAABB{
Node: NewNode(name),
internalSize: dim.Size(),
}
collider.SetWorldPositionVec(dim.Center())
collider.Node.onTransformUpdate = collider.updateSize
collider.updateSize()
collider.owner = collider
return collider
}
// updateSize updates the ColliderAABB'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 *ColliderAABB) 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 ColliderAABB's internal dimensions (prior to resizing or rotating the Node).
func (box *ColliderAABB) 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()
}
}
func (box *ColliderAABB) Dimensions() Dimensions {
// The dimensions are already scaled and include rotation, so rather than transform,
// they just need to be moved by the AABB's world position to be accurate
dim := box.dimensions
dim.Max = dim.Max.Add(box.WorldPosition())
dim.Min = dim.Min.Add(box.WorldPosition())
return dim.Canon()
}
// Clone returns a new ColliderAABB.
func (box *ColliderAABB) Clone() INode {
clone := NewColliderAABB(box.name, box.internalSize.X, box.internalSize.Y, box.internalSize.Z)
clone.Node = box.Node.clone(clone).(*Node)
clone.Node.onTransformUpdate = clone.updateSize
if runCallbacks && 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 ColliderAABB
// in world space.
func (box *ColliderAABB) ClosestPoint(point Vector3) Vector3 {
pos := box.WorldPosition()
half := box.dimensions.Size().Scale(0.5)
if point.X > pos.X+half.X {
point.X = pos.X + half.X
} else if point.X < pos.X-half.X {
point.X = pos.X - half.X
}
if point.Y > pos.Y+half.Y {
point.Y = pos.Y + half.Y
} else if point.Y < pos.Y-half.Y {
point.Y = pos.Y - half.Y
}
if point.Z > pos.Z+half.Z {
point.Z = pos.Z + half.Z
} else if point.Z < pos.Z-half.Z {
point.Z = pos.Z - half.Z
}
return point
}
// 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 *ColliderAABB) 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 ColliderAABB collides with another Collider.
func (box *ColliderAABB) Colliding(other Collider) bool {
return box.Collision(other) != nil
}
// ContainsAABB returns if the calling ColliderAABB contains the provided other ColliderAABB.
func (box *ColliderAABB) ContainsAABB(other *ColliderAABB) 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 ColliderAABB and the other Collider. If
// there is no intersection, the function returns nil. (Note that ColliderAABB > ColliderTriangles collision
// is buggy at the moment.)
func (box *ColliderAABB) Collision(other Collider) *Collision {
if other == box || other == nil {
return nil
}
switch otherCollider := other.(type) {
case *ColliderAABB:
return btAABBAABB(box, otherCollider)
case *ColliderSphere:
intersection := btSphereAABB(otherCollider, box)
if intersection != nil {
for _, inter := range intersection.Intersections {
inter.MTV = inter.MTV.Invert()
inter.Normal = inter.Normal.Invert()
}
intersection.Object = otherCollider
}
return intersection
case *ColliderTriangles:
return btAABBTriangles(box, otherCollider)
case *ColliderCapsule:
intersection := btCapsuleAABB(otherCollider, box)
if intersection != nil {
for _, inter := range intersection.Intersections {
inter.MTV = inter.MTV.Invert()
inter.Normal = inter.Normal.Invert()
}
intersection.Object = otherCollider
}
return intersection
}
panic("Unimplemented collider 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 the first collision found with the object; if no collision is found, then it returns nil.
func (box *ColliderAABB) CollisionTest(settings CollisionTestSettings) *Collision {
return commonCollisionTest(box, settings)
}
func (box *ColliderAABB) 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 *ColliderAABB) Type() NodeType {
return NodeTypeColliderAABB
}