-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathboundsTriangles.go
278 lines (208 loc) · 8.26 KB
/
boundsTriangles.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
275
276
277
278
package tetra3d
import "github.com/solarlune/tetra3d/math32"
// BoundingTriangles is a Node specifically for detecting a collision between any of the triangles from a mesh instance and another BoundingObject.
type BoundingTriangles struct {
*Node
BoundingAABB *BoundingAABB
Broadphase *Broadphase
Mesh *Mesh
}
// NewBoundingTriangles returns a new BoundingTriangles object. name is the name of the BoundingTriangles node, while mesh is a reference
// to the Mesh that the BoundingTriangles object should use for collision. broadphaseCellSize is how large the BoundingTriangle's broadphase
// collision cells should be - larger cells means more triangles are checked at a time when doing collision checks, while larger cells also means
// fewer broadphase cells need to be checked. Striking a balance is a good idea if you're setting this value by hand (by default, the grid size is
// the maximum dimension size / 20, rounded up (a grid of at least one cell every 20 Blender Units). A size of 0 disables the usage of the broadphase
// for collision checks.
func NewBoundingTriangles(name string, mesh *Mesh, broadphaseGridSize float32) *BoundingTriangles {
margin := float32(0.25) // An additional margin to help ensure the broadphase is crossed before checking for collisions
bt := &BoundingTriangles{
Node: NewNode(name),
BoundingAABB: NewBoundingAABB("triangle broadphase aabb", mesh.Dimensions.Width()+margin, mesh.Dimensions.Height()+margin, mesh.Dimensions.Depth()+margin),
Mesh: mesh,
}
bt.Node.onTransformUpdate = bt.UpdateTransform
// This initializes the broadphase using a default grid size.
// If the object is too small (less than 5 units large), it may not be worth doing
maxDim := bt.Mesh.Dimensions.MaxDimension()
gridSize := 0
if broadphaseGridSize > 0 {
gridSize = int(math32.Ceil(maxDim / broadphaseGridSize))
}
bt.Broadphase = NewBroadphase(gridSize, bt.WorldPosition(), mesh)
bt.owner = bt
return bt
}
// DisableBroadphase turns off the broadphase system for collision detection by settings its grid and cell size to 0.
// To turn broadphase collision back on, simply call Broadphase.Resize(gridSize) with a gridSize value above 0.
func (bt *BoundingTriangles) DisableBroadphase() {
bt.Broadphase.Resize(0)
}
// Transform returns a Matrix4 indicating the global position, rotation, and scale of the object, transforming it by any parents'.
// If there's no change between the previous Transform() call and this one, Transform() will return a cached version of the
// transform for efficiency.
func (bt *BoundingTriangles) UpdateTransform() {
transform := bt.Node.Transform()
bt.BoundingAABB.SetWorldTransform(transform)
rot := bt.WorldRotation().MultVec(bt.Mesh.Dimensions.Center())
bt.BoundingAABB.MoveVec(rot)
bt.BoundingAABB.Transform()
if bt.Broadphase != nil {
bt.Broadphase.center.SetWorldTransform(transform)
bt.Broadphase.center.MoveVec(rot)
bt.Broadphase.center.Transform() // Update the transform
}
}
// Clone returns a new BoundingTriangles Node with the same values set as the original.
func (bt *BoundingTriangles) Clone() INode {
clone := NewBoundingTriangles(bt.name, bt.Mesh, 0) // Broadphase size is set to 0 so cloning doesn't create the broadphase triangle sets
clone.Broadphase = bt.Broadphase.Clone()
clone.Node = bt.Node.clone(clone).(*Node)
clone.Node.onTransformUpdate = clone.UpdateTransform
if clone.Callbacks() != nil && clone.Callbacks().OnClone != nil {
clone.Callbacks().OnClone(clone)
}
return clone
}
// Colliding returns true if the BoundingTriangles object is intersecting the other specified BoundingObject.
func (bt *BoundingTriangles) Colliding(other IBoundingObject) bool {
return bt.Collision(other) != nil
}
// Collision returns a Collision if the BoundingTriangles object is intersecting another BoundingObject. If
// no intersection is reported, Collision returns nil. (Note that BoundingTriangles > AABB collision is buggy at the moment.)
func (bt *BoundingTriangles) Collision(other IBoundingObject) *Collision {
if other == bt || other == nil {
return nil
}
switch otherBounds := other.(type) {
case *BoundingAABB:
intersection := otherBounds.Collision(bt)
if intersection != nil {
for _, inter := range intersection.Intersections {
inter.MTV = inter.MTV.Invert()
inter.Normal = inter.Normal.Invert()
}
intersection.BoundingObject = otherBounds
}
return intersection
case *BoundingSphere:
intersection := otherBounds.Collision(bt)
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 btTrianglesTriangles(bt, otherBounds)
case *BoundingCapsule:
intersection := otherBounds.Collision(bt)
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 (bt *BoundingTriangles) CollisionTest(settings CollisionTestSettings) bool {
return commonCollisionTest(bt, settings)
}
type collisionPlane struct {
Normal Vector3
Distance float32
}
func newCollisionPlane() collisionPlane {
return collisionPlane{}
}
func (plane *collisionPlane) Set(v0, v1, v2 Vector3) {
first := v1.Sub(v0)
second := v2.Sub(v0)
normal := first.Cross(second).Unit()
distance := normal.Dot(v0)
plane.Normal = normal
plane.Distance = distance
}
func (plane *collisionPlane) ClosestPoint(point Vector3) Vector3 {
dist := plane.Normal.Dot(point) - plane.Distance
return point.Sub(plane.Normal.Scale(dist))
}
func (plane *collisionPlane) RayAgainstPlane(from, to Vector3, doublesided bool) (Vector3, bool) {
dir := to.Sub(from).Unit()
nd := dir.Dot(plane.Normal)
pn := from.Dot(plane.Normal)
if !doublesided && nd >= 0 {
// if !doublesided && nd <= 0 {
return Vector3{}, false
}
t := (plane.Distance - pn) / nd
if t >= 0 {
return from.Add(dir.Scale(t)), true
}
return Vector3{}, false
}
var colPlane = newCollisionPlane()
func closestPointOnTri(point, v0, v1, v2 Vector3) Vector3 {
colPlane.Set(v0, v1, v2)
if planePoint := colPlane.ClosestPoint(point); isPointInsideTriangle(planePoint, v0, v1, v2) {
return planePoint
}
ab := colPlane.closestPointOnLine(point, v0, v1)
bc := colPlane.closestPointOnLine(point, v1, v2)
ca := colPlane.closestPointOnLine(point, v2, v0)
closest := ab
closestDist := point.DistanceSquared(ab)
bcDist := point.DistanceSquared(bc)
caDist := point.DistanceSquared(ca)
if bcDist < closestDist {
closest = bc
closestDist = bcDist
}
if caDist < closestDist {
closest = ca
}
return closest
}
func isPointInsideTriangle(point, v0, v1, v2 Vector3) bool {
u, v := pointInsideTriangle(point, v0, v1, v2)
return (u >= 0) && (v >= 0) && (u+v < 1)
}
func pointInsideTriangle(point, v0, v1, v2 Vector3) (u, v float32) {
ca := v2.Sub(v0)
ba := v1.Sub(v0)
pa := point.Sub(v0)
dot00 := ca.Dot(ca)
dot01 := ca.Dot(ba)
dot02 := ca.Dot(pa)
dot11 := ba.Dot(ba)
dot12 := ba.Dot(pa)
invDenom := 1.0 / ((dot00 * dot11) - (dot01 * dot01))
u = ((dot11 * dot02) - (dot01 * dot12)) * invDenom
v = ((dot00 * dot12) - (dot01 * dot02)) * invDenom
// return (u >= 0) && (v >= 0) && (u+v < 1)
return
}
func (plane *collisionPlane) closestPointOnLine(point, start, end Vector3) Vector3 {
diff := end.Sub(start)
dotA := point.Sub(start).Dot(diff)
dotB := diff.Dot(diff)
d := dotA / dotB
if d > 1 {
d = 1
} else if d < 0 {
d = 0
}
return start.Add(diff.Scale(d))
}
/////
// Type returns the NodeType for this object.
func (bt *BoundingTriangles) Type() NodeType {
return NodeTypeBoundingTriangles
}