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
|
/*
Box2DX Copyright (c) 2009 Ihar Kalasouski http://code.google.com/p/box2dx
Box2D original C++ version Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
using System;
using Box2DX.Common;
namespace Box2DX.Collision
{
// Structures and functions used for computing contact points, distance
// queries, and TOI queries.
public partial class Collision
{
public static readonly byte NullFeature = Common.Math.UCHAR_MAX;
public static bool TestOverlap(AABB a, AABB b)
{
Vec2 d1, d2;
d1 = b.LowerBound - a.UpperBound;
d2 = a.LowerBound - b.UpperBound;
if (d1.X > 0.0f || d1.Y > 0.0f)
return false;
if (d2.X > 0.0f || d2.Y > 0.0f)
return false;
return true;
}
/// <summary>
/// Compute the point states given two manifolds. The states pertain to the transition from manifold1
/// to manifold2. So state1 is either persist or remove while state2 is either add or persist.
/// </summary>
public static void GetPointStates(PointState[/*b2_maxManifoldPoints*/] state1, PointState[/*b2_maxManifoldPoints*/] state2,
Manifold manifold1, Manifold manifold2)
{
for (int i = 0; i < Common.Settings.MaxManifoldPoints; ++i)
{
state1[i] = PointState.NullState;
state2[i] = PointState.NullState;
}
// Detect persists and removes.
for (int i = 0; i < manifold1.PointCount; ++i)
{
ContactID id = manifold1.Points[i].ID;
state1[i] = PointState.RemoveState;
for (int j = 0; j < manifold2.PointCount; ++j)
{
if (manifold2.Points[j].ID.Key == id.Key)
{
state1[i] = PointState.PersistState;
break;
}
}
}
// Detect persists and adds.
for (int i = 0; i < manifold2.PointCount; ++i)
{
ContactID id = manifold2.Points[i].ID;
state2[i] = PointState.AddState;
for (int j = 0; j < manifold1.PointCount; ++j)
{
if (manifold1.Points[j].ID.Key == id.Key)
{
state2[i] = PointState.PersistState;
break;
}
}
}
}
// Sutherland-Hodgman clipping.
public static int ClipSegmentToLine(out ClipVertex[/*2*/] vOut, ClipVertex[/*2*/] vIn, Vec2 normal, float offset)
{
vOut = new ClipVertex[2];
// Start with no output points
int numOut = 0;
// Calculate the distance of end points to the line
float distance0 = Vec2.Dot(normal, vIn[0].V) - offset;
float distance1 = Vec2.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
float interp = distance0 / (distance0 - distance1);
vOut[numOut].V = vIn[0].V + interp * (vIn[1].V - vIn[0].V);
if (distance0 > 0.0f)
{
vOut[numOut].ID = vIn[0].ID;
}
else
{
vOut[numOut].ID = vIn[1].ID;
}
++numOut;
}
return numOut;
}
}
/// <summary>
/// The features that intersect to form the contact point.
/// </summary>
public struct Features
{
/// <summary>
/// The edge that defines the outward contact normal.
/// </summary>
public Byte ReferenceEdge;
/// <summary>
/// The edge most anti-parallel to the reference edge.
/// </summary>
public Byte IncidentEdge;
/// <summary>
/// The vertex (0 or 1) on the incident edge that was clipped.
/// </summary>
public Byte IncidentVertex;
/// <summary>
/// A value of 1 indicates that the reference edge is on shape2.
/// </summary>
public Byte Flip;
}
/// <summary>
/// Contact ids to facilitate warm starting.
/// </summary>
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)]
public struct ContactID
{
[System.Runtime.InteropServices.FieldOffset(0)]
public Features Features;
/// <summary>
/// Used to quickly compare contact ids.
/// </summary>
[System.Runtime.InteropServices.FieldOffset(0)]
public UInt32 Key;
}
/// <summary>
/// A manifold point is a contact point belonging to a contact
/// manifold. It holds details related to the geometry and dynamics
/// of the contact points.
/// The local point usage depends on the manifold type:
/// -Circles: the local center of circleB
/// -FaceA: the local center of cirlceB or the clip point of polygonB
/// -FaceB: the clip point of polygonA
/// This structure is stored across time steps, so we keep it small.
/// Note: the impulses are used for internal caching and may not
/// provide reliable contact forces, especially for high speed collisions.
/// </summary>
public class ManifoldPoint
{
/// <summary>
/// Usage depends on manifold type.
/// </summary>
public Vec2 LocalPoint;
/// <summary>
/// The non-penetration impulse.
/// </summary>
public float NormalImpulse;
/// <summary>
/// The friction impulse.
/// </summary>
public float TangentImpulse;
/// <summary>
/// Uniquely identifies a contact point between two shapes.
/// </summary>
public ContactID ID;
public ManifoldPoint Clone()
{
ManifoldPoint newPoint = new ManifoldPoint();
newPoint.LocalPoint = this.LocalPoint;
newPoint.NormalImpulse = this.NormalImpulse;
newPoint.TangentImpulse = this.TangentImpulse;
newPoint.ID = this.ID;
return newPoint;
}
}
public enum ManifoldType
{
Circles,
FaceA,
FaceB
}
/// <summary>
/// A manifold for two touching convex shapes.
/// </summary>
public class Manifold
{
/// <summary>
/// The points of contact.
/// </summary>
public ManifoldPoint[/*Settings.MaxManifoldPoints*/] Points = new ManifoldPoint[Settings.MaxManifoldPoints];
public Vec2 LocalPlaneNormal;
/// <summary>
/// Usage depends on manifold type.
/// </summary>
public Vec2 LocalPoint;
public ManifoldType Type;
/// <summary>
/// The number of manifold points.
/// </summary>
public int PointCount;
public Manifold()
{
for (int i = 0; i < Settings.MaxManifoldPoints; i++)
Points[i] = new ManifoldPoint();
}
public Manifold Clone()
{
Manifold newManifold = new Manifold();
newManifold.LocalPlaneNormal = this.LocalPlaneNormal;
newManifold.LocalPoint = this.LocalPoint;
newManifold.Type = this.Type;
newManifold.PointCount = this.PointCount;
int pointCount = this.Points.Length;
ManifoldPoint[] tmp = new ManifoldPoint[pointCount];
for (int i = 0; i < pointCount; i++)
{
tmp[i] = this.Points[i].Clone();
}
newManifold.Points = tmp;
return newManifold;
}
}
/// <summary>
/// A line segment.
/// </summary>
public struct Segment
{
// Collision Detection in Interactive 3D Environments by Gino van den Bergen
// From Section 3.4.1
// x = mu1 * p1 + mu2 * p2
// mu1 + mu2 = 1 && mu1 >= 0 && mu2 >= 0
// mu1 = 1 - mu2;
// x = (1 - mu2) * p1 + mu2 * p2
// = p1 + mu2 * (p2 - p1)
// x = s + a * r (s := start, r := end - start)
// s + a * r = p1 + mu2 * d (d := p2 - p1)
// -a * r + mu2 * d = b (b := s - p1)
// [-r d] * [a; mu2] = b
// Cramer's rule:
// denom = det[-r d]
// a = det[b d] / denom
// mu2 = det[-r b] / denom
/// <summary>
/// Ray cast against this segment with another segment.
/// </summary>
public bool TestSegment(out float lambda, out Vec2 normal, Segment segment, float maxLambda)
{
lambda = 0f;
normal = new Vec2();
Vec2 s = segment.P1;
Vec2 r = segment.P2 - s;
Vec2 d = P2 - P1;
Vec2 n = Vec2.Cross(d, 1.0f);
float k_slop = 100.0f * Common.Settings.FLT_EPSILON;
float denom = -Vec2.Dot(r, n);
// Cull back facing collision and ignore parallel segments.
if (denom > k_slop)
{
// Does the segment intersect the infinite line associated with this segment?
Vec2 b = s - P1;
float a = Vec2.Dot(b, n);
if (0.0f <= a && a <= maxLambda * denom)
{
float mu2 = -r.X * b.Y + r.Y * b.X;
// Does the segment intersect this segment?
if (-k_slop * denom <= mu2 && mu2 <= denom * (1.0f + k_slop))
{
a /= denom;
n.Normalize();
lambda = a;
normal = n;
return true;
}
}
}
return false;
}
/// <summary>
/// The starting point.
/// </summary>
public Vec2 P1;
/// <summary>
/// The ending point.
/// </summary>
public Vec2 P2;
}
/// <summary>
/// An axis aligned bounding box.
/// </summary>
public struct AABB
{
/// <summary>
/// The lower vertex.
/// </summary>
public Vec2 LowerBound;
/// <summary>
/// The upper vertex.
/// </summary>
public Vec2 UpperBound;
/// Verify that the bounds are sorted.
public bool IsValid
{
get
{
Vec2 d = UpperBound - LowerBound;
bool valid = d.X >= 0.0f && d.Y >= 0.0f;
valid = valid && LowerBound.IsValid && UpperBound.IsValid;
return valid;
}
}
/// Get the center of the AABB.
public Vec2 Center
{
get { return 0.5f * (LowerBound + UpperBound); }
}
/// Get the extents of the AABB (half-widths).
public Vec2 Extents
{
get { return 0.5f * (UpperBound - LowerBound); }
}
/// Combine two AABBs into this one.
public void Combine(AABB aabb1, AABB aabb2)
{
LowerBound = Common.Math.Min(aabb1.LowerBound, aabb2.LowerBound);
UpperBound = Common.Math.Max(aabb1.UpperBound, aabb2.UpperBound);
}
/// Does this aabb contain the provided AABB.
public bool Contains(AABB aabb)
{
bool result = LowerBound.X <= aabb.LowerBound.X;
result = result && LowerBound.Y <= aabb.LowerBound.Y;
result = result && aabb.UpperBound.X <= UpperBound.X;
result = result && aabb.UpperBound.Y <= UpperBound.Y;
return result;
}
/// <summary>
// From Real-time Collision Detection, p179.
/// </summary>
public void RayCast(out RayCastOutput output, RayCastInput input)
{
float tmin = -Common.Settings.FLT_MAX;
float tmax = Common.Settings.FLT_MAX;
output = new RayCastOutput();
output.Hit = false;
Vec2 p = input.P1;
Vec2 d = input.P2 - input.P1;
Vec2 absD = Common.Math.Abs(d);
Vec2 normal = new Vec2(0);
for (int i = 0; i < 2; ++i)
{
if (absD[i] < Common.Settings.FLT_EPSILON)
{
// Parallel.
if (p[i] < LowerBound[i] || UpperBound[i] < p[i])
{
return;
}
}
else
{
float inv_d = 1.0f / d[i];
float t1 = (LowerBound[i] - p[i]) * inv_d;
float t2 = (UpperBound[i] - p[i]) * inv_d;
// Sign of the normal vector.
float s = -1.0f;
if (t1 > t2)
{
Common.Math.Swap(ref t1, ref t2);
s = 1.0f;
}
// Push the min up
if (t1 > tmin)
{
normal.SetZero();
normal[i] = s;
tmin = t1;
}
// Pull the max down
tmax = Common.Math.Min(tmax, t2);
if (tmin > tmax)
{
return;
}
}
}
// Does the ray start inside the box?
// Does the ray intersect beyond the max fraction?
if (tmin < 0.0f || input.MaxFraction < tmin)
{
return;
}
// Intersection.
output.Fraction = tmin;
output.Normal = normal;
output.Hit = true;
}
}
/// <summary>
/// This is used for determining the state of contact points.
/// </summary>
public enum PointState
{
/// <summary>
/// Point does not exist.
/// </summary>
NullState,
/// <summary>
/// Point was added in the update.
/// </summary>
AddState,
/// <summary>
/// Point persisted across the update.
/// </summary>
PersistState,
/// <summary>
///Point was removed in the update.
/// </summary>
RemoveState
}
/// <summary>
/// This is used to compute the current state of a contact manifold.
/// </summary>
public class WorldManifold
{
/// <summary>
/// World vector pointing from A to B.
/// </summary>
public Vec2 Normal;
/// <summary>
/// World contact point (point of intersection).
/// </summary>
public Vec2[] Points = new Vec2[Common.Settings.MaxManifoldPoints];
public WorldManifold Clone()
{
WorldManifold newManifold = new WorldManifold();
newManifold.Normal = this.Normal;
this.Points.CopyTo(newManifold.Points, 0);
return newManifold;
}
/// Evaluate the manifold with supplied transforms. This assumes
/// modest motion from the original state. This does not change the
/// point count, impulses, etc. The radii must come from the shapes
/// that generated the manifold.
public void Initialize(Manifold manifold, XForm xfA, float radiusA, XForm xfB, float radiusB)
{
if (manifold.PointCount == 0)
{
return;
}
switch (manifold.Type)
{
case ManifoldType.Circles:
{
Vec2 pointA = Common.Math.Mul(xfA, manifold.LocalPoint);
Vec2 pointB = Common.Math.Mul(xfB, manifold.Points[0].LocalPoint);
Vec2 normal = new Vec2(1.0f, 0.0f);
if (Vec2.DistanceSquared(pointA, pointB) > Common.Settings.FLT_EPSILON_SQUARED)
{
normal = pointB - pointA;
normal.Normalize();
}
Normal = normal;
Vec2 cA = pointA + radiusA * normal;
Vec2 cB = pointB - radiusB * normal;
Points[0] = 0.5f * (cA + cB);
}
break;
case ManifoldType.FaceA:
{
Vec2 normal = Common.Math.Mul(xfA.R, manifold.LocalPlaneNormal);
Vec2 planePoint = Common.Math.Mul(xfA, manifold.LocalPoint);
// Ensure normal points from A to B.
Normal = normal;
for (int i = 0; i < manifold.PointCount; ++i)
{
Vec2 clipPoint = Common.Math.Mul(xfB, manifold.Points[i].LocalPoint);
Vec2 cA = clipPoint + (radiusA - Vec2.Dot(clipPoint - planePoint, normal)) * normal;
Vec2 cB = clipPoint - radiusB * normal;
Points[i] = 0.5f * (cA + cB);
}
}
break;
case ManifoldType.FaceB:
{
Vec2 normal = Common.Math.Mul(xfB.R, manifold.LocalPlaneNormal);
Vec2 planePoint = Common.Math.Mul(xfB, manifold.LocalPoint);
// Ensure normal points from A to B.
Normal = -normal;
for (int i = 0; i < manifold.PointCount; ++i)
{
Vec2 clipPoint = Common.Math.Mul(xfA, manifold.Points[i].LocalPoint);
Vec2 cA = clipPoint - radiusA * normal;
Vec2 cB = clipPoint + (radiusB - Vec2.Dot(clipPoint - planePoint, normal)) * normal;
Points[i] = 0.5f * (cA + cB);
}
}
break;
}
}
}
/// <summary>
/// Used for computing contact manifolds.
/// </summary>
public struct ClipVertex
{
public Vec2 V;
public ContactID ID;
}
/// <summary>
/// Ray-cast input data.
/// </summary>
public struct RayCastInput
{
public Vec2 P1, P2;
public float MaxFraction;
}
/// <summary>
/// Ray-cast output data.
/// </summary>
public struct RayCastOutput
{
public Vec2 Normal;
public float Fraction;
public bool Hit;
}
}
|