-
Notifications
You must be signed in to change notification settings - Fork 19
/
GjkEpa.js
767 lines (643 loc) · 27.5 KB
/
GjkEpa.js
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/**
* Provides the classes and algorithms for running GJK+EPA based collision detection
*
* @class GjkEpa
* @static
*/
Goblin.GjkEpa = {
margins: 0.03,
result: null,
max_iterations: 20,
epa_condition: 0.001,
/**
* Holds a point on the edge of a Minkowski difference along with that point's witnesses and the direction used to find the point
*
* @class SupportPoint
* @param witness_a {vec3} Point in first object used to find the supporting point
* @param witness_b {vec3} Point in the second object ued to find th supporting point
* @param point {vec3} The support point on the edge of the Minkowski difference
* @constructor
*/
SupportPoint: function( witness_a, witness_b, point ) {
this.witness_a = witness_a;
this.witness_b = witness_b;
this.point = point;
},
/**
* Finds the extant point on the edge of the Minkowski difference for `object_a` - `object_b` in `direction`
*
* @method findSupportPoint
* @param object_a {Goblin.RigidBody} First object in the search
* @param object_b {Goblin.RigidBody} Second object in the search
* @param direction {vec3} Direction to find the extant point in
* @param gjk_point {Goblin.GjkEpa.SupportPoint} `SupportPoint` class to store the resulting point & witnesses in
*/
findSupportPoint: (function(){
var temp = new Goblin.Vector3();
return function( object_a, object_b, direction, support_point ) {
// Find witnesses from the objects
object_a.findSupportPoint( direction, support_point.witness_a );
temp.scaleVector( direction, -1 );
object_b.findSupportPoint( temp, support_point.witness_b );
// Find the CSO support point
support_point.point.subtractVectors( support_point.witness_a, support_point.witness_b );
};
})(),
testCollision: function( object_a, object_b ) {
var simplex = Goblin.GjkEpa.GJK( object_a, object_b );
if ( Goblin.GjkEpa.result != null ) {
return Goblin.GjkEpa.result;
} else if ( simplex != null ) {
return Goblin.GjkEpa.EPA( simplex );
}
},
/**
* Perform GJK algorithm against two objects. Returns a ContactDetails object if there is a collision, else null
*
* @method GJK
* @param object_a {Goblin.RigidBody}
* @param object_b {Goblin.RigidBody}
* @return {Goblin.ContactDetails|Boolean} Returns `null` if no collision, else a `ContactDetails` object
*/
GJK: (function(){
return function( object_a, object_b ) {
var simplex = new Goblin.GjkEpa.Simplex( object_a, object_b ),
last_point;
Goblin.GjkEpa.result = null;
while ( ( last_point = simplex.addPoint() ) ){}
// If last_point is false then there is no collision
if ( last_point === false ) {
Goblin.GjkEpa.freeSimplex( simplex );
return null;
}
return simplex;
};
})(),
freeSimplex: function( simplex ) {
// Free the support points used by this simplex
for ( var i = 0, points_length = simplex.points.length; i < points_length; i++ ) {
Goblin.ObjectPool.freeObject( 'GJK2SupportPoint', simplex.points[i] );
}
},
freePolyhedron: function( polyhedron ) {
// Free the support points used by the polyhedron (includes the points from the simplex used to create the polyhedron
var pool = Goblin.ObjectPool.pools['GJK2SupportPoint'];
for ( var i = 0, faces_length = polyhedron.faces.length; i < faces_length; i++ ) {
// The indexOf checking is required because vertices are shared between faces
if ( pool.indexOf( polyhedron.faces[i].a ) === -1 ) {
Goblin.ObjectPool.freeObject( 'GJK2SupportPoint', polyhedron.faces[i].a );
}
if ( pool.indexOf( polyhedron.faces[i].b ) === -1 ) {
Goblin.ObjectPool.freeObject( 'GJK2SupportPoint', polyhedron.faces[i].b );
}
if ( pool.indexOf( polyhedron.faces[i].c ) === -1 ) {
Goblin.ObjectPool.freeObject( 'GJK2SupportPoint', polyhedron.faces[i].c );
}
}
},
/**
* Performs the Expanding Polytope Algorithm a GJK simplex
*
* @method EPA
* @param simplex {Goblin.GjkEpa.Simplex} Simplex generated by the GJK algorithm
* @return {Goblin.ContactDetails}
*/
EPA: (function(){
var barycentric = new Goblin.Vector3(),
confirm = {
a: new Goblin.Vector3(),
b: new Goblin.Vector3(),
c: new Goblin.Vector3()
};
return function( simplex ) {
// Time to convert the simplex to real faces
// @TODO this should be a priority queue where the position in the queue is ordered by distance from face to origin
var polyhedron = new Goblin.GjkEpa.Polyhedron( simplex );
var i = 0;
// Expand the polyhedron until it doesn't expand any more
while ( ++i ) {
polyhedron.findFaceClosestToOrigin();
// Find a new support point in the direction of the closest point
if ( polyhedron.closest_face_distance < Goblin.EPSILON ) {
_tmp_vec3_1.copy( polyhedron.faces[polyhedron.closest_face].normal );
} else {
_tmp_vec3_1.copy( polyhedron.closest_point );
}
var support_point = Goblin.ObjectPool.getObject( 'GJK2SupportPoint' );
Goblin.GjkEpa.findSupportPoint( simplex.object_a, simplex.object_b, _tmp_vec3_1, support_point );
// Check for terminating condition
_tmp_vec3_1.subtractVectors( support_point.point, polyhedron.closest_point );
var gap = _tmp_vec3_1.lengthSquared();
if ( i === Goblin.GjkEpa.max_iterations || ( gap < Goblin.GjkEpa.epa_condition && polyhedron.closest_face_distance > Goblin.EPSILON ) ) {
// Get a ContactDetails object and fill out its details
var contact = Goblin.ObjectPool.getObject( 'ContactDetails' );
contact.object_a = simplex.object_a;
contact.object_b = simplex.object_b;
contact.contact_normal.normalizeVector( polyhedron.closest_point );
if ( contact.contact_normal.lengthSquared() === 0 ) {
contact.contact_normal.subtractVectors( contact.object_b.position, contact.object_a.position );
}
contact.contact_normal.normalize();
Goblin.GeometryMethods.findBarycentricCoordinates( polyhedron.closest_point, polyhedron.faces[polyhedron.closest_face].a.point, polyhedron.faces[polyhedron.closest_face].b.point, polyhedron.faces[polyhedron.closest_face].c.point, barycentric );
if ( isNaN( barycentric.x ) ) {
// @TODO: Avoid this degenerate case
//console.log( 'Point not in triangle' );
//debugger;
Goblin.GjkEpa.freePolyhedron( polyhedron );
return null;
}
// Contact coordinates of object a
confirm.a.scaleVector( polyhedron.faces[polyhedron.closest_face].a.witness_a, barycentric.x );
confirm.b.scaleVector( polyhedron.faces[polyhedron.closest_face].b.witness_a, barycentric.y );
confirm.c.scaleVector( polyhedron.faces[polyhedron.closest_face].c.witness_a, barycentric.z );
contact.contact_point_in_a.addVectors( confirm.a, confirm.b );
contact.contact_point_in_a.add( confirm.c );
// Contact coordinates of object b
confirm.a.scaleVector( polyhedron.faces[polyhedron.closest_face].a.witness_b, barycentric.x );
confirm.b.scaleVector( polyhedron.faces[polyhedron.closest_face].b.witness_b, barycentric.y );
confirm.c.scaleVector( polyhedron.faces[polyhedron.closest_face].c.witness_b, barycentric.z );
contact.contact_point_in_b.addVectors( confirm.a, confirm.b );
contact.contact_point_in_b.add( confirm.c );
// Find actual contact point
contact.contact_point.addVectors( contact.contact_point_in_a, contact.contact_point_in_b );
contact.contact_point.scale( 0.5 );
// Set objects' local points
contact.object_a.transform_inverse.transformVector3( contact.contact_point_in_a );
contact.object_b.transform_inverse.transformVector3( contact.contact_point_in_b );
// Calculate penetration depth
contact.penetration_depth = polyhedron.closest_point.length() + Goblin.GjkEpa.margins;
contact.restitution = ( simplex.object_a.restitution + simplex.object_b.restitution ) / 2;
contact.friction = ( simplex.object_a.friction + simplex.object_b.friction ) / 2;
Goblin.GjkEpa.freePolyhedron( polyhedron );
return contact;
}
polyhedron.addVertex( support_point );
}
Goblin.GjkEpa.freePolyhedron( polyhedron );
return null;
};
})(),
Face: function( polyhedron, a, b, c ) {
this.active = true;
//this.polyhedron = polyhedron;
this.a = a;
this.b = b;
this.c = c;
this.normal = new Goblin.Vector3();
this.neighbors = [];
_tmp_vec3_1.subtractVectors( b.point, a.point );
_tmp_vec3_2.subtractVectors( c.point, a.point );
this.normal.crossVectors( _tmp_vec3_1, _tmp_vec3_2 );
this.normal.normalize();
}
};
Goblin.GjkEpa.Polyhedron = function( simplex ) {
this.closest_face = null;
this.closest_face_distance = null;
this.closest_point = new Goblin.Vector3();
this.faces = [
//BCD, ACB, CAD, DAB
new Goblin.GjkEpa.Face( this, simplex.points[2], simplex.points[1], simplex.points[0] ),
new Goblin.GjkEpa.Face( this, simplex.points[3], simplex.points[1], simplex.points[2] ),
new Goblin.GjkEpa.Face( this, simplex.points[1], simplex.points[3], simplex.points[0] ),
new Goblin.GjkEpa.Face( this, simplex.points[0], simplex.points[3], simplex.points[2] )
];
this.faces[0].neighbors.push( this.faces[1], this.faces[2], this.faces[3] );
this.faces[1].neighbors.push( this.faces[2], this.faces[0], this.faces[3] );
this.faces[2].neighbors.push( this.faces[1], this.faces[3], this.faces[0] );
this.faces[3].neighbors.push( this.faces[2], this.faces[1], this.faces[0] );
};
Goblin.GjkEpa.Polyhedron.prototype = {
addVertex: function( vertex )
{
var edges = [], faces = [], i, j, a, b, last_b;
this.faces[this.closest_face].silhouette( vertex, edges );
// Re-order the edges if needed
for ( i = 0; i < edges.length - 5; i += 5 ) {
a = edges[i+3];
b = edges[i+4];
// Ensure this edge really should be the next one
if ( i !== 0 && last_b !== a ) {
// It shouldn't
for ( j = i + 5; j < edges.length; j += 5 ) {
if ( edges[j+3] === last_b ) {
// Found it
var tmp = edges.slice( i, i + 5 );
edges[i] = edges[j];
edges[i+1] = edges[j+1];
edges[i+2] = edges[j+2];
edges[i+3] = edges[j+3];
edges[i+4] = edges[j+4];
edges[j] = tmp[0];
edges[j+1] = tmp[1];
edges[j+2] = tmp[2];
edges[j+3] = tmp[3];
edges[j+4] = tmp[4];
a = edges[i+3];
b = edges[i+4];
break;
}
}
}
last_b = b;
}
for ( i = 0; i < edges.length; i += 5 ) {
var neighbor = edges[i];
a = edges[i+3];
b = edges[i+4];
var face = new Goblin.GjkEpa.Face( this, b, vertex, a );
face.neighbors[2] = edges[i];
faces.push( face );
neighbor.neighbors[neighbor.neighbors.indexOf( edges[i+2] )] = face;
}
for ( i = 0; i < faces.length; i++ ) {
faces[i].neighbors[0] = faces[ i + 1 === faces.length ? 0 : i + 1 ];
faces[i].neighbors[1] = faces[ i - 1 < 0 ? faces.length - 1 : i - 1 ];
}
Array.prototype.push.apply( this.faces, faces );
return edges;
},
findFaceClosestToOrigin: (function(){
var origin = new Goblin.Vector3(),
point = new Goblin.Vector3();
return function() {
this.closest_face_distance = Infinity;
var distance, i;
for ( i = 0; i < this.faces.length; i++ ) {
if ( this.faces[i].active === false ) {
continue;
}
Goblin.GeometryMethods.findClosestPointInTriangle( origin, this.faces[i].a.point, this.faces[i].b.point, this.faces[i].c.point, point );
distance = point.lengthSquared();
if ( distance < this.closest_face_distance ) {
this.closest_face_distance = distance;
this.closest_face = i;
this.closest_point.copy( point );
}
}
};
})()
};
Goblin.GjkEpa.Face.prototype = {
/**
* Determines if a vertex is in front of or behind the face
*
* @method classifyVertex
* @param vertex {vec3} Vertex to classify
* @return {Number} If greater than 0 then `vertex' is in front of the face
*/
classifyVertex: function( vertex ) {
var w = this.normal.dot( this.a.point );
return this.normal.dot( vertex.point ) - w;
},
silhouette: function( point, edges, source ) {
if ( this.active === false ) {
return;
}
if ( this.classifyVertex( point ) > 0 ) {
// This face is visible from `point`. Deactivate this face and alert the neighbors
this.active = false;
this.neighbors[0].silhouette( point, edges, this );
this.neighbors[1].silhouette( point, edges, this );
this.neighbors[2].silhouette( point, edges, this );
} else if ( source ) {
// This face is a neighbor to a now-silhouetted face, determine which neighbor and replace it
var neighbor_idx = this.neighbors.indexOf( source ),
a, b;
if ( neighbor_idx === 0 ) {
a = this.a;
b = this.b;
} else if ( neighbor_idx === 1 ) {
a = this.b;
b = this.c;
} else {
a = this.c;
b = this.a;
}
edges.push( this, neighbor_idx, source, b, a );
}
}
};
(function(){
var origin = new Goblin.Vector3(),
ao = new Goblin.Vector3(),
ab = new Goblin.Vector3(),
ac = new Goblin.Vector3(),
ad = new Goblin.Vector3();
var barycentric = new Goblin.Vector3(),
confirm = {
a: new Goblin.Vector3(),
b: new Goblin.Vector3(),
c: new Goblin.Vector3()
};
Goblin.GjkEpa.Simplex = function( object_a, object_b ) {
this.object_a = object_a;
this.object_b = object_b;
this.points = [];
this.iterations = 0;
this.next_direction = new Goblin.Vector3();
this.updateDirection();
};
Goblin.GjkEpa.Simplex.prototype = {
addPoint: function() {
if ( ++this.iterations === Goblin.GjkEpa.max_iterations ) {
return false;
}
var support_point = Goblin.ObjectPool.getObject( 'GJK2SupportPoint' );
Goblin.GjkEpa.findSupportPoint( this.object_a, this.object_b, this.next_direction, support_point );
this.points.push( support_point );
if ( support_point.point.dot( this.next_direction ) < 0 && this.points.length > 1 ) {
// Check the margins first
// @TODO this can be expanded to support 1-simplex (2 points)
if ( this.points.length >= 3 ) {
Goblin.GeometryMethods.findClosestPointInTriangle(
origin,
this.points[0].point,
this.points[1].point,
this.points[2].point,
_tmp_vec3_1
);
var distanceSquared = _tmp_vec3_1.lengthSquared();
if ( distanceSquared <= Goblin.GjkEpa.margins * Goblin.GjkEpa.margins ) {
// Get a ContactDetails object and fill out its details
var contact = Goblin.ObjectPool.getObject( 'ContactDetails' );
contact.object_a = this.object_a;
contact.object_b = this.object_b;
contact.contact_normal.normalizeVector( _tmp_vec3_1 );
if ( contact.contact_normal.lengthSquared() === 0 ) {
contact.contact_normal.subtractVectors( contact.object_b.position, contact.object_a.position );
}
contact.contact_normal.normalize();
contact.contact_normal.scale( -1 );
contact.penetration_depth = Goblin.GjkEpa.margins - Math.sqrt( distanceSquared );
Goblin.GeometryMethods.findBarycentricCoordinates( _tmp_vec3_1, this.points[0].point, this.points[1].point, this.points[2].point, barycentric );
if ( isNaN( barycentric.x ) ) {
//debugger;
return false;
}
// Contact coordinates of object a
confirm.a.scaleVector( this.points[0].witness_a, barycentric.x );
confirm.b.scaleVector( this.points[1].witness_a, barycentric.y );
confirm.c.scaleVector( this.points[2].witness_a, barycentric.z );
contact.contact_point_in_a.addVectors( confirm.a, confirm.b );
contact.contact_point_in_a.add( confirm.c );
// Contact coordinates of object b
contact.contact_point_in_b.scaleVector( contact.contact_normal, -contact.penetration_depth );
contact.contact_point_in_b.add( contact.contact_point_in_a );
// Find actual contact point
contact.contact_point.addVectors( contact.contact_point_in_a, contact.contact_point_in_b );
contact.contact_point.scale( 0.5 );
// Set objects' local points
contact.object_a.transform_inverse.transformVector3( contact.contact_point_in_a );
contact.object_b.transform_inverse.transformVector3( contact.contact_point_in_b );
contact.restitution = ( this.object_a.restitution + this.object_b.restitution ) / 2;
contact.friction = ( this.object_a.friction + this.object_b.friction ) / 2;
//Goblin.GjkEpa.freePolyhedron( polyhedron );
Goblin.GjkEpa.result = contact;
return null;
}
}
// if the last added point was not past the origin in the direction
// then the Minkowski difference cannot contain the origin because
// point added is past the edge of the Minkowski difference
return false;
}
if ( this.updateDirection() === true ) {
// Found a collision
return null;
}
return support_point;
},
findDirectionFromLine: function() {
ao.scaleVector( this.points[1].point, -1 );
ab.subtractVectors( this.points[0].point, this.points[1].point );
if ( ab.dot( ao ) < 0 ) {
// Origin is on the opposite side of A from B
this.next_direction.copy( ao );
Goblin.ObjectPool.freeObject( 'GJK2SupportPoint', this.points[1] );
this.points.length = 1; // Remove second point
} else {
// Origin lies between A and B, move on to a 2-simplex
this.next_direction.crossVectors( ab, ao );
this.next_direction.cross( ab );
// In the case that `ab` and `ao` are parallel vectors, direction becomes a 0-vector
if (
this.next_direction.x === 0 &&
this.next_direction.y === 0 &&
this.next_direction.z === 0
) {
ab.normalize();
this.next_direction.x = 1 - Math.abs( ab.x );
this.next_direction.y = 1 - Math.abs( ab.y );
this.next_direction.z = 1 - Math.abs( ab.z );
}
}
},
findDirectionFromTriangle: function() {
// Triangle
var a = this.points[2],
b = this.points[1],
c = this.points[0];
ao.scaleVector( a.point, -1 ); // ao
ab.subtractVectors( b.point, a.point ); // ab
ac.subtractVectors( c.point, a.point ); // ac
// Determine the triangle's normal
_tmp_vec3_1.crossVectors( ab, ac );
// Edge cross products
_tmp_vec3_2.crossVectors( ab, _tmp_vec3_1 );
_tmp_vec3_3.crossVectors( _tmp_vec3_1, ac );
if ( _tmp_vec3_3.dot( ao ) >= 0 ) {
// Origin lies on side of ac opposite the triangle
if ( ac.dot( ao ) >= 0 ) {
// Origin outside of the ac line, so we form a new
// 1-simplex (line) with points A and C, leaving B behind
this.points.length = 0;
this.points.push( c, a );
Goblin.ObjectPool.freeObject( 'GJK2SupportPoint', b );
// New search direction is from ac towards the origin
this.next_direction.crossVectors( ac, ao );
this.next_direction.cross( ac );
} else {
// *
if ( ab.dot( ao ) >= 0 ) {
// Origin outside of the ab line, so we form a new
// 1-simplex (line) with points A and B, leaving C behind
this.points.length = 0;
this.points.push( b, a );
Goblin.ObjectPool.freeObject( 'GJK2SupportPoint', c );
// New search direction is from ac towards the origin
this.next_direction.crossVectors( ab, ao );
this.next_direction.cross( ab );
} else {
// only A gives us a good reference point, start over with a 0-simplex
this.points.length = 0;
this.points.push( a );
Goblin.ObjectPool.freeObject( 'GJK2SupportPoint', b );
Goblin.ObjectPool.freeObject( 'GJK2SupportPoint', c );
}
// *
}
} else {
// Origin lies on the triangle side of ac
if ( _tmp_vec3_2.dot( ao ) >= 0 ) {
// Origin lies on side of ab opposite the triangle
// *
if ( ab.dot( ao ) >= 0 ) {
// Origin outside of the ab line, so we form a new
// 1-simplex (line) with points A and B, leaving C behind
this.points.length = 0;
this.points.push( b, a );
Goblin.ObjectPool.freeObject( 'GJK2SupportPoint', c );
// New search direction is from ac towards the origin
this.next_direction.crossVectors( ab, ao );
this.next_direction.cross( ab );
} else {
// only A gives us a good reference point, start over with a 0-simplex
this.points.length = 0;
this.points.push( a );
Goblin.ObjectPool.freeObject( 'GJK2SupportPoint', b );
Goblin.ObjectPool.freeObject( 'GJK2SupportPoint', c );
}
// *
} else {
// Origin lies somewhere in the triangle or above/below it
if ( _tmp_vec3_1.dot( ao ) >= 0 ) {
// Origin is on the front side of the triangle
this.next_direction.copy( _tmp_vec3_1 );
this.points.length = 0;
this.points.push( a, b, c );
} else {
// Origin is on the back side of the triangle
this.next_direction.copy( _tmp_vec3_1 );
this.next_direction.scale( -1 );
}
}
}
},
getFaceNormal: function( a, b, c, destination ) {
ab.subtractVectors( b.point, a.point );
ac.subtractVectors( c.point, a.point );
destination.crossVectors( ab, ac );
destination.normalize();
},
faceNormalDotOrigin: function( a, b, c ) {
// Find face normal
this.getFaceNormal( a, b, c, _tmp_vec3_1 );
// Find direction of origin from center of face
_tmp_vec3_2.addVectors( a.point, b.point );
_tmp_vec3_2.add( c.point );
_tmp_vec3_2.scale( -3 );
_tmp_vec3_2.normalize();
return _tmp_vec3_1.dot( _tmp_vec3_2 );
},
findDirectionFromTetrahedron: function() {
var a = this.points[3],
b = this.points[2],
c = this.points[1],
d = this.points[0];
// Check each of the four sides to see which one is facing the origin.
// Then keep the three points for that triangle and use its normal as the search direction
// The four faces are BCD, ACB, CAD, DAB
var closest_face = null,
closest_dot = Goblin.EPSILON,
face_dot;
// @TODO we end up calculating the "winning" face normal twice, don't do that
face_dot = this.faceNormalDotOrigin( b, c, d );
if ( face_dot > closest_dot ) {
closest_face = 1;
closest_dot = face_dot;
}
face_dot = this.faceNormalDotOrigin( a, c, b );
if ( face_dot > closest_dot ) {
closest_face = 2;
closest_dot = face_dot;
}
face_dot = this.faceNormalDotOrigin( c, a, d );
if ( face_dot > closest_dot ) {
closest_face = 3;
closest_dot = face_dot;
}
face_dot = this.faceNormalDotOrigin( d, a, b );
if ( face_dot > closest_dot ) {
closest_face = 4;
closest_dot = face_dot;
}
if ( closest_face === null ) {
// We have a collision, ready for EPA
return true;
} else if ( closest_face === 1 ) {
// BCD
this.points.length = 0;
this.points.push( b, c, d );
this.getFaceNormal( b, c, d, _tmp_vec3_1 );
this.next_direction.copy( _tmp_vec3_1 );
} else if ( closest_face === 2 ) {
// ACB
this.points.length = 0;
this.points.push( a, c, b );
this.getFaceNormal( a, c, b, _tmp_vec3_1 );
this.next_direction.copy( _tmp_vec3_1 );
} else if ( closest_face === 3 ) {
// CAD
this.points.length = 0;
this.points.push( c, a, d );
this.getFaceNormal( c, a, d, _tmp_vec3_1 );
this.next_direction.copy( _tmp_vec3_1 );
} else if ( closest_face === 4 ) {
// DAB
this.points.length = 0;
this.points.push( d, a, b );
this.getFaceNormal( d, a, b, _tmp_vec3_1 );
this.next_direction.copy( _tmp_vec3_1 );
}
},
containsOrigin: function() {
var a = this.points[3],
b = this.points[2],
c = this.points[1],
d = this.points[0];
// Check DCA
ab.subtractVectors( d.point, a.point );
ad.subtractVectors( c.point, a.point );
_tmp_vec3_1.crossVectors( ab, ad );
if ( _tmp_vec3_1.dot( a.point ) > 0 ) {
return false;
}
// Check CBA
ab.subtractVectors( c.point, a.point );
ad.subtractVectors( b.point, a.point );
_tmp_vec3_1.crossVectors( ab, ad );
if ( _tmp_vec3_1.dot( a.point ) > 0 ) {
return false;
}
// Check ADB
ab.subtractVectors( b.point, a.point );
ad.subtractVectors( d.point, a.point );
_tmp_vec3_1.crossVectors( ab, ad );
if ( _tmp_vec3_1.dot( a.point ) > 0 ) {
return false;
}
// Check DCB
ab.subtractVectors( d.point, c.point );
ad.subtractVectors( b.point, c.point );
_tmp_vec3_1.crossVectors( ab, ad );
if ( _tmp_vec3_1.dot( d.point ) > 0 ) {
return false;
}
return true;
},
updateDirection: function() {
if ( this.points.length === 0 ) {
this.next_direction.subtractVectors( this.object_b.position, this.object_a.position );
} else if ( this.points.length === 1 ) {
this.next_direction.scale( -1 );
} else if ( this.points.length === 2 ) {
this.findDirectionFromLine();
} else if ( this.points.length === 3 ) {
this.findDirectionFromTriangle();
} else {
return this.findDirectionFromTetrahedron();
}
}
};
})();