forked from ByteArena/box2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollisionB2Distance.go
708 lines (605 loc) · 16.1 KB
/
CollisionB2Distance.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
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
package box2d
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// B2Distance.h
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/// A distance proxy is used by the GJK algorithm.
/// It encapsulates any shape.
type B2DistanceProxy struct {
M_buffer [2]B2Vec2
M_vertices []B2Vec2 // is a memory blob using pointer arithmetic in original implementation
M_count int
M_radius float64
}
func MakeB2DistanceProxy() B2DistanceProxy {
return B2DistanceProxy{
M_vertices: make([]B2Vec2, 0),
M_count: 0,
M_radius: 0.0,
}
}
func NewB2DistanceProxy() *B2DistanceProxy {
res := MakeB2DistanceProxy()
return &res
}
/// Used to warm start b2Distance.
/// Set count to zero on first call.
type B2SimplexCache struct {
Metric float64 ///< length or area
Count int
IndexA [3]int ///< vertices on shape A
IndexB [3]int ///< vertices on shape B
}
func MakeB2SimplexCache() B2SimplexCache {
return B2SimplexCache{
Metric: 0,
Count: 0,
IndexA: [3]int{}, ///< vertices on shape A
IndexB: [3]int{}, ///< vertices on shape B
}
}
func NewB2SimplexCache() *B2SimplexCache {
res := MakeB2SimplexCache()
return &res
}
/// Input for b2Distance.
/// You have to option to use the shape radii
/// in the computation. Even
type B2DistanceInput struct {
ProxyA B2DistanceProxy
ProxyB B2DistanceProxy
TransformA B2Transform
TransformB B2Transform
UseRadii bool
}
func MakeB2DistanceInput() B2DistanceInput {
return B2DistanceInput{
ProxyA: MakeB2DistanceProxy(),
ProxyB: MakeB2DistanceProxy(),
TransformA: MakeB2Transform(),
TransformB: MakeB2Transform(),
UseRadii: false,
}
}
func NewB2DistanceInput() *B2DistanceInput {
res := MakeB2DistanceInput()
return &res
}
/// Output for b2Distance.
type B2DistanceOutput struct {
PointA B2Vec2 ///< closest point on shapeA
PointB B2Vec2 ///< closest point on shapeB
Distance float64
Iterations int ///< number of GJK iterations used
}
func MakeB2DistanceOutput() B2DistanceOutput {
return B2DistanceOutput{
PointA: MakeB2Vec2(0, 0),
PointB: MakeB2Vec2(0, 0),
Distance: 0,
Iterations: 0,
}
}
func NewB2DistanceOutput() *B2DistanceOutput {
res := MakeB2DistanceOutput()
return &res
}
// //////////////////////////////////////////////////////////////////////////
func (p B2DistanceProxy) GetVertexCount() int {
return p.M_count
}
func (p B2DistanceProxy) GetVertex(index int) B2Vec2 {
B2Assert(0 <= index && index < p.M_count)
return p.M_vertices[index]
}
func (p B2DistanceProxy) GetSupport(d B2Vec2) int {
bestIndex := 0
bestValue := B2Vec2Dot(p.M_vertices[0], d)
for i := 1; i < p.M_count; i++ {
value := B2Vec2Dot(p.M_vertices[i], d)
if value > bestValue {
bestIndex = i
bestValue = value
}
}
return bestIndex
}
func (p B2DistanceProxy) GetSupportVertex(d B2Vec2) B2Vec2 {
bestIndex := 0
bestValue := B2Vec2Dot(p.M_vertices[0], d)
for i := 1; i < p.M_count; i++ {
value := B2Vec2Dot(p.M_vertices[i], d)
if value > bestValue {
bestIndex = i
bestValue = value
}
}
return p.M_vertices[bestIndex]
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// B2Distance.cpp
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// GJK using Voronoi regions (Christer Ericson) and Barycentric coordinates.
var b2_gjkCalls, b2_gjkIters, b2_gjkMaxIters int
func (p *B2DistanceProxy) Set(shape B2ShapeInterface, index int) {
switch shape.GetType() {
case B2Shape_Type.E_circle:
{
circle := (shape).(*B2CircleShape)
p.M_vertices = []B2Vec2{circle.M_p}
p.M_count = 1
p.M_radius = circle.M_radius
}
break
case B2Shape_Type.E_polygon:
{
polygon := shape.(*B2PolygonShape)
p.M_vertices = polygon.M_vertices[:]
p.M_count = polygon.M_count
p.M_radius = polygon.M_radius
}
break
case B2Shape_Type.E_chain:
{
chain := shape.(*B2ChainShape)
B2Assert(0 <= index && index < chain.M_count)
p.M_buffer[0] = chain.M_vertices[index]
if index+1 < chain.M_count {
p.M_buffer[1] = chain.M_vertices[index+1]
} else {
p.M_buffer[1] = chain.M_vertices[0]
}
p.M_vertices = p.M_buffer[:]
p.M_count = 2
p.M_radius = chain.M_radius
}
break
case B2Shape_Type.E_edge:
{
edge := shape.(*B2EdgeShape)
p.M_vertices = []B2Vec2{edge.M_vertex1, edge.M_vertex2}
p.M_count = 2
p.M_radius = edge.M_radius
}
break
default:
B2Assert(false)
}
}
type B2SimplexVertex struct {
WA B2Vec2 // support point in proxyA
WB B2Vec2 // support point in proxyB
W B2Vec2 // wB - wA
A float64 // barycentric coordinate for closest point
IndexA int // wA index
IndexB int // wB index
}
func MakeB2SimplexVertex() B2SimplexVertex {
return B2SimplexVertex{
WA: MakeB2Vec2(0, 0),
WB: MakeB2Vec2(0, 0),
W: MakeB2Vec2(0, 0),
A: 0,
IndexA: 0,
IndexB: 0,
}
}
func NewB2SimplexVertex() *B2SimplexVertex {
res := MakeB2SimplexVertex()
return &res
}
type B2Simplex struct {
//M_v1, M_v2, M_v3 *B2SimplexVertex
M_vs [3]B2SimplexVertex
M_count int
}
func MakeB2Simplex() B2Simplex {
return B2Simplex{
M_vs: [3]B2SimplexVertex{
MakeB2SimplexVertex(),
MakeB2SimplexVertex(),
MakeB2SimplexVertex(),
},
}
}
func NewB2Simplex() *B2Simplex {
res := MakeB2Simplex()
return &res
}
func (simplex *B2Simplex) ReadCache(cache *B2SimplexCache, proxyA *B2DistanceProxy, transformA B2Transform, proxyB *B2DistanceProxy, transformB B2Transform) {
B2Assert(cache.Count <= 3)
// Copy data from cache.
simplex.M_count = cache.Count
vertices := &simplex.M_vs
for i := 0; i < simplex.M_count; i++ {
v := &vertices[i]
v.IndexA = cache.IndexA[i]
v.IndexB = cache.IndexB[i]
wALocal := proxyA.GetVertex(v.IndexA)
wBLocal := proxyB.GetVertex(v.IndexB)
v.WA = B2TransformVec2Mul(transformA, wALocal)
v.WB = B2TransformVec2Mul(transformB, wBLocal)
v.W = B2Vec2Sub(v.WB, v.WA)
v.A = 0.0
}
// Compute the new simplex metric, if it is substantially different than
// old metric then flush the simplex.
if simplex.M_count > 1 {
metric1 := cache.Metric
metric2 := simplex.GetMetric()
if metric2 < 0.5*metric1 || 2.0*metric1 < metric2 || metric2 < B2_epsilon {
// Reset the simplex.
simplex.M_count = 0
}
}
// If the cache is empty or invalid ...
if simplex.M_count == 0 {
v := &vertices[0]
v.IndexA = 0
v.IndexB = 0
wALocal := proxyA.GetVertex(0)
wBLocal := proxyB.GetVertex(0)
v.WA = B2TransformVec2Mul(transformA, wALocal)
v.WB = B2TransformVec2Mul(transformB, wBLocal)
v.W = B2Vec2Sub(v.WB, v.WA)
v.A = 1.0
simplex.M_count = 1
}
}
func (simplex B2Simplex) WriteCache(cache *B2SimplexCache) {
cache.Metric = simplex.GetMetric()
cache.Count = simplex.M_count
vertices := &simplex.M_vs
for i := 0; i < simplex.M_count; i++ {
cache.IndexA[i] = vertices[i].IndexA
cache.IndexB[i] = vertices[i].IndexB
}
}
func (simplex B2Simplex) GetSearchDirection() B2Vec2 {
switch simplex.M_count {
case 1:
return simplex.M_vs[0].W.OperatorNegate()
case 2:
{
e12 := B2Vec2Sub(simplex.M_vs[1].W, simplex.M_vs[0].W)
sgn := B2Vec2Cross(e12, simplex.M_vs[0].W.OperatorNegate())
if sgn > 0.0 {
// Origin is left of e12.
return B2Vec2CrossScalarVector(1.0, e12)
} else {
// Origin is right of e12.
return B2Vec2CrossVectorScalar(e12, 1.0)
}
}
default:
B2Assert(false)
return B2Vec2_zero
}
}
func (simplex B2Simplex) GetClosestPoint() B2Vec2 {
switch simplex.M_count {
case 0:
B2Assert(false)
return B2Vec2_zero
case 1:
return simplex.M_vs[0].W
case 2:
return B2Vec2Add(
B2Vec2MulScalar(
simplex.M_vs[0].A,
simplex.M_vs[0].W,
),
B2Vec2MulScalar(
simplex.M_vs[1].A,
simplex.M_vs[1].W,
),
)
case 3:
return B2Vec2_zero
default:
B2Assert(false)
return B2Vec2_zero
}
}
func (simplex B2Simplex) GetWitnessPoints(pA *B2Vec2, pB *B2Vec2) {
switch simplex.M_count {
case 0:
B2Assert(false)
break
case 1:
*pA = simplex.M_vs[0].WA
*pB = simplex.M_vs[0].WB
break
case 2:
*pA = B2Vec2Add(
B2Vec2MulScalar(simplex.M_vs[0].A, simplex.M_vs[0].WA),
B2Vec2MulScalar(simplex.M_vs[1].A, simplex.M_vs[1].WA),
)
*pB = B2Vec2Add(
B2Vec2MulScalar(simplex.M_vs[0].A, simplex.M_vs[0].WB),
B2Vec2MulScalar(simplex.M_vs[1].A, simplex.M_vs[1].WB),
)
break
case 3:
*pA = B2Vec2Add(
B2Vec2Add(
B2Vec2MulScalar(simplex.M_vs[0].A, simplex.M_vs[0].WA),
B2Vec2MulScalar(simplex.M_vs[1].A, simplex.M_vs[1].WA),
),
B2Vec2MulScalar(simplex.M_vs[2].A, simplex.M_vs[2].WA),
)
*pB = *pA
break
default:
B2Assert(false)
break
}
}
func (simplex B2Simplex) GetMetric() float64 {
switch simplex.M_count {
case 0:
B2Assert(false)
return 0.0
case 1:
return 0.0
case 2:
return B2Vec2Distance(simplex.M_vs[0].W, simplex.M_vs[1].W)
case 3:
return B2Vec2Cross(
B2Vec2Sub(simplex.M_vs[1].W, simplex.M_vs[0].W),
B2Vec2Sub(simplex.M_vs[2].W, simplex.M_vs[0].W),
)
default:
B2Assert(false)
return 0.0
}
}
////////////////////////////////////////////////////
// Solve a line segment using barycentric coordinates.
func (simplex *B2Simplex) Solve2() {
w1 := simplex.M_vs[0].W
w2 := simplex.M_vs[1].W
e12 := B2Vec2Sub(w2, w1)
// w1 region
d12_2 := -B2Vec2Dot(w1, e12)
if d12_2 <= 0.0 {
// a2 <= 0, so we clamp it to 0
simplex.M_vs[0].A = 1.0
simplex.M_count = 1
return
}
// w2 region
d12_1 := B2Vec2Dot(w2, e12)
if d12_1 <= 0.0 {
// a1 <= 0, so we clamp it to 0
simplex.M_vs[1].A = 1.0
simplex.M_count = 1
simplex.M_vs[0] = simplex.M_vs[1]
return
}
// Must be in e12 region.
inv_d12 := 1.0 / (d12_1 + d12_2)
simplex.M_vs[0].A = d12_1 * inv_d12
simplex.M_vs[1].A = d12_2 * inv_d12
simplex.M_count = 2
}
// // Possible regions:
// // - points[2]
// // - edge points[0]-points[2]
// // - edge points[1]-points[2]
// // - inside the triangle
func (simplex *B2Simplex) Solve3() {
w1 := simplex.M_vs[0].W
w2 := simplex.M_vs[1].W
w3 := simplex.M_vs[2].W
// Edge12
// [1 1 ][a1] = [1]
// [w1.e12 w2.e12][a2] = [0]
// a3 = 0
e12 := B2Vec2Sub(w2, w1)
w1e12 := B2Vec2Dot(w1, e12)
w2e12 := B2Vec2Dot(w2, e12)
d12_1 := w2e12
d12_2 := -w1e12
// Edge13
// [1 1 ][a1] = [1]
// [w1.e13 w3.e13][a3] = [0]
// a2 = 0
e13 := B2Vec2Sub(w3, w1)
w1e13 := B2Vec2Dot(w1, e13)
w3e13 := B2Vec2Dot(w3, e13)
d13_1 := w3e13
d13_2 := -w1e13
// Edge23
// [1 1 ][a2] = [1]
// [w2.e23 w3.e23][a3] = [0]
// a1 = 0
e23 := B2Vec2Sub(w3, w2)
w2e23 := B2Vec2Dot(w2, e23)
w3e23 := B2Vec2Dot(w3, e23)
d23_1 := w3e23
d23_2 := -w2e23
// Triangle123
n123 := B2Vec2Cross(e12, e13)
d123_1 := n123 * B2Vec2Cross(w2, w3)
d123_2 := n123 * B2Vec2Cross(w3, w1)
d123_3 := n123 * B2Vec2Cross(w1, w2)
// w1 region
if d12_2 <= 0.0 && d13_2 <= 0.0 {
simplex.M_vs[0].A = 1.0
simplex.M_count = 1
return
}
// e12
if d12_1 > 0.0 && d12_2 > 0.0 && d123_3 <= 0.0 {
inv_d12 := 1.0 / (d12_1 + d12_2)
simplex.M_vs[0].A = d12_1 * inv_d12
simplex.M_vs[1].A = d12_2 * inv_d12
simplex.M_count = 2
return
}
// e13
if d13_1 > 0.0 && d13_2 > 0.0 && d123_2 <= 0.0 {
inv_d13 := 1.0 / (d13_1 + d13_2)
simplex.M_vs[0].A = d13_1 * inv_d13
simplex.M_vs[2].A = d13_2 * inv_d13
simplex.M_count = 2
simplex.M_vs[1] = simplex.M_vs[2]
return
}
// w2 region
if d12_1 <= 0.0 && d23_2 <= 0.0 {
simplex.M_vs[1].A = 1.0
simplex.M_count = 1
simplex.M_vs[0] = simplex.M_vs[1]
return
}
// w3 region
if d13_1 <= 0.0 && d23_1 <= 0.0 {
simplex.M_vs[2].A = 1.0
simplex.M_count = 1
simplex.M_vs[0] = simplex.M_vs[2]
return
}
// e23
if d23_1 > 0.0 && d23_2 > 0.0 && d123_1 <= 0.0 {
inv_d23 := 1.0 / (d23_1 + d23_2)
simplex.M_vs[1].A = d23_1 * inv_d23
simplex.M_vs[2].A = d23_2 * inv_d23
simplex.M_count = 2
simplex.M_vs[0] = simplex.M_vs[2]
return
}
// Must be in triangle123
inv_d123 := 1.0 / (d123_1 + d123_2 + d123_3)
simplex.M_vs[0].A = d123_1 * inv_d123
simplex.M_vs[1].A = d123_2 * inv_d123
simplex.M_vs[2].A = d123_3 * inv_d123
simplex.M_count = 3
}
func B2Distance(output *B2DistanceOutput, cache *B2SimplexCache, input *B2DistanceInput) {
b2_gjkCalls++
proxyA := &input.ProxyA
proxyB := &input.ProxyB
transformA := input.TransformA
transformB := input.TransformB
// Initialize the simplex.
simplex := MakeB2Simplex()
simplex.ReadCache(cache, proxyA, transformA, proxyB, transformB)
// Get simplex vertices as an array.
vertices := &simplex.M_vs
k_maxIters := 20
// These store the vertices of the last simplex so that we
// can check for duplicates and prevent cycling.
saveA := make([]int, 3)
saveB := make([]int, 3)
saveCount := 0
// Main iteration loop.
iter := 0
for iter < k_maxIters {
// Copy simplex so we can identify duplicates.
saveCount = simplex.M_count
for i := 0; i < saveCount; i++ {
saveA[i] = vertices[i].IndexA
saveB[i] = vertices[i].IndexB
}
switch simplex.M_count {
case 1:
break
case 2:
simplex.Solve2()
break
case 3:
simplex.Solve3()
break
default:
B2Assert(false)
}
// If we have 3 points, then the origin is in the corresponding triangle.
if simplex.M_count == 3 {
break
}
// Get search direction.
d := simplex.GetSearchDirection()
// Ensure the search direction is numerically fit.
if d.LengthSquared() < B2_epsilon*B2_epsilon {
// The origin is probably contained by a line segment
// or triangle. Thus the shapes are overlapped.
// We can't return zero here even though there may be overlap.
// In case the simplex is a point, segment, or triangle it is difficult
// to determine if the origin is contained in the CSO or very close to it.
break
}
// Compute a tentative new simplex vertex using support points.
vertex := &vertices[simplex.M_count]
vertex.IndexA = proxyA.GetSupport(
B2RotVec2MulT(transformA.Q, d.OperatorNegate()),
)
vertex.WA = B2TransformVec2Mul(transformA, proxyA.GetVertex(vertex.IndexA))
// b2Vec2 wBLocal;
vertex.IndexB = proxyB.GetSupport(B2RotVec2MulT(transformB.Q, d))
vertex.WB = B2TransformVec2Mul(transformB, proxyB.GetVertex(vertex.IndexB))
vertex.W = B2Vec2Sub(vertex.WB, vertex.WA)
// Iteration count is equated to the number of support point calls.
iter++
b2_gjkIters++
// Check for duplicate support points. This is the main termination criteria.
duplicate := false
for i := 0; i < saveCount; i++ {
if vertex.IndexA == saveA[i] && vertex.IndexB == saveB[i] {
duplicate = true
break
}
}
// If we found a duplicate support point we must exit to avoid cycling.
if duplicate {
break
}
// New vertex is ok and needed.
simplex.M_count++
}
if iter > b2_gjkMaxIters {
b2_gjkMaxIters = iter
}
// Prepare output.
simplex.GetWitnessPoints(&output.PointA, &output.PointB)
output.Distance = B2Vec2Distance(output.PointA, output.PointB)
output.Iterations = iter
// // Cache the simplex.
simplex.WriteCache(cache)
// // Apply radii if requested.
if input.UseRadii {
rA := proxyA.M_radius
rB := proxyB.M_radius
if output.Distance > rA+rB && output.Distance > B2_epsilon {
// Shapes are still no overlapped.
// Move the witness points to the outer surface.
output.Distance -= rA + rB
normal := B2Vec2Sub(output.PointB, output.PointA)
normal.Normalize()
output.PointA.OperatorPlusInplace(
B2Vec2MulScalar(rA, normal),
)
output.PointB.OperatorMinusInplace(
B2Vec2MulScalar(rB, normal),
)
} else {
// Shapes are overlapped when radii are considered.
// Move the witness points to the middle.
p := B2Vec2MulScalar(
0.5,
B2Vec2Add(output.PointA, output.PointB),
)
output.PointA = p
output.PointB = p
output.Distance = 0.0
}
}
}