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
|
#include "Arbiter.h"
#include "Body.h"
#include "World.h"
#include "Joint.h"
using namespace Phy2D;
// Box vertex and edge numbering:
//
// ^ y
// |
// e1
// v2 ------ v1
// | |
// e2 | | e4 --> x
// | |
// v3 ------ v4
// e3
enum Axis
{
FACE_A_X,
FACE_A_Y,
FACE_B_X,
FACE_B_Y
};
enum EdgeNumbers
{
NO_EDGE = 0,
EDGE1,
EDGE2,
EDGE3,
EDGE4
};
struct ClipVertex
{
ClipVertex() { fp.value = 0; }
Vec2 v;
FeaturePair fp;
};
void Flip(FeaturePair& fp)
{
Swap(fp.e.inEdge1, fp.e.inEdge2);
Swap(fp.e.outEdge1, fp.e.outEdge2);
}
int ClipSegmentToLine(ClipVertex vOut[2], ClipVertex vIn[2],
const Vec2& normal, number offset, char clipEdge)
{
// Start with no output points
int numOut = 0;
// Calculate the distance of end points to the line
number distance0 = Dot(normal, vIn[0].v) - offset;
number distance1 = Dot(normal, vIn[1].v) - offset;
// If the points are behind the plane
if (distance0 <= 0.0f) vOut[numOut++] = vIn[0];
if (distance1 <= 0.0f) vOut[numOut++] = vIn[1];
// If the points are on different sides of the plane
if (distance0 * distance1 < 0.0f)
{
// Find intersection point of edge and plane
number interp = distance0 / (distance0 - distance1);
vOut[numOut].v = vIn[0].v + interp * (vIn[1].v - vIn[0].v);
if (distance0 > 0.0f)
{
vOut[numOut].fp = vIn[0].fp;
vOut[numOut].fp.e.inEdge1 = clipEdge;
vOut[numOut].fp.e.inEdge2 = NO_EDGE;
}
else
{
vOut[numOut].fp = vIn[1].fp;
vOut[numOut].fp.e.outEdge1 = clipEdge;
vOut[numOut].fp.e.outEdge2 = NO_EDGE;
}
++numOut;
}
return numOut;
}
static void ComputeIncidentEdge(ClipVertex c[2], const Vec2& h, const Vec2& pos,
const Mat22& Rot, const Vec2& normal)
{
// The normal is from the reference box. Convert it
// to the incident boxe's frame and flip sign.
Mat22 RotT = Rot.Transpose();
Vec2 n = -(RotT * normal);
Vec2 nAbs = Abs(n);
if (nAbs.x > nAbs.y)
{
if (Sign(n.x) > 0.0f)
{
c[0].v.Set(h.x, -h.y);
c[0].fp.e.inEdge2 = EDGE3;
c[0].fp.e.outEdge2 = EDGE4;
c[1].v.Set(h.x, h.y);
c[1].fp.e.inEdge2 = EDGE4;
c[1].fp.e.outEdge2 = EDGE1;
}
else
{
c[0].v.Set(-h.x, h.y);
c[0].fp.e.inEdge2 = EDGE1;
c[0].fp.e.outEdge2 = EDGE2;
c[1].v.Set(-h.x, -h.y);
c[1].fp.e.inEdge2 = EDGE2;
c[1].fp.e.outEdge2 = EDGE3;
}
}
else
{
if (Sign(n.y) > 0.0f)
{
c[0].v.Set(h.x, h.y);
c[0].fp.e.inEdge2 = EDGE4;
c[0].fp.e.outEdge2 = EDGE1;
c[1].v.Set(-h.x, h.y);
c[1].fp.e.inEdge2 = EDGE1;
c[1].fp.e.outEdge2 = EDGE2;
}
else
{
c[0].v.Set(-h.x, -h.y);
c[0].fp.e.inEdge2 = EDGE2;
c[0].fp.e.outEdge2 = EDGE3;
c[1].v.Set(h.x, -h.y);
c[1].fp.e.inEdge2 = EDGE3;
c[1].fp.e.outEdge2 = EDGE4;
}
}
c[0].v = pos + Rot * c[0].v;
c[1].v = pos + Rot * c[1].v;
}
namespace Phy2D
{
// The normal points from A to B
int Collide(Contact* contacts, Body* bodyA, Body* bodyB)
{
// Setup
Vec2 hA = 0.5f * bodyA->width;
Vec2 hB = 0.5f * bodyB->width;
Vec2 posA = bodyA->position;
Vec2 posB = bodyB->position;
Mat22 RotA(bodyA->rotation), RotB(bodyB->rotation);
Mat22 RotAT = RotA.Transpose();
Mat22 RotBT = RotB.Transpose();
Vec2 dp = posB - posA;
Vec2 dA = RotAT * dp;
Vec2 dB = RotBT * dp;
Mat22 C = RotAT * RotB;
Mat22 absC = Abs(C);
Mat22 absCT = absC.Transpose();
// Box A faces
Vec2 faceA = Abs(dA) - hA - absC * hB;
if (faceA.x > 0.0f || faceA.y > 0.0f)
return 0;
// Box B faces
Vec2 faceB = Abs(dB) - absCT * hA - hB;
if (faceB.x > 0.0f || faceB.y > 0.0f)
return 0;
// Find best axis
Axis axis;
number separation;
Vec2 normal;
// Box A faces
axis = FACE_A_X;
separation = faceA.x;
normal = dA.x > 0.0f ? RotA.col1 : -RotA.col1;
const number relativeTol = 0.95f;
const number absoluteTol = 0.01f;
if (faceA.y > relativeTol * separation + absoluteTol * hA.y)
{
axis = FACE_A_Y;
separation = faceA.y;
normal = dA.y > 0.0f ? RotA.col2 : -RotA.col2;
}
// Box B faces
if (faceB.x > relativeTol * separation + absoluteTol * hB.x)
{
axis = FACE_B_X;
separation = faceB.x;
normal = dB.x > 0.0f ? RotB.col1 : -RotB.col1;
}
if (faceB.y > relativeTol * separation + absoluteTol * hB.y)
{
axis = FACE_B_Y;
separation = faceB.y;
normal = dB.y > 0.0f ? RotB.col2 : -RotB.col2;
}
// Setup clipping plane data based on the separating axis
Vec2 frontNormal, sideNormal;
ClipVertex incidentEdge[2];
number front, negSide, posSide;
char negEdge, posEdge;
// Compute the clipping lines and the line segment to be clipped.
switch (axis)
{
case FACE_A_X:
{
frontNormal = normal;
front = Dot(posA, frontNormal) + hA.x;
sideNormal = RotA.col2;
number side = Dot(posA, sideNormal);
negSide = -side + hA.y;
posSide = side + hA.y;
negEdge = EDGE3;
posEdge = EDGE1;
ComputeIncidentEdge(incidentEdge, hB, posB, RotB, frontNormal);
}
break;
case FACE_A_Y:
{
frontNormal = normal;
front = Dot(posA, frontNormal) + hA.y;
sideNormal = RotA.col1;
number side = Dot(posA, sideNormal);
negSide = -side + hA.x;
posSide = side + hA.x;
negEdge = EDGE2;
posEdge = EDGE4;
ComputeIncidentEdge(incidentEdge, hB, posB, RotB, frontNormal);
}
break;
case FACE_B_X:
{
frontNormal = -normal;
front = Dot(posB, frontNormal) + hB.x;
sideNormal = RotB.col2;
number side = Dot(posB, sideNormal);
negSide = -side + hB.y;
posSide = side + hB.y;
negEdge = EDGE3;
posEdge = EDGE1;
ComputeIncidentEdge(incidentEdge, hA, posA, RotA, frontNormal);
}
break;
case FACE_B_Y:
{
frontNormal = -normal;
front = Dot(posB, frontNormal) + hB.y;
sideNormal = RotB.col1;
number side = Dot(posB, sideNormal);
negSide = -side + hB.x;
posSide = side + hB.x;
negEdge = EDGE2;
posEdge = EDGE4;
ComputeIncidentEdge(incidentEdge, hA, posA, RotA, frontNormal);
}
break;
}
// clip other face with 5 box planes (1 face plane, 4 edge planes)
ClipVertex clipPoints1[2];
ClipVertex clipPoints2[2];
int np;
// Clip to box side 1
np = ClipSegmentToLine(clipPoints1, incidentEdge, -sideNormal, negSide, negEdge);
if (np < 2)
return 0;
// Clip to negative box side 1
np = ClipSegmentToLine(clipPoints2, clipPoints1, sideNormal, posSide, posEdge);
if (np < 2)
return 0;
// Now clipPoints2 contains the clipping points.
// Due to roundoff, it is possible that clipping removes all points.
int numContacts = 0;
for (int i = 0; i < 2; ++i)
{
number separation = Dot(frontNormal, clipPoints2[i].v) - front;
if (separation <= 0)
{
contacts[numContacts].separation = separation;
contacts[numContacts].normal = normal;
// slide contact point onto reference face (easy to cull)
contacts[numContacts].position = clipPoints2[i].v - separation * frontNormal;
contacts[numContacts].feature = clipPoints2[i].fp;
if (axis == FACE_B_X || axis == FACE_B_Y)
Flip(contacts[numContacts].feature);
++numContacts;
}
}
return numContacts;
}
}
|