-
Notifications
You must be signed in to change notification settings - Fork 48
/
DynamicsB2World.go
1318 lines (1050 loc) · 31.9 KB
/
DynamicsB2World.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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package box2d
import (
"fmt"
"math"
)
/// The world class manages all physics entities, dynamic simulation,
/// and asynchronous queries. The world also contains efficient memory
/// management facilities.
var B2World_Flags = struct {
E_newFixture int
E_locked int
E_clearForces int
}{
E_newFixture: 0x0001,
E_locked: 0x0002,
E_clearForces: 0x0004,
}
// /// The world class manages all physics entities, dynamic simulation,
// /// and asynchronous queries. The world also contains efficient memory
// /// management facilities.
type B2World struct {
M_flags int
M_contactManager B2ContactManager
M_bodyList *B2Body // linked list
M_jointList B2JointInterface // has to be backed by pointer
M_bodyCount int
M_jointCount int
M_gravity B2Vec2
M_allowSleep bool
M_destructionListener B2DestructionListenerInterface
//G_debugDraw *B2Draw
// This is used to compute the time step ratio to
// support a variable time step.
M_inv_dt0 float64
// These are for debugging the solver.
M_warmStarting bool
M_continuousPhysics bool
M_subStepping bool
M_stepComplete bool
M_profile B2Profile
}
func (world B2World) GetBodyList() *B2Body {
return world.M_bodyList
}
func (world B2World) GetJointList() B2JointInterface { // returns a pointer
return world.M_jointList
}
func (world B2World) GetContactList() B2ContactInterface { // returns a pointer
return world.M_contactManager.M_contactList
}
func (world B2World) GetBodyCount() int {
return world.M_bodyCount
}
func (world B2World) GetJointCount() int {
return world.M_jointCount
}
func (world B2World) GetContactCount() int {
return world.M_contactManager.M_contactCount
}
func (world *B2World) SetGravity(gravity B2Vec2) {
world.M_gravity = gravity
}
func (world B2World) GetGravity() B2Vec2 {
return world.M_gravity
}
func (world B2World) IsLocked() bool {
return (world.M_flags & B2World_Flags.E_locked) == B2World_Flags.E_locked
}
func (world *B2World) SetAutoClearForces(flag bool) {
if flag {
world.M_flags |= B2World_Flags.E_clearForces
} else {
world.M_flags &= ^B2World_Flags.E_clearForces
}
}
/// Get the flag that controls automatic clearing of forces after each time step.
func (world B2World) GetAutoClearForces() bool {
return (world.M_flags & B2World_Flags.E_clearForces) == B2World_Flags.E_clearForces
}
func (world B2World) GetContactManager() B2ContactManager {
return world.M_contactManager
}
func (world B2World) GetProfile() B2Profile {
return world.M_profile
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// B2World.cpp
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
func MakeB2World(gravity B2Vec2) B2World {
world := B2World{}
world.M_destructionListener = nil
//world.G_debugDraw = nil
world.M_bodyList = nil
world.M_jointList = nil
world.M_bodyCount = 0
world.M_jointCount = 0
world.M_warmStarting = true
world.M_continuousPhysics = true
world.M_subStepping = false
world.M_stepComplete = true
world.M_allowSleep = true
world.M_gravity = gravity
world.M_flags = B2World_Flags.E_clearForces
world.M_inv_dt0 = 0.0
world.M_contactManager = MakeB2ContactManager()
return world
}
func (world *B2World) Destroy() {
// Some shapes allocate using b2Alloc.
b := world.M_bodyList
for b != nil {
bNext := b.M_next
f := b.M_fixtureList
for f != nil {
fNext := f.M_next
f.M_proxyCount = 0
f.Destroy()
f = fNext
}
b = bNext
}
}
func (world *B2World) SetDestructionListener(listener B2DestructionListenerInterface) {
world.M_destructionListener = listener
}
func (world *B2World) SetContactFilter(filter B2ContactFilterInterface) {
world.M_contactManager.M_contactFilter = filter
}
func (world *B2World) SetContactListener(listener B2ContactListenerInterface) {
world.M_contactManager.M_contactListener = listener
}
// void (world *B2World) SetDebugDraw(b2Draw* debugDraw)
// {
// g_debugDraw = debugDraw;
// }
func (world *B2World) CreateBody(def *B2BodyDef) *B2Body {
B2Assert(world.IsLocked() == false)
if world.IsLocked() {
return nil
}
b := NewB2Body(def, world)
// Add to world doubly linked list.
b.M_prev = nil
b.M_next = world.M_bodyList
if world.M_bodyList != nil {
world.M_bodyList.M_prev = b
}
world.M_bodyList = b
world.M_bodyCount++
return b
}
func (world *B2World) DestroyBody(b *B2Body) {
B2Assert(world.M_bodyCount > 0)
B2Assert(world.IsLocked() == false)
if world.IsLocked() {
return
}
// Delete the attached joints.
je := b.M_jointList
for je != nil {
je0 := je
je = je.Next
if world.M_destructionListener != nil {
world.M_destructionListener.SayGoodbyeToJoint(je0.Joint)
}
world.DestroyJoint(je0.Joint)
b.M_jointList = je
}
b.M_jointList = nil
// Delete the attached contacts.
ce := b.M_contactList
for ce != nil {
ce0 := ce
ce = ce.Next
world.M_contactManager.Destroy(ce0.Contact)
}
b.M_contactList = nil
// Delete the attached fixtures. This destroys broad-phase proxies.
f := b.M_fixtureList
for f != nil {
f0 := f
f = f.M_next
if world.M_destructionListener != nil {
world.M_destructionListener.SayGoodbyeToFixture(f0)
}
f0.DestroyProxies(&world.M_contactManager.M_broadPhase)
f0.Destroy()
b.M_fixtureList = f
b.M_fixtureCount -= 1
}
b.M_fixtureList = nil
b.M_fixtureCount = 0
// Remove world body list.
if b.M_prev != nil {
b.M_prev.M_next = b.M_next
}
if b.M_next != nil {
b.M_next.M_prev = b.M_prev
}
if b == world.M_bodyList {
world.M_bodyList = b.M_next
}
world.M_bodyCount--
}
func (world *B2World) CreateJoint(def B2JointDefInterface) B2JointInterface {
B2Assert(world.IsLocked() == false)
if world.IsLocked() {
return nil
}
j := B2JointCreate(def)
// Connect to the world list.
j.SetPrev(nil)
j.SetNext(world.M_jointList)
if world.M_jointList != nil {
world.M_jointList.SetPrev(j)
}
world.M_jointList = j
world.M_jointCount++
// Connect to the bodies' doubly linked lists.
j.GetEdgeA().Joint = j
j.GetEdgeA().Other = j.GetBodyB()
j.GetEdgeA().Prev = nil
j.GetEdgeA().Next = j.GetBodyA().M_jointList
if j.GetBodyA().M_jointList != nil {
j.GetBodyA().M_jointList.Prev = j.GetEdgeA()
}
j.GetBodyA().M_jointList = j.GetEdgeA()
j.GetEdgeB().Joint = j
j.GetEdgeB().Other = j.GetBodyA()
j.GetEdgeB().Prev = nil
j.GetEdgeB().Next = j.GetBodyB().M_jointList
if j.GetBodyB().M_jointList != nil {
j.GetBodyB().M_jointList.Prev = j.GetEdgeB()
}
j.GetBodyB().M_jointList = j.GetEdgeB()
bodyA := def.GetBodyA()
bodyB := def.GetBodyB()
// If the joint prevents collisions, then flag any contacts for filtering.
if def.IsCollideConnected() == false {
edge := bodyB.GetContactList()
for edge != nil {
if edge.Other == bodyA {
// Flag the contact for filtering at the next time step (where either
// body is awake).
edge.Contact.FlagForFiltering()
}
edge = edge.Next
}
}
// Note: creating a joint doesn't wake the bodies.
return j
}
func (world *B2World) DestroyJoint(j B2JointInterface) { // j backed by pointer
B2Assert(world.IsLocked() == false)
if world.IsLocked() {
return
}
collideConnected := j.IsCollideConnected()
// Remove from the doubly linked list.
if j.GetPrev() != nil {
j.GetPrev().SetNext(j.GetNext())
}
if j.GetNext() != nil {
j.GetNext().SetPrev(j.GetPrev())
}
if j == world.M_jointList {
world.M_jointList = j.GetNext()
}
// Disconnect from island graph.
bodyA := j.GetBodyA()
bodyB := j.GetBodyB()
// Wake up connected bodies.
bodyA.SetAwake(true)
bodyB.SetAwake(true)
// Remove from body 1.
if j.GetEdgeA().Prev != nil {
j.GetEdgeA().Prev.Next = j.GetEdgeA().Next
}
if j.GetEdgeA().Next != nil {
j.GetEdgeA().Next.Prev = j.GetEdgeA().Prev
}
if j.GetEdgeA() == bodyA.M_jointList {
bodyA.M_jointList = j.GetEdgeA().Next
}
j.GetEdgeA().Prev = nil
j.GetEdgeA().Next = nil
// Remove from body 2
if j.GetEdgeB().Prev != nil {
j.GetEdgeB().Prev.Next = j.GetEdgeB().Next
}
if j.GetEdgeB().Next != nil {
j.GetEdgeB().Next.Prev = j.GetEdgeB().Prev
}
if j.GetEdgeB() == bodyB.M_jointList {
bodyB.M_jointList = j.GetEdgeB().Next
}
j.GetEdgeB().Prev = nil
j.GetEdgeB().Next = nil
B2JointDestroy(j)
B2Assert(world.M_jointCount > 0)
world.M_jointCount--
// If the joint prevents collisions, then flag any contacts for filtering.
if collideConnected == false {
edge := bodyB.GetContactList()
for edge != nil {
if edge.Other == bodyA {
// Flag the contact for filtering at the next time step (where either
// body is awake).
edge.Contact.FlagForFiltering()
}
edge = edge.Next
}
}
}
func (world *B2World) SetAllowSleeping(flag bool) {
if flag == world.M_allowSleep {
return
}
world.M_allowSleep = flag
if world.M_allowSleep == false {
for b := world.M_bodyList; b != nil; b = b.M_next {
b.SetAwake(true)
}
}
}
// Find islands, integrate and solve constraints, solve position constraints
func (world *B2World) Solve(step B2TimeStep) {
world.M_profile.SolveInit = 0.0
world.M_profile.SolveVelocity = 0.0
world.M_profile.SolvePosition = 0.0
// Size the island for the worst case.
island := MakeB2Island(
world.M_bodyCount,
world.M_contactManager.M_contactCount,
world.M_jointCount,
world.M_contactManager.M_contactListener,
)
// Clear all the island flags.
for b := world.M_bodyList; b != nil; b = b.M_next {
b.M_flags &= ^B2Body_Flags.E_islandFlag
}
for c := world.M_contactManager.M_contactList; c != nil; c = c.GetNext() {
c.SetFlags(c.GetFlags() & ^B2Body_Flags.E_islandFlag)
}
for j := world.M_jointList; j != nil; j = j.GetNext() {
j.SetIslandFlag(false)
}
// Build and simulate all awake islands.
stackSize := world.M_bodyCount
stack := make([]*B2Body, stackSize)
for seed := world.M_bodyList; seed != nil; seed = seed.M_next {
if (seed.M_flags & B2Body_Flags.E_islandFlag) != 0x0000 {
continue
}
if seed.IsAwake() == false || seed.IsActive() == false {
continue
}
// The seed can be dynamic or kinematic.
if seed.GetType() == B2BodyType.B2_staticBody {
continue
}
// Reset island and stack.
island.Clear()
stackCount := 0
stack[stackCount] = seed
stackCount++
seed.M_flags |= B2Body_Flags.E_islandFlag
// Perform a depth first search (DFS) on the constraint graph.
for stackCount > 0 {
// Grab the next body off the stack and add it to the island.
stackCount--
b := stack[stackCount]
B2Assert(b.IsActive() == true)
island.AddBody(b)
// Make sure the body is awake (without resetting sleep timer).
b.M_flags |= B2Body_Flags.E_awakeFlag
// To keep islands as small as possible, we don't
// propagate islands across static bodies.
if b.GetType() == B2BodyType.B2_staticBody {
continue
}
// Search all contacts connected to this body.
for ce := b.M_contactList; ce != nil; ce = ce.Next {
contact := ce.Contact
// Has this contact already been added to an island?
if (contact.GetFlags() & B2Body_Flags.E_islandFlag) != 0x0000 {
continue
}
// Is this contact solid and touching?
if contact.IsEnabled() == false || contact.IsTouching() == false {
continue
}
// Skip sensors.
sensorA := contact.GetFixtureA().M_isSensor
sensorB := contact.GetFixtureB().M_isSensor
if sensorA || sensorB {
continue
}
island.AddContact(contact)
contact.SetFlags(contact.GetFlags() | B2Body_Flags.E_islandFlag)
other := ce.Other
// Was the other body already added to this island?
if (other.M_flags & B2Body_Flags.E_islandFlag) != 0x0000 {
continue
}
B2Assert(stackCount < stackSize)
stack[stackCount] = other
stackCount++
other.M_flags |= B2Body_Flags.E_islandFlag
}
// Search all joints connect to this body.
for je := b.M_jointList; je != nil; je = je.Next {
if je.Joint.GetIslandFlag() == true {
continue
}
other := je.Other
// Don't simulate joints connected to inactive bodies.
if other.IsActive() == false {
continue
}
island.Add(je.Joint)
je.Joint.SetIslandFlag(true)
if other.M_flags&B2Body_Flags.E_islandFlag != 0x0000 {
continue
}
B2Assert(stackCount < stackSize)
stack[stackCount] = other
stackCount++
other.M_flags |= B2Body_Flags.E_islandFlag
}
}
profile := MakeB2Profile()
island.Solve(&profile, step, world.M_gravity, world.M_allowSleep)
world.M_profile.SolveInit += profile.SolveInit
world.M_profile.SolveVelocity += profile.SolveVelocity
world.M_profile.SolvePosition += profile.SolvePosition
// Post solve cleanup.
for i := 0; i < island.M_bodyCount; i++ {
// Allow static bodies to participate in other islands.
b := island.M_bodies[i]
if b.GetType() == B2BodyType.B2_staticBody {
b.M_flags &= ^B2Body_Flags.E_islandFlag
}
}
}
stack = nil
{
timer := MakeB2Timer()
// Synchronize fixtures, check for out of range bodies.
for b := world.M_bodyList; b != nil; b = b.GetNext() {
// If a body was not in an island then it did not move.
if (b.M_flags & B2Body_Flags.E_islandFlag) == 0 {
continue
}
if b.GetType() == B2BodyType.B2_staticBody {
continue
}
// Update fixtures (for broad-phase).
b.SynchronizeFixtures()
}
// Look for new contacts.
world.M_contactManager.FindNewContacts()
world.M_profile.Broadphase = timer.GetMilliseconds()
}
}
// Find TOI contacts and solve them.
func (world *B2World) SolveTOI(step B2TimeStep) {
island := MakeB2Island(2*B2_maxTOIContacts, B2_maxTOIContacts, 0, world.M_contactManager.M_contactListener)
if world.M_stepComplete {
for b := world.M_bodyList; b != nil; b = b.M_next {
b.M_flags &= ^B2Body_Flags.E_islandFlag
b.M_sweep.Alpha0 = 0.0
}
for c := world.M_contactManager.M_contactList; c != nil; c = c.GetNext() {
// Invalidate TOI
c.SetFlags(c.GetFlags() & ^(B2Contact_Flag.E_toiFlag | B2Contact_Flag.E_islandFlag))
c.SetTOICount(0)
c.SetTOI(1.0)
}
}
// Find TOI events and solve them.
for {
// Find the first TOI.
var minContact B2ContactInterface = nil // has to be a pointer
minAlpha := 1.0
for c := world.M_contactManager.M_contactList; c != nil; c = c.GetNext() {
// Is this contact disabled?
if c.IsEnabled() == false {
continue
}
// Prevent excessive sub-stepping.
if c.GetTOICount() > B2_maxSubSteps {
continue
}
alpha := 1.0
if (c.GetFlags() & B2Contact_Flag.E_toiFlag) != 0x0000 {
// This contact has a valid cached TOI.
alpha = c.GetTOI()
} else {
fA := c.GetFixtureA()
fB := c.GetFixtureB()
// Is there a sensor?
if fA.IsSensor() || fB.IsSensor() {
continue
}
bA := fA.GetBody()
bB := fB.GetBody()
typeA := bA.M_type
typeB := bB.M_type
B2Assert(typeA == B2BodyType.B2_dynamicBody || typeB == B2BodyType.B2_dynamicBody)
activeA := bA.IsAwake() && typeA != B2BodyType.B2_staticBody
activeB := bB.IsAwake() && typeB != B2BodyType.B2_staticBody
// Is at least one body active (awake and dynamic or kinematic)?
if activeA == false && activeB == false {
continue
}
collideA := bA.IsBullet() || typeA != B2BodyType.B2_dynamicBody
collideB := bB.IsBullet() || typeB != B2BodyType.B2_dynamicBody
// Are these two non-bullet dynamic bodies?
if collideA == false && collideB == false {
continue
}
// Compute the TOI for this contact.
// Put the sweeps onto the same time interval.
alpha0 := bA.M_sweep.Alpha0
if bA.M_sweep.Alpha0 < bB.M_sweep.Alpha0 {
alpha0 = bB.M_sweep.Alpha0
bA.M_sweep.Advance(alpha0)
} else if bB.M_sweep.Alpha0 < bA.M_sweep.Alpha0 {
alpha0 = bA.M_sweep.Alpha0
bB.M_sweep.Advance(alpha0)
}
B2Assert(alpha0 < 1.0)
indexA := c.GetChildIndexA()
indexB := c.GetChildIndexB()
// Compute the time of impact in interval [0, minTOI]
input := MakeB2TOIInput()
input.ProxyA.Set(fA.GetShape(), indexA)
input.ProxyB.Set(fB.GetShape(), indexB)
input.SweepA = bA.M_sweep
input.SweepB = bB.M_sweep
input.TMax = 1.0
output := MakeB2TOIOutput()
B2TimeOfImpact(&output, &input)
// Beta is the fraction of the remaining portion of the .
beta := output.T
if output.State == B2TOIOutput_State.E_touching {
alpha = math.Min(alpha0+(1.0-alpha0)*beta, 1.0)
} else {
alpha = 1.0
}
c.SetTOI(alpha)
c.SetFlags(c.GetFlags() | B2Contact_Flag.E_toiFlag)
}
if alpha < minAlpha {
// This is the minimum TOI found so far.
minContact = c
minAlpha = alpha
}
}
if minContact == nil || 1.0-10.0*B2_epsilon < minAlpha {
// No more TOI events. Done!
world.M_stepComplete = true
break
}
// Advance the bodies to the TOI.
fA := minContact.GetFixtureA()
fB := minContact.GetFixtureB()
bA := fA.GetBody()
bB := fB.GetBody()
backup1 := bA.M_sweep
backup2 := bB.M_sweep
bA.Advance(minAlpha)
bB.Advance(minAlpha)
// The TOI contact likely has some new contact points.
B2ContactUpdate(minContact, world.M_contactManager.M_contactListener)
minContact.SetFlags(minContact.GetFlags() & ^B2Contact_Flag.E_toiFlag)
minContact.SetTOICount(minContact.GetTOICount() + 1)
// Is the contact solid?
if minContact.IsEnabled() == false || minContact.IsTouching() == false {
// Restore the sweeps.
minContact.SetEnabled(false)
bA.M_sweep = backup1
bB.M_sweep = backup2
bA.SynchronizeTransform()
bB.SynchronizeTransform()
continue
}
bA.SetAwake(true)
bB.SetAwake(true)
// Build the island
island.Clear()
island.AddBody(bA)
island.AddBody(bB)
island.AddContact(minContact)
bA.M_flags |= B2Body_Flags.E_islandFlag
bB.M_flags |= B2Body_Flags.E_islandFlag
minContact.SetFlags(minContact.GetFlags() | B2Contact_Flag.E_islandFlag)
// Get contacts on bodyA and bodyB.
bodies := [2]*B2Body{bA, bB}
for i := 0; i < 2; i++ {
body := bodies[i]
if body.M_type == B2BodyType.B2_dynamicBody {
for ce := body.M_contactList; ce != nil; ce = ce.Next {
if island.M_bodyCount == island.M_bodyCapacity {
break
}
if island.M_contactCount == island.M_contactCapacity {
break
}
contact := ce.Contact
// Has this contact already been added to the island?
if (contact.GetFlags() & B2Contact_Flag.E_islandFlag) != 0x0000 {
continue
}
// Only add static, kinematic, or bullet bodies.
other := ce.Other
if other.M_type == B2BodyType.B2_dynamicBody && body.IsBullet() == false && other.IsBullet() == false {
continue
}
// Skip sensors.
sensorA := contact.GetFixtureA().M_isSensor
sensorB := contact.GetFixtureB().M_isSensor
if sensorA || sensorB {
continue
}
// Tentatively advance the body to the TOI.
backup := other.M_sweep
if (other.M_flags & B2Body_Flags.E_islandFlag) == 0 {
other.Advance(minAlpha)
}
// Update the contact points
B2ContactUpdate(contact, world.M_contactManager.M_contactListener)
// Was the contact disabled by the user?
if contact.IsEnabled() == false {
other.M_sweep = backup
other.SynchronizeTransform()
continue
}
// Are there contact points?
if contact.IsTouching() == false {
other.M_sweep = backup
other.SynchronizeTransform()
continue
}
// Add the contact to the island
contact.SetFlags(contact.GetFlags() | B2Contact_Flag.E_islandFlag)
island.AddContact(contact)
// Has the other body already been added to the island?
if (other.M_flags & B2Body_Flags.E_islandFlag) != 0x0000 {
continue
}
// Add the other body to the island.
other.M_flags |= B2Body_Flags.E_islandFlag
if other.M_type != B2BodyType.B2_staticBody {
other.SetAwake(true)
}
island.AddBody(other)
}
}
}
subStep := MakeB2TimeStep()
subStep.Dt = (1.0 - minAlpha) * step.Dt
subStep.Inv_dt = 1.0 / subStep.Dt
subStep.DtRatio = 1.0
subStep.PositionIterations = 20
subStep.VelocityIterations = step.VelocityIterations
subStep.WarmStarting = false
island.SolveTOI(subStep, bA.M_islandIndex, bB.M_islandIndex)
// Reset island flags and synchronize broad-phase proxies.
for i := 0; i < island.M_bodyCount; i++ {
body := island.M_bodies[i]
body.M_flags &= ^B2Body_Flags.E_islandFlag
if body.M_type != B2BodyType.B2_dynamicBody {
continue
}
body.SynchronizeFixtures()
// Invalidate all contact TOIs on this displaced body.
for ce := body.M_contactList; ce != nil; ce = ce.Next {
ce.Contact.SetFlags(ce.Contact.GetFlags() & ^(B2Contact_Flag.E_toiFlag | B2Contact_Flag.E_islandFlag))
}
}
// Commit fixture proxy movements to the broad-phase so that new contacts are created.
// Also, some contacts can be destroyed.
world.M_contactManager.FindNewContacts()
if world.M_subStepping {
world.M_stepComplete = false
break
}
}
}
func (world *B2World) Step(dt float64, velocityIterations int, positionIterations int) {
stepTimer := MakeB2Timer()
// If new fixtures were added, we need to find the new contacts.
if (world.M_flags & B2World_Flags.E_newFixture) != 0x0000 {
world.M_contactManager.FindNewContacts()
world.M_flags &= ^B2World_Flags.E_newFixture
}
world.M_flags |= B2World_Flags.E_locked
step := MakeB2TimeStep()
step.Dt = dt
step.VelocityIterations = velocityIterations
step.PositionIterations = positionIterations
if dt > 0.0 {
step.Inv_dt = 1.0 / dt
} else {
step.Inv_dt = 0.0
}
step.DtRatio = world.M_inv_dt0 * dt
step.WarmStarting = world.M_warmStarting
// Update contacts. This is where some contacts are destroyed.
{
timer := MakeB2Timer()
world.M_contactManager.Collide()
world.M_profile.Collide = timer.GetMilliseconds()
}
// Integrate velocities, solve velocity constraints, and integrate positions.
if world.M_stepComplete && step.Dt > 0.0 {
timer := MakeB2Timer()
world.Solve(step)
world.M_profile.Solve = timer.GetMilliseconds()
}
// Handle TOI events.
if world.M_continuousPhysics && step.Dt > 0.0 {
timer := MakeB2Timer()
world.SolveTOI(step)
world.M_profile.SolveTOI = timer.GetMilliseconds()
}
if step.Dt > 0.0 {
world.M_inv_dt0 = step.Inv_dt
}
if (world.M_flags & B2World_Flags.E_clearForces) != 0x0000 {
world.ClearForces()
}
world.M_flags &= ^B2World_Flags.E_locked
world.M_profile.Step = stepTimer.GetMilliseconds()
}
func (world *B2World) ClearForces() {
for body := world.M_bodyList; body != nil; body = body.GetNext() {
body.M_force.SetZero()
body.M_torque = 0.0
}
}
type B2WorldQueryWrapper struct {
BroadPhase *B2BroadPhase
Callback B2BroadPhaseQueryCallback
}
func MakeB2WorldQueryWrapper() B2WorldQueryWrapper {
return B2WorldQueryWrapper{}
}
func (query *B2WorldQueryWrapper) QueryCallback(proxyId int) bool {
proxy := query.BroadPhase.GetUserData(proxyId).(*B2FixtureProxy)
return query.Callback(proxy.Fixture)
}
func (world *B2World) QueryAABB(callback B2BroadPhaseQueryCallback, aabb B2AABB) {
wrapper := MakeB2WorldQueryWrapper()
wrapper.BroadPhase = &world.M_contactManager.M_broadPhase
wrapper.Callback = callback
world.M_contactManager.M_broadPhase.Query(wrapper.QueryCallback, aabb)
}
func (world *B2World) RayCast(callback B2RaycastCallback, point1 B2Vec2, point2 B2Vec2) {
// B2TreeRayCastCallback
wrapper := func(input B2RayCastInput, nodeId int) float64 {
userData := world.M_contactManager.M_broadPhase.GetUserData(nodeId)
proxy := userData.(*B2FixtureProxy)
fixture := proxy.Fixture
index := proxy.ChildIndex
output := MakeB2RayCastOutput()
hit := fixture.RayCast(&output, input, index)
if hit {
fraction := output.Fraction
point := B2Vec2Add(B2Vec2MulScalar((1.0-fraction), input.P1), B2Vec2MulScalar(fraction, input.P2))
return callback(fixture, point, output.Normal, fraction)
}
return input.MaxFraction
}
input := MakeB2RayCastInput()
input.MaxFraction = 1.0
input.P1 = point1