forked from ByteArena/box2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollisionB2CollideEdge.go
607 lines (523 loc) · 16.2 KB
/
CollisionB2CollideEdge.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
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
package box2d
import (
"math"
)
// Compute contact points for edge versus circle.
// This accounts for edge connectivity.
func CollideEdgeAndCircle(manifold *Manifold, edgeA *EdgeShape, xfA Transform, circleB *CircleShape, xfB Transform) {
manifold.PointCount = 0
// Compute circle in frame of edge
Q := TransformVec2MulT(xfA, TransformVec2Mul(xfB, circleB.M_p))
A := edgeA.M_vertex1
B := edgeA.M_vertex2
e := Vec2Sub(B, A)
// Barycentric coordinates
u := Vec2Dot(e, Vec2Sub(B, Q))
v := Vec2Dot(e, Vec2Sub(Q, A))
radius := edgeA.M_radius + circleB.M_radius
cf := MakeContactFeature()
cf.IndexB = 0
cf.TypeB = ContactFeatureType.Vertex
// Region A
if v <= 0.0 {
P := A
d := Vec2Sub(Q, P)
dd := Vec2Dot(d, d)
if dd > radius*radius {
return
}
// Is there an edge connected to A?
if edgeA.M_hasVertex0 {
A1 := edgeA.M_vertex0
B1 := A
e1 := Vec2Sub(B1, A1)
u1 := Vec2Dot(e1, Vec2Sub(B1, Q))
// Is the circle in Region AB of the previous edge?
if u1 > 0.0 {
return
}
}
cf.IndexA = 0
cf.TypeA = ContactFeatureType.Vertex
manifold.PointCount = 1
manifold.Type = ManifoldType.Circles
manifold.LocalNormal.SetZero()
manifold.LocalPoint = P
manifold.Points[0].Id.SetKey(0)
manifold.Points[0].Id.IndexA = cf.IndexA
manifold.Points[0].Id.IndexB = cf.IndexB
manifold.Points[0].Id.TypeA = cf.TypeA
manifold.Points[0].Id.TypeB = cf.TypeB
manifold.Points[0].LocalPoint = circleB.M_p
return
}
// Region B
if u <= 0.0 {
P := B
d := Vec2Sub(Q, P)
dd := Vec2Dot(d, d)
if dd > radius*radius {
return
}
// Is there an edge connected to B?
if edgeA.M_hasVertex3 {
B2 := edgeA.M_vertex3
A2 := B
e2 := Vec2Sub(B2, A2)
v2 := Vec2Dot(e2, Vec2Sub(Q, A2))
// Is the circle in Region AB of the next edge?
if v2 > 0.0 {
return
}
}
cf.IndexA = 1
cf.TypeA = ContactFeatureType.Vertex
manifold.PointCount = 1
manifold.Type = ManifoldType.Circles
manifold.LocalNormal.SetZero()
manifold.LocalPoint = P
manifold.Points[0].Id.SetKey(0)
manifold.Points[0].Id.IndexA = cf.IndexA
manifold.Points[0].Id.IndexB = cf.IndexB
manifold.Points[0].Id.TypeA = cf.TypeA
manifold.Points[0].Id.TypeB = cf.TypeB
manifold.Points[0].LocalPoint = circleB.M_p
return
}
// Region AB
den := Vec2Dot(e, e)
assert(den > 0.0)
P := Vec2MulScalar(1.0/den, Vec2Add(Vec2MulScalar(u, A), Vec2MulScalar(v, B)))
d := Vec2Sub(Q, P)
dd := Vec2Dot(d, d)
if dd > radius*radius {
return
}
n := MakeVec2(-e.Y, e.X)
if Vec2Dot(n, Vec2Sub(Q, A)) < 0.0 {
n.Set(-n.X, -n.Y)
}
n.Normalize()
cf.IndexA = 0
cf.TypeA = ContactFeatureType.Face
manifold.PointCount = 1
manifold.Type = ManifoldType.FaceA
manifold.LocalNormal = n
manifold.LocalPoint = A
manifold.Points[0].Id.SetKey(0)
manifold.Points[0].Id.IndexA = cf.IndexA
manifold.Points[0].Id.IndexB = cf.IndexB
manifold.Points[0].Id.TypeA = cf.TypeA
manifold.Points[0].Id.TypeB = cf.TypeB
manifold.Points[0].LocalPoint = circleB.M_p
}
// This structure is used to keep track of the best separating axis.
var EPAxisType = struct {
Unknown uint8
EdgeA uint8
EdgeB uint8
}{
Unknown: 0,
EdgeA: 1,
EdgeB: 2,
}
type EPAxis struct {
Type uint8
Index int
Separation float64
}
func MakeEPAxis() EPAxis {
return EPAxis{}
}
// This holds polygon B expressed in frame A.
type TempPolygon struct {
Vertices [maxPolygonVertices]Vec2
Normals [maxPolygonVertices]Vec2
Count int
}
// Reference face used for clipping
type ReferenceFace struct {
I1, I2 int
V1, V2 Vec2
Normal Vec2
SideNormal1 Vec2
SideOffset1 float64
SideNormal2 Vec2
SideOffset2 float64
}
func MakeReferenceFace() ReferenceFace {
return ReferenceFace{}
}
var EPCollider_VertexType = struct {
E_isolated uint8
E_concave uint8
E_convex uint8
}{
E_isolated: 0,
E_concave: 1,
E_convex: 2,
}
// This class collides and edge and a polygon, taking into account edge adjacency.
type EPCollider struct {
M_polygonB TempPolygon
M_xf Transform
M_centroidB Vec2
M_v0, M_v1, M_v2, M_v3 Vec2
M_normal0, M_normal1, M_normal2 Vec2
M_normal Vec2
M_type1, M_type2 uint8
M_lowerLimit, M_upperLimit Vec2
M_radius float64
M_front bool
}
func MakeEPCollider() EPCollider {
return EPCollider{}
}
// Algorithm:
// 1. Classify v1 and v2
// 2. Classify polygon centroid as front or back
// 3. Flip normal if necessary
// 4. Initialize normal range to [-pi, pi] about face normal
// 5. Adjust normal range according to adjacent edges
// 6. Visit each separating axes, only accept axes within the range
// 7. Return if _any_ axis indicates separation
// 8. Clip
func (collider *EPCollider) Collide(manifold *Manifold, edgeA *EdgeShape, xfA Transform, polygonB *PolygonShape, xfB Transform) {
collider.M_xf = TransformMulT(xfA, xfB)
collider.M_centroidB = TransformVec2Mul(collider.M_xf, polygonB.M_centroid)
collider.M_v0 = edgeA.M_vertex0
collider.M_v1 = edgeA.M_vertex1
collider.M_v2 = edgeA.M_vertex2
collider.M_v3 = edgeA.M_vertex3
hasVertex0 := edgeA.M_hasVertex0
hasVertex3 := edgeA.M_hasVertex3
edge1 := Vec2Sub(collider.M_v2, collider.M_v1)
edge1.Normalize()
collider.M_normal1.Set(edge1.Y, -edge1.X)
offset1 := Vec2Dot(collider.M_normal1, Vec2Sub(collider.M_centroidB, collider.M_v1))
offset0 := 0.0
offset2 := 0.0
convex1 := false
convex2 := false
// Is there a preceding edge?
if hasVertex0 {
edge0 := Vec2Sub(collider.M_v1, collider.M_v0)
edge0.Normalize()
collider.M_normal0.Set(edge0.Y, -edge0.X)
convex1 = Vec2Cross(edge0, edge1) >= 0.0
offset0 = Vec2Dot(collider.M_normal0, Vec2Sub(collider.M_centroidB, collider.M_v0))
}
// Is there a following edge?
if hasVertex3 {
edge2 := Vec2Sub(collider.M_v3, collider.M_v2)
edge2.Normalize()
collider.M_normal2.Set(edge2.Y, -edge2.X)
convex2 = Vec2Cross(edge1, edge2) > 0.0
offset2 = Vec2Dot(collider.M_normal2, Vec2Sub(collider.M_centroidB, collider.M_v2))
}
// Determine front or back collision. Determine collision normal limits.
if hasVertex0 && hasVertex3 {
if convex1 && convex2 {
collider.M_front = offset0 >= 0.0 || offset1 >= 0.0 || offset2 >= 0.0
if collider.M_front {
collider.M_normal = collider.M_normal1
collider.M_lowerLimit = collider.M_normal0
collider.M_upperLimit = collider.M_normal2
} else {
collider.M_normal = collider.M_normal1.OperatorNegate()
collider.M_lowerLimit = collider.M_normal1.OperatorNegate()
collider.M_upperLimit = collider.M_normal1.OperatorNegate()
}
} else if convex1 {
collider.M_front = offset0 >= 0.0 || (offset1 >= 0.0 && offset2 >= 0.0)
if collider.M_front {
collider.M_normal = collider.M_normal1
collider.M_lowerLimit = collider.M_normal0
collider.M_upperLimit = collider.M_normal1
} else {
collider.M_normal = collider.M_normal1.OperatorNegate()
collider.M_lowerLimit = collider.M_normal2.OperatorNegate()
collider.M_upperLimit = collider.M_normal1.OperatorNegate()
}
} else if convex2 {
collider.M_front = offset2 >= 0.0 || (offset0 >= 0.0 && offset1 >= 0.0)
if collider.M_front {
collider.M_normal = collider.M_normal1
collider.M_lowerLimit = collider.M_normal1
collider.M_upperLimit = collider.M_normal2
} else {
collider.M_normal = collider.M_normal1.OperatorNegate()
collider.M_lowerLimit = collider.M_normal1.OperatorNegate()
collider.M_upperLimit = collider.M_normal0.OperatorNegate()
}
} else {
collider.M_front = offset0 >= 0.0 && offset1 >= 0.0 && offset2 >= 0.0
if collider.M_front {
collider.M_normal = collider.M_normal1
collider.M_lowerLimit = collider.M_normal1
collider.M_upperLimit = collider.M_normal1
} else {
collider.M_normal = collider.M_normal1.OperatorNegate()
collider.M_lowerLimit = collider.M_normal2.OperatorNegate()
collider.M_upperLimit = collider.M_normal0.OperatorNegate()
}
}
} else if hasVertex0 {
if convex1 {
collider.M_front = offset0 >= 0.0 || offset1 >= 0.0
if collider.M_front {
collider.M_normal = collider.M_normal1
collider.M_lowerLimit = collider.M_normal0
collider.M_upperLimit = collider.M_normal1.OperatorNegate()
} else {
collider.M_normal = collider.M_normal1.OperatorNegate()
collider.M_lowerLimit = collider.M_normal1
collider.M_upperLimit = collider.M_normal1.OperatorNegate()
}
} else {
collider.M_front = offset0 >= 0.0 && offset1 >= 0.0
if collider.M_front {
collider.M_normal = collider.M_normal1
collider.M_lowerLimit = collider.M_normal1
collider.M_upperLimit = collider.M_normal1.OperatorNegate()
} else {
collider.M_normal = collider.M_normal1.OperatorNegate()
collider.M_lowerLimit = collider.M_normal1
collider.M_upperLimit = collider.M_normal0.OperatorNegate()
}
}
} else if hasVertex3 {
if convex2 {
collider.M_front = offset1 >= 0.0 || offset2 >= 0.0
if collider.M_front {
collider.M_normal = collider.M_normal1
collider.M_lowerLimit = collider.M_normal1.OperatorNegate()
collider.M_upperLimit = collider.M_normal2
} else {
collider.M_normal = collider.M_normal1.OperatorNegate()
collider.M_lowerLimit = collider.M_normal1.OperatorNegate()
collider.M_upperLimit = collider.M_normal1
}
} else {
collider.M_front = offset1 >= 0.0 && offset2 >= 0.0
if collider.M_front {
collider.M_normal = collider.M_normal1
collider.M_lowerLimit = collider.M_normal1.OperatorNegate()
collider.M_upperLimit = collider.M_normal1
} else {
collider.M_normal = collider.M_normal1.OperatorNegate()
collider.M_lowerLimit = collider.M_normal2.OperatorNegate()
collider.M_upperLimit = collider.M_normal1
}
}
} else {
collider.M_front = offset1 >= 0.0
if collider.M_front {
collider.M_normal = collider.M_normal1
collider.M_lowerLimit = collider.M_normal1.OperatorNegate()
collider.M_upperLimit = collider.M_normal1.OperatorNegate()
} else {
collider.M_normal = collider.M_normal1.OperatorNegate()
collider.M_lowerLimit = collider.M_normal1
collider.M_upperLimit = collider.M_normal1
}
}
// Get polygonB in frameA
collider.M_polygonB.Count = polygonB.M_count
for i := 0; i < polygonB.M_count; i++ {
collider.M_polygonB.Vertices[i] = TransformVec2Mul(collider.M_xf, polygonB.M_vertices[i])
collider.M_polygonB.Normals[i] = RotVec2Mul(collider.M_xf.Q, polygonB.M_normals[i])
}
collider.M_radius = polygonB.M_radius + edgeA.M_radius
manifold.PointCount = 0
edgeAxis := collider.ComputeEdgeSeparation()
// If no valid normal can be found than this edge should not collide.
if edgeAxis.Type == EPAxisType.Unknown {
return
}
if edgeAxis.Separation > collider.M_radius {
return
}
polygonAxis := collider.ComputePolygonSeparation()
if polygonAxis.Type != EPAxisType.Unknown && polygonAxis.Separation > collider.M_radius {
return
}
// Use hysteresis for jitter reduction.
k_relativeTol := 0.98
k_absoluteTol := 0.001
primaryAxis := MakeEPAxis()
if polygonAxis.Type == EPAxisType.Unknown {
primaryAxis = edgeAxis
} else if polygonAxis.Separation > k_relativeTol*edgeAxis.Separation+k_absoluteTol {
primaryAxis = polygonAxis
} else {
primaryAxis = edgeAxis
}
ie := make([]ClipVertex, 2)
rf := MakeReferenceFace()
if primaryAxis.Type == EPAxisType.EdgeA {
manifold.Type = ManifoldType.FaceA
// Search for the polygon normal that is most anti-parallel to the edge normal.
bestIndex := 0
bestValue := Vec2Dot(collider.M_normal, collider.M_polygonB.Normals[0])
for i := 1; i < collider.M_polygonB.Count; i++ {
value := Vec2Dot(collider.M_normal, collider.M_polygonB.Normals[i])
if value < bestValue {
bestValue = value
bestIndex = i
}
}
i1 := bestIndex
i2 := 0
if i1+1 < collider.M_polygonB.Count {
i2 = i1 + 1
}
ie[0].V = collider.M_polygonB.Vertices[i1]
ie[0].Id.IndexA = 0
ie[0].Id.IndexB = uint8(i1)
ie[0].Id.TypeA = ContactFeatureType.Face
ie[0].Id.TypeB = ContactFeatureType.Vertex
ie[1].V = collider.M_polygonB.Vertices[i2]
ie[1].Id.IndexA = 0
ie[1].Id.IndexB = uint8(i2)
ie[1].Id.TypeA = ContactFeatureType.Face
ie[1].Id.TypeB = ContactFeatureType.Vertex
if collider.M_front {
rf.I1 = 0
rf.I2 = 1
rf.V1 = collider.M_v1
rf.V2 = collider.M_v2
rf.Normal = collider.M_normal1
} else {
rf.I1 = 1
rf.I2 = 0
rf.V1 = collider.M_v2
rf.V2 = collider.M_v1
rf.Normal = collider.M_normal1.OperatorNegate()
}
} else {
manifold.Type = ManifoldType.FaceB
ie[0].V = collider.M_v1
ie[0].Id.IndexA = 0
ie[0].Id.IndexB = uint8(primaryAxis.Index)
ie[0].Id.TypeA = ContactFeatureType.Vertex
ie[0].Id.TypeB = ContactFeatureType.Face
ie[1].V = collider.M_v2
ie[1].Id.IndexA = 0
ie[1].Id.IndexB = uint8(primaryAxis.Index)
ie[1].Id.TypeA = ContactFeatureType.Vertex
ie[1].Id.TypeB = ContactFeatureType.Face
rf.I1 = primaryAxis.Index
if rf.I1+1 < collider.M_polygonB.Count {
rf.I2 = rf.I1 + 1
} else {
rf.I2 = 0
}
rf.V1 = collider.M_polygonB.Vertices[rf.I1]
rf.V2 = collider.M_polygonB.Vertices[rf.I2]
rf.Normal = collider.M_polygonB.Normals[rf.I1]
}
rf.SideNormal1.Set(rf.Normal.Y, -rf.Normal.X)
rf.SideNormal2 = rf.SideNormal1.OperatorNegate()
rf.SideOffset1 = Vec2Dot(rf.SideNormal1, rf.V1)
rf.SideOffset2 = Vec2Dot(rf.SideNormal2, rf.V2)
// Clip incident edge against extruded edge1 side edges.
clipPoints1 := make([]ClipVertex, 2)
clipPoints2 := make([]ClipVertex, 2)
np := 0
// Clip to box side 1
np = ClipSegmentToLine(clipPoints1, ie, rf.SideNormal1, rf.SideOffset1, rf.I1)
if np < maxManifoldPoints {
return
}
// Clip to negative box side 1
np = ClipSegmentToLine(clipPoints2, clipPoints1, rf.SideNormal2, rf.SideOffset2, rf.I2)
if np < maxManifoldPoints {
return
}
// Now clipPoints2 contains the clipped points.
if primaryAxis.Type == EPAxisType.EdgeA {
manifold.LocalNormal = rf.Normal
manifold.LocalPoint = rf.V1
} else {
manifold.LocalNormal = polygonB.M_normals[rf.I1]
manifold.LocalPoint = polygonB.M_vertices[rf.I1]
}
pointCount := 0
for i := 0; i < maxManifoldPoints; i++ {
separation := 0.0
separation = Vec2Dot(rf.Normal, Vec2Sub(clipPoints2[i].V, rf.V1))
if separation <= collider.M_radius {
cp := &manifold.Points[pointCount]
if primaryAxis.Type == EPAxisType.EdgeA {
cp.LocalPoint = TransformVec2MulT(collider.M_xf, clipPoints2[i].V)
cp.Id = clipPoints2[i].Id
} else {
cp.LocalPoint = clipPoints2[i].V
cp.Id.TypeA = clipPoints2[i].Id.TypeB
cp.Id.TypeB = clipPoints2[i].Id.TypeA
cp.Id.IndexA = clipPoints2[i].Id.IndexB
cp.Id.IndexB = clipPoints2[i].Id.IndexA
}
pointCount++
}
}
manifold.PointCount = pointCount
}
func (collider *EPCollider) ComputeEdgeSeparation() EPAxis {
axis := MakeEPAxis()
axis.Type = EPAxisType.EdgeA
if collider.M_front {
axis.Index = 0
} else {
axis.Index = 1
}
axis.Separation = maxFloat
for i := 0; i < collider.M_polygonB.Count; i++ {
s := Vec2Dot(collider.M_normal, Vec2Sub(collider.M_polygonB.Vertices[i], collider.M_v1))
if s < axis.Separation {
axis.Separation = s
}
}
return axis
}
func (collider *EPCollider) ComputePolygonSeparation() EPAxis {
axis := MakeEPAxis()
axis.Type = EPAxisType.Unknown
axis.Index = -1
axis.Separation = -maxFloat
perp := MakeVec2(-collider.M_normal.Y, collider.M_normal.X)
for i := 0; i < collider.M_polygonB.Count; i++ {
n := collider.M_polygonB.Normals[i].OperatorNegate()
s1 := Vec2Dot(n, Vec2Sub(collider.M_polygonB.Vertices[i], collider.M_v1))
s2 := Vec2Dot(n, Vec2Sub(collider.M_polygonB.Vertices[i], collider.M_v2))
s := math.Min(s1, s2)
if s > collider.M_radius {
// No collision
axis.Type = EPAxisType.EdgeB
axis.Index = i
axis.Separation = s
return axis
}
// Adjacency
if Vec2Dot(n, perp) >= 0.0 {
if Vec2Dot(Vec2Sub(n, collider.M_upperLimit), collider.M_normal) < -angularSlop {
continue
}
} else {
if Vec2Dot(Vec2Sub(n, collider.M_lowerLimit), collider.M_normal) < -angularSlop {
continue
}
}
if s > axis.Separation {
axis.Type = EPAxisType.EdgeB
axis.Index = i
axis.Separation = s
}
}
return axis
}
func CollideEdgeAndPolygon(manifold *Manifold, edgeA *EdgeShape, xfA Transform, polygonB *PolygonShape, xfB Transform) {
collider := MakeEPCollider()
collider.Collide(manifold, edgeA, xfA, polygonB, xfB)
}