-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollision-tests.js
226 lines (159 loc) · 4.01 KB
/
collision-tests.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
/**
* @function
* @param {THREE.Triangle} t1 - Triangular face
* @param {THREE.Triangle} t2 - Triangular face
* @returns {boolean} Whether the two triangles intersect
*/
function doTrianglesIntersect(t1, t2) {
/*
Adapated from section "4.1 Separation of Triangles" of:
- [Dynamic Collision Detection using Oriented Bounding Boxes](https://www.geometrictools.com/Documentation/DynamicCollisionDetection.pdf)
*/
// Triangle 1:
var A0 = t1.a;
var A1 = t1.b;
var A2 = t1.c;
var E0 = A1.clone().sub(A0);
var E1 = A2.clone().sub(A0);
var E2 = E1.clone().sub(E0);
var N = E0.clone().cross(E1);
// Triangle 2:
var B0 = t2.a;
var B1 = t2.b;
var B2 = t2.c;
var F0 = B1.clone().sub(B0);
var F1 = B2.clone().sub(B0);
var F2 = F1.clone().sub(F0);
var M = F0.clone().cross(F1);
var D = B0.clone().sub(A0);
function areProjectionsSeparated(p0, p1, p2, q0, q1, q2) {
var min_p = Math.min(p0, p1, p2),
max_p = Math.max(p0, p1, p2),
min_q = Math.min(q0, q1, q2),
max_q = Math.max(q0, q1, q2);
return ((min_p > max_q) || (max_p < min_q));
}
// Only potential separating axes for non-parallel and non-coplanar triangles are tested.
// Seperating axis: N
{
var p0 = 0,
p1 = 0,
p2 = 0,
q0 = N.dot(D),
q1 = q0 + N.dot(F0),
q2 = q0 + N.dot(F1);
if (areProjectionsSeparated(p0, p1, p2, q0, q1, q2))
return false;
}
// Separating axis: M
{
var p0 = 0,
p1 = M.dot(E0),
p2 = M.dot(E1),
q0 = M.dot(D),
q1 = q0,
q2 = q0;
if (areProjectionsSeparated(p0, p1, p2, q0, q1, q2))
return false;
}
// Seperating axis: E0 × F0
{
var p0 = 0,
p1 = 0,
p2 = -(N.dot(F0)),
q0 = E0.clone().cross(F0).dot(D),
q1 = q0,
q2 = q0 + M.dot(E0);
if (areProjectionsSeparated(p0, p1, p2, q0, q1, q2))
return false;
}
// Seperating axis: E0 × F1
{
var p0 = 0,
p1 = 0,
p2 = -(N.dot(F1)),
q0 = E0.clone().cross(F1).dot(D),
q1 = q0 - M.dot(E0),
q2 = q0;
if (areProjectionsSeparated(p0, p1, p2, q0, q1, q2))
return false;
}
// Seperating axis: E0 × F2
{
var p0 = 0,
p1 = 0,
p2 = -(N.dot(F2)),
q0 = E0.clone().cross(F2).dot(D),
q1 = q0 - M.dot(E0),
q2 = q1;
if (areProjectionsSeparated(p0, p1, p2, q0, q1, q2))
return false;
}
// Seperating axis: E1 × F0
{
var p0 = 0,
p1 = N.dot(F0),
p2 = 0,
q0 = E1.clone().cross(F0).dot(D),
q1 = q0,
q2 = q0 + M.dot(E1);
if (areProjectionsSeparated(p0, p1, p2, q0, q1, q2))
return false;
}
// Seperating axis: E1 × F1
{
var p0 = 0,
p1 = N.dot(F1),
p2 = 0,
q0 = E1.clone().cross(F1).dot(D),
q1 = q0 - M.dot(E1),
q2 = q0;
if (areProjectionsSeparated(p0, p1, p2, q0, q1, q2))
return false;
}
// Seperating axis: E1 × F2
{
var p0 = 0,
p1 = N.dot(F2),
p2 = 0,
q0 = E1.clone().cross(F2).dot(D),
q1 = q0 - M.dot(E1),
q2 = q1;
if (areProjectionsSeparated(p0, p1, p2, q0, q1, q2))
return false;
}
// Seperating axis: E2 × F0
{
var p0 = 0,
p1 = N.dot(F0),
p2 = p1,
q0 = E2.clone().cross(F0).dot(D),
q1 = q0,
q2 = q0 + M.dot(E2);
if (areProjectionsSeparated(p0, p1, p2, q0, q1, q2))
return false;
}
// Seperating axis: E2 × F1
{
var p0 = 0,
p1 = N.dot(F1),
p2 = p1,
q0 = E2.clone().cross(F1).dot(D),
q1 = q0 - M.dot(E2),
q2 = q0;
if (areProjectionsSeparated(p0, p1, p2, q0, q1, q2))
return false;
}
// Seperating axis: E2 × F2
{
var p0 = 0,
p1 = N.dot(F2),
p2 = p1,
q0 = E2.clone().cross(F2).dot(D),
q1 = q0 - M.dot(E2),
q2 = q1;
if (areProjectionsSeparated(p0, p1, p2, q0, q1, q2))
return false;
}
return true;
}