-
Notifications
You must be signed in to change notification settings - Fork 48
/
DynamicsB2Body.go
959 lines (752 loc) · 23.6 KB
/
DynamicsB2Body.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
package box2d
import (
"fmt"
)
/// The body type.
/// static: zero mass, zero velocity, may be manually moved
/// kinematic: zero mass, non-zero velocity set by user, moved by solver
/// dynamic: positive mass, non-zero velocity determined by forces, moved by solver
var B2BodyType = struct {
B2_staticBody uint8
B2_kinematicBody uint8
B2_dynamicBody uint8
}{
B2_staticBody: 0,
B2_kinematicBody: 1,
B2_dynamicBody: 2,
}
/// A body definition holds all the data needed to construct a rigid body.
/// You can safely re-use body definitions. Shapes are added to a body after construction.
type B2BodyDef struct {
/// The body type: static, kinematic, or dynamic.
/// Note: if a dynamic body would have zero mass, the mass is set to one.
Type uint8
/// The world position of the body. Avoid creating bodies at the origin
/// since this can lead to many overlapping shapes.
Position B2Vec2
/// The world angle of the body in radians.
Angle float64
/// The linear velocity of the body's origin in world co-ordinates.
LinearVelocity B2Vec2
/// The angular velocity of the body.
AngularVelocity float64
/// Linear damping is use to reduce the linear velocity. The damping parameter
/// can be larger than 1.0 but the damping effect becomes sensitive to the
/// time step when the damping parameter is large.
/// Units are 1/time
LinearDamping float64
/// Angular damping is use to reduce the angular velocity. The damping parameter
/// can be larger than 1.0 but the damping effect becomes sensitive to the
/// time step when the damping parameter is large.
/// Units are 1/time
AngularDamping float64
/// Set this flag to false if this body should never fall asleep. Note that
/// this increases CPU usage.
AllowSleep bool
/// Is this body initially awake or sleeping?
Awake bool
/// Should this body be prevented from rotating? Useful for characters.
FixedRotation bool
/// Is this a fast moving body that should be prevented from tunneling through
/// other moving bodies? Note that all bodies are prevented from tunneling through
/// kinematic and static bodies. This setting is only considered on dynamic bodies.
/// @warning You should use this flag sparingly since it increases processing time.
Bullet bool
/// Does this body start out active?
Active bool
/// Use this to store application specific body data.
UserData interface{}
/// Scale the gravity applied to this body.
GravityScale float64
}
/// This constructor sets the body definition default values.
func MakeB2BodyDef() B2BodyDef {
return B2BodyDef{
UserData: nil,
Position: MakeB2Vec2(0, 0),
Angle: 0.0,
LinearVelocity: MakeB2Vec2(0, 0),
AngularVelocity: 0.0,
LinearDamping: 0.0,
AngularDamping: 0.0,
AllowSleep: true,
Awake: true,
FixedRotation: false,
Bullet: false,
Type: B2BodyType.B2_staticBody,
Active: true,
GravityScale: 1.0,
}
}
func NewB2BodyDef() *B2BodyDef {
res := MakeB2BodyDef()
return &res
}
var B2Body_Flags = struct {
E_islandFlag uint32
E_awakeFlag uint32
E_autoSleepFlag uint32
E_bulletFlag uint32
E_fixedRotationFlag uint32
E_activeFlag uint32
E_toiFlag uint32
}{
E_islandFlag: 0x0001,
E_awakeFlag: 0x0002,
E_autoSleepFlag: 0x0004,
E_bulletFlag: 0x0008,
E_fixedRotationFlag: 0x0010,
E_activeFlag: 0x0020,
E_toiFlag: 0x0040,
}
type B2Body struct {
M_type uint8
M_flags uint32
M_islandIndex int
M_xf B2Transform // the body origin transform
M_sweep B2Sweep // the swept motion for CCD
M_linearVelocity B2Vec2
M_angularVelocity float64
M_force B2Vec2
M_torque float64
M_world *B2World
M_prev *B2Body
M_next *B2Body
M_fixtureList *B2Fixture // linked list
M_fixtureCount int
M_jointList *B2JointEdge // linked list
M_contactList *B2ContactEdge // linked list
M_mass, M_invMass float64
// Rotational inertia about the center of mass.
M_I, M_invI float64
M_linearDamping float64
M_angularDamping float64
M_gravityScale float64
M_sleepTime float64
M_userData interface{}
}
func (body B2Body) GetType() uint8 {
return body.M_type
}
func (body B2Body) GetTransform() B2Transform {
return body.M_xf
}
func (body B2Body) GetPosition() B2Vec2 {
return body.M_xf.P
}
func (body B2Body) GetAngle() float64 {
return body.M_sweep.A
}
func (body B2Body) GetWorldCenter() B2Vec2 {
return body.M_sweep.C
}
func (body B2Body) GetLocalCenter() B2Vec2 {
return body.M_sweep.LocalCenter
}
func (body *B2Body) SetLinearVelocity(v B2Vec2) {
if body.M_type == B2BodyType.B2_staticBody {
return
}
if B2Vec2Dot(v, v) > 0.0 {
body.SetAwake(true)
}
body.M_linearVelocity = v
}
func (body B2Body) GetLinearVelocity() B2Vec2 {
return body.M_linearVelocity
}
func (body *B2Body) SetAngularVelocity(w float64) {
if body.M_type == B2BodyType.B2_staticBody {
return
}
if w*w > 0.0 {
body.SetAwake(true)
}
body.M_angularVelocity = w
}
func (body B2Body) GetAngularVelocity() float64 {
return body.M_angularVelocity
}
func (body B2Body) GetMass() float64 {
return body.M_mass
}
func (body B2Body) GetInertia() float64 {
return body.M_I + body.M_mass*B2Vec2Dot(body.M_sweep.LocalCenter, body.M_sweep.LocalCenter)
}
func (body B2Body) GetMassData(data *B2MassData) {
data.Mass = body.M_mass
data.I = body.M_I + body.M_mass*B2Vec2Dot(body.M_sweep.LocalCenter, body.M_sweep.LocalCenter)
data.Center = body.M_sweep.LocalCenter
}
func (body B2Body) GetWorldPoint(localPoint B2Vec2) B2Vec2 {
return B2TransformVec2Mul(body.M_xf, localPoint)
}
func (body B2Body) GetWorldVector(localVector B2Vec2) B2Vec2 {
return B2RotVec2Mul(body.M_xf.Q, localVector)
}
func (body B2Body) GetLocalPoint(worldPoint B2Vec2) B2Vec2 {
return B2TransformVec2MulT(body.M_xf, worldPoint)
}
func (body B2Body) GetLocalVector(worldVector B2Vec2) B2Vec2 {
return B2RotVec2MulT(body.M_xf.Q, worldVector)
}
func (body B2Body) GetLinearVelocityFromWorldPoint(worldPoint B2Vec2) B2Vec2 {
return B2Vec2Add(body.M_linearVelocity, B2Vec2CrossScalarVector(body.M_angularVelocity, B2Vec2Sub(worldPoint, body.M_sweep.C)))
}
func (body B2Body) GetLinearVelocityFromLocalPoint(localPoint B2Vec2) B2Vec2 {
return body.GetLinearVelocityFromWorldPoint(body.GetWorldPoint(localPoint))
}
func (body B2Body) GetLinearDamping() float64 {
return body.M_linearDamping
}
func (body *B2Body) SetLinearDamping(linearDamping float64) {
body.M_linearDamping = linearDamping
}
func (body B2Body) GetAngularDamping() float64 {
return body.M_angularDamping
}
func (body *B2Body) SetAngularDamping(angularDamping float64) {
body.M_angularDamping = angularDamping
}
func (body B2Body) GetGravityScale() float64 {
return body.M_gravityScale
}
func (body *B2Body) SetGravityScale(scale float64) {
body.M_gravityScale = scale
}
func (body *B2Body) SetBullet(flag bool) {
if flag {
body.M_flags |= B2Body_Flags.E_bulletFlag
} else {
body.M_flags &= ^B2Body_Flags.E_bulletFlag
}
}
func (body B2Body) IsBullet() bool {
return (body.M_flags & B2Body_Flags.E_bulletFlag) == B2Body_Flags.E_bulletFlag
}
func (body *B2Body) SetAwake(flag bool) {
if flag {
body.M_flags |= B2Body_Flags.E_awakeFlag
body.M_sleepTime = 0.0
} else {
body.M_flags &= ^B2Body_Flags.E_awakeFlag
body.M_sleepTime = 0.0
body.M_linearVelocity.SetZero()
body.M_angularVelocity = 0.0
body.M_force.SetZero()
body.M_torque = 0.0
}
}
func (body B2Body) IsAwake() bool {
return (body.M_flags & B2Body_Flags.E_awakeFlag) == B2Body_Flags.E_awakeFlag
}
func (body B2Body) IsActive() bool {
return (body.M_flags & B2Body_Flags.E_activeFlag) == B2Body_Flags.E_activeFlag
}
func (body B2Body) IsFixedRotation() bool {
return (body.M_flags & B2Body_Flags.E_fixedRotationFlag) == B2Body_Flags.E_fixedRotationFlag
}
func (body *B2Body) SetSleepingAllowed(flag bool) {
if flag {
body.M_flags |= B2Body_Flags.E_autoSleepFlag
} else {
body.M_flags &= ^B2Body_Flags.E_autoSleepFlag
body.SetAwake(true)
}
}
func (body B2Body) IsSleepingAllowed() bool {
return (body.M_flags & B2Body_Flags.E_autoSleepFlag) == B2Body_Flags.E_autoSleepFlag
}
func (body B2Body) GetFixtureList() *B2Fixture {
return body.M_fixtureList
}
func (body B2Body) GetJointList() *B2JointEdge {
return body.M_jointList
}
func (body B2Body) GetContactList() *B2ContactEdge {
return body.M_contactList
}
func (body B2Body) GetNext() *B2Body {
return body.M_next
}
func (body *B2Body) SetUserData(data interface{}) {
body.M_userData = data
}
func (body B2Body) GetUserData() interface{} {
return body.M_userData
}
func (body *B2Body) ApplyForce(force B2Vec2, point B2Vec2, wake bool) {
if body.M_type != B2BodyType.B2_dynamicBody {
return
}
if wake && (body.M_flags&B2Body_Flags.E_awakeFlag) == 0 {
body.SetAwake(true)
}
// Don't accumulate a force if the body is sleeping.
if (body.M_flags & B2Body_Flags.E_awakeFlag) != 0x0000 {
body.M_force.OperatorPlusInplace(force)
body.M_torque += B2Vec2Cross(
B2Vec2Sub(point, body.M_sweep.C),
force,
)
}
}
func (body *B2Body) ApplyForceToCenter(force B2Vec2, wake bool) {
if body.M_type != B2BodyType.B2_dynamicBody {
return
}
if wake && (body.M_flags&B2Body_Flags.E_awakeFlag) == 0 {
body.SetAwake(true)
}
// Don't accumulate a force if the body is sleeping
if (body.M_flags & B2Body_Flags.E_awakeFlag) != 0x0000 {
body.M_force.OperatorPlusInplace(force)
}
}
func (body *B2Body) ApplyTorque(torque float64, wake bool) {
if body.M_type != B2BodyType.B2_dynamicBody {
return
}
if wake && (body.M_flags&B2Body_Flags.E_awakeFlag) == 0 {
body.SetAwake(true)
}
// Don't accumulate a force if the body is sleeping
if (body.M_flags & B2Body_Flags.E_awakeFlag) != 0x0000 {
body.M_torque += torque
}
}
func (body *B2Body) ApplyLinearImpulse(impulse B2Vec2, point B2Vec2, wake bool) {
if body.M_type != B2BodyType.B2_dynamicBody {
return
}
if wake && (body.M_flags&B2Body_Flags.E_awakeFlag) == 0 {
body.SetAwake(true)
}
// Don't accumulate velocity if the body is sleeping
if (body.M_flags & B2Body_Flags.E_awakeFlag) != 0x0000 {
body.M_linearVelocity.OperatorPlusInplace(B2Vec2MulScalar(body.M_invMass, impulse))
body.M_angularVelocity += body.M_invI * B2Vec2Cross(
B2Vec2Sub(point, body.M_sweep.C),
impulse,
)
}
}
func (body *B2Body) ApplyLinearImpulseToCenter(impulse B2Vec2, wake bool) {
if body.M_type != B2BodyType.B2_dynamicBody {
return
}
if wake && (body.M_flags&B2Body_Flags.E_awakeFlag) == 0 {
body.SetAwake(true)
}
// Don't accumulate velocity if the body is sleeping
if (body.M_flags & B2Body_Flags.E_awakeFlag) != 0x0000 {
body.M_linearVelocity.OperatorPlusInplace(B2Vec2MulScalar(body.M_invMass, impulse))
}
}
func (body *B2Body) ApplyAngularImpulse(impulse float64, wake bool) {
if body.M_type != B2BodyType.B2_dynamicBody {
return
}
if wake && (body.M_flags&B2Body_Flags.E_awakeFlag) == 0 {
body.SetAwake(true)
}
// Don't accumulate velocity if the body is sleeping
if (body.M_flags & B2Body_Flags.E_awakeFlag) != 0x0000 {
body.M_angularVelocity += body.M_invI * impulse
}
}
func (body *B2Body) SynchronizeTransform() {
body.M_xf.Q.Set(body.M_sweep.A)
body.M_xf.P = B2Vec2Sub(body.M_sweep.C, B2RotVec2Mul(body.M_xf.Q, body.M_sweep.LocalCenter))
}
func (body *B2Body) Advance(alpha float64) {
// Advance to the new safe time. This doesn't sync the broad-phase.
body.M_sweep.Advance(alpha)
body.M_sweep.C = body.M_sweep.C0
body.M_sweep.A = body.M_sweep.A0
body.M_xf.Q.Set(body.M_sweep.A)
body.M_xf.P = B2Vec2Sub(body.M_sweep.C, B2RotVec2Mul(body.M_xf.Q, body.M_sweep.LocalCenter))
}
func (body B2Body) GetWorld() *B2World {
return body.M_world
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// B2Body.cpp
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
func NewB2Body(bd *B2BodyDef, world *B2World) *B2Body {
B2Assert(bd.Position.IsValid())
B2Assert(bd.LinearVelocity.IsValid())
B2Assert(B2IsValid(bd.Angle))
B2Assert(B2IsValid(bd.AngularVelocity))
B2Assert(B2IsValid(bd.AngularDamping) && bd.AngularDamping >= 0.0)
B2Assert(B2IsValid(bd.LinearDamping) && bd.LinearDamping >= 0.0)
body := &B2Body{}
body.M_flags = 0
if bd.Bullet {
body.M_flags |= B2Body_Flags.E_bulletFlag
}
if bd.FixedRotation {
body.M_flags |= B2Body_Flags.E_fixedRotationFlag
}
if bd.AllowSleep {
body.M_flags |= B2Body_Flags.E_autoSleepFlag
}
if bd.Awake {
body.M_flags |= B2Body_Flags.E_awakeFlag
}
if bd.Active {
body.M_flags |= B2Body_Flags.E_activeFlag
}
body.M_world = world
body.M_xf.P = bd.Position
body.M_xf.Q.Set(bd.Angle)
body.M_sweep.LocalCenter.SetZero()
body.M_sweep.C0 = body.M_xf.P
body.M_sweep.C = body.M_xf.P
body.M_sweep.A0 = bd.Angle
body.M_sweep.A = bd.Angle
body.M_sweep.Alpha0 = 0.0
body.M_jointList = nil
body.M_contactList = nil
body.M_prev = nil
body.M_next = nil
body.M_linearVelocity = bd.LinearVelocity
body.M_angularVelocity = bd.AngularVelocity
body.M_linearDamping = bd.LinearDamping
body.M_angularDamping = bd.AngularDamping
body.M_gravityScale = bd.GravityScale
body.M_force.SetZero()
body.M_torque = 0.0
body.M_sleepTime = 0.0
body.M_type = bd.Type
if body.M_type == B2BodyType.B2_dynamicBody {
body.M_mass = 1.0
body.M_invMass = 1.0
} else {
body.M_mass = 0.0
body.M_invMass = 0.0
}
body.M_I = 0.0
body.M_invI = 0.0
body.M_userData = bd.UserData
body.M_fixtureList = nil
body.M_fixtureCount = 0
return body
}
func (body *B2Body) SetType(bodytype uint8) {
B2Assert(body.M_world.IsLocked() == false)
if body.M_world.IsLocked() == true {
return
}
if body.M_type == bodytype {
return
}
body.M_type = bodytype
body.ResetMassData()
if body.M_type == B2BodyType.B2_staticBody {
body.M_linearVelocity.SetZero()
body.M_angularVelocity = 0.0
body.M_sweep.A0 = body.M_sweep.A
body.M_sweep.C0 = body.M_sweep.C
body.SynchronizeFixtures()
}
body.SetAwake(true)
body.M_force.SetZero()
body.M_torque = 0.0
// Delete the attached contacts.
ce := body.M_contactList
for ce != nil {
ce0 := ce
ce = ce.Next
body.M_world.M_contactManager.Destroy(ce0.Contact)
}
body.M_contactList = nil
// Touch the proxies so that new contacts will be created (when appropriate)
broadPhase := body.M_world.M_contactManager.M_broadPhase
for f := body.M_fixtureList; f != nil; f = f.M_next {
proxyCount := f.M_proxyCount
for i := 0; i < proxyCount; i++ {
broadPhase.TouchProxy(f.M_proxies[i].ProxyId)
}
}
}
func (body *B2Body) CreateFixtureFromDef(def *B2FixtureDef) *B2Fixture {
B2Assert(body.M_world.IsLocked() == false)
if body.M_world.IsLocked() == true {
return nil
}
fixture := NewB2Fixture()
fixture.Create(body, def)
if (body.M_flags & B2Body_Flags.E_activeFlag) != 0x0000 {
broadPhase := &body.M_world.M_contactManager.M_broadPhase
fixture.CreateProxies(broadPhase, body.M_xf)
}
fixture.M_next = body.M_fixtureList
body.M_fixtureList = fixture
body.M_fixtureCount++
fixture.M_body = body
// Adjust mass properties if needed.
if fixture.M_density > 0.0 {
body.ResetMassData()
}
// Let the world know we have a new fixture. This will cause new contacts
// to be created at the beginning of the next time step.
body.M_world.M_flags |= B2World_Flags.E_newFixture
return fixture
}
func (body *B2Body) CreateFixture(shape B2ShapeInterface, density float64) *B2Fixture {
def := MakeB2FixtureDef()
def.Shape = shape
def.Density = density
return body.CreateFixtureFromDef(&def)
}
func (body *B2Body) DestroyFixture(fixture *B2Fixture) {
if fixture == nil {
return
}
B2Assert(body.M_world.IsLocked() == false)
if body.M_world.IsLocked() == true {
return
}
B2Assert(fixture.M_body == body)
// Remove the fixture from this body's singly linked list.
B2Assert(body.M_fixtureCount > 0)
node := &body.M_fixtureList
found := false
for *node != nil {
if *node == fixture {
*node = fixture.M_next
found = true
break
}
node = &(*node).M_next
}
// You tried to remove a shape that is not attached to this body.
B2Assert(found)
// Destroy any contacts associated with the fixture.
edge := body.M_contactList
for edge != nil {
c := edge.Contact
edge = edge.Next
fixtureA := c.GetFixtureA()
fixtureB := c.GetFixtureB()
if fixture == fixtureA || fixture == fixtureB {
// This destroys the contact and removes it from
// this body's contact list.
body.M_world.M_contactManager.Destroy(c)
}
}
if (body.M_flags & B2Body_Flags.E_activeFlag) != 0x0000 {
broadPhase := &body.M_world.M_contactManager.M_broadPhase
fixture.DestroyProxies(broadPhase)
}
fixture.M_body = nil
fixture.M_next = nil
fixture.Destroy()
body.M_fixtureCount--
// Reset the mass data.
body.ResetMassData()
}
func (body *B2Body) ResetMassData() {
// Compute mass data from shapes. Each shape has its own density.
body.M_mass = 0.0
body.M_invMass = 0.0
body.M_I = 0.0
body.M_invI = 0.0
body.M_sweep.LocalCenter.SetZero()
// Static and kinematic bodies have zero mass.
if body.M_type == B2BodyType.B2_staticBody || body.M_type == B2BodyType.B2_kinematicBody {
body.M_sweep.C0 = body.M_xf.P
body.M_sweep.C = body.M_xf.P
body.M_sweep.A0 = body.M_sweep.A
return
}
B2Assert(body.M_type == B2BodyType.B2_dynamicBody)
// Accumulate mass over all fixtures.
localCenter := MakeB2Vec2(0, 0)
for f := body.M_fixtureList; f != nil; f = f.M_next {
if f.M_density == 0.0 {
continue
}
massData := NewMassData()
f.GetMassData(massData)
body.M_mass += massData.Mass
localCenter.OperatorPlusInplace(B2Vec2MulScalar(massData.Mass, massData.Center))
body.M_I += massData.I
}
// Compute center of mass.
if body.M_mass > 0.0 {
body.M_invMass = 1.0 / body.M_mass
localCenter.OperatorScalarMulInplace(body.M_invMass)
} else {
// Force all dynamic bodies to have a positive mass.
body.M_mass = 1.0
body.M_invMass = 1.0
}
if body.M_I > 0.0 && (body.M_flags&B2Body_Flags.E_fixedRotationFlag) == 0 {
// Center the inertia about the center of mass.
body.M_I -= body.M_mass * B2Vec2Dot(localCenter, localCenter)
B2Assert(body.M_I > 0.0)
body.M_invI = 1.0 / body.M_I
} else {
body.M_I = 0.0
body.M_invI = 0.0
}
// Move center of mass.
oldCenter := body.M_sweep.C
body.M_sweep.LocalCenter = localCenter
body.M_sweep.C0 = B2TransformVec2Mul(body.M_xf, body.M_sweep.LocalCenter)
body.M_sweep.C = B2TransformVec2Mul(body.M_xf, body.M_sweep.LocalCenter)
// Update center of mass velocity.
body.M_linearVelocity.OperatorPlusInplace(B2Vec2CrossScalarVector(
body.M_angularVelocity,
B2Vec2Sub(body.M_sweep.C, oldCenter),
))
}
func (body *B2Body) SetMassData(massData *B2MassData) {
B2Assert(body.M_world.IsLocked() == false)
if body.M_world.IsLocked() == true {
return
}
if body.M_type != B2BodyType.B2_dynamicBody {
return
}
body.M_invMass = 0.0
body.M_I = 0.0
body.M_invI = 0.0
body.M_mass = massData.Mass
if body.M_mass <= 0.0 {
body.M_mass = 1.0
}
body.M_invMass = 1.0 / body.M_mass
if massData.I > 0.0 && (body.M_flags&B2Body_Flags.E_fixedRotationFlag) == 0 {
body.M_I = massData.I - body.M_mass*B2Vec2Dot(massData.Center, massData.Center)
B2Assert(body.M_I > 0.0)
body.M_invI = 1.0 / body.M_I
}
// Move center of mass.
oldCenter := body.M_sweep.C
body.M_sweep.LocalCenter = massData.Center
body.M_sweep.C0 = B2TransformVec2Mul(body.M_xf, body.M_sweep.LocalCenter)
body.M_sweep.C = B2TransformVec2Mul(body.M_xf, body.M_sweep.LocalCenter)
// Update center of mass velocity.
body.M_linearVelocity.OperatorPlusInplace(
B2Vec2CrossScalarVector(
body.M_angularVelocity,
B2Vec2Sub(body.M_sweep.C, oldCenter),
),
)
}
func (body B2Body) ShouldCollide(other *B2Body) bool {
// At least one body should be dynamic.
if body.M_type != B2BodyType.B2_dynamicBody && other.M_type != B2BodyType.B2_dynamicBody {
return false
}
// Does a joint prevent collision?
for jn := body.M_jointList; jn != nil; jn = jn.Next {
if jn.Other == other {
if jn.Joint.IsCollideConnected() == false {
return false
}
}
}
return true
}
func (body *B2Body) SetTransform(position B2Vec2, angle float64) {
B2Assert(body.M_world.IsLocked() == false)
if body.M_world.IsLocked() == true {
return
}
body.M_xf.Q.Set(angle)
body.M_xf.P = position
body.M_sweep.C = B2TransformVec2Mul(body.M_xf, body.M_sweep.LocalCenter)
body.M_sweep.A = angle
body.M_sweep.C0 = body.M_sweep.C
body.M_sweep.A0 = angle
broadPhase := &body.M_world.M_contactManager.M_broadPhase
for f := body.M_fixtureList; f != nil; f = f.M_next {
f.Synchronize(broadPhase, body.M_xf, body.M_xf)
}
}
func (body *B2Body) SynchronizeFixtures() {
xf1 := MakeB2Transform()
xf1.Q.Set(body.M_sweep.A0)
xf1.P = B2Vec2Sub(body.M_sweep.C0, B2RotVec2Mul(xf1.Q, body.M_sweep.LocalCenter))
broadPhase := &body.M_world.M_contactManager.M_broadPhase
for f := body.M_fixtureList; f != nil; f = f.M_next {
f.Synchronize(broadPhase, xf1, body.M_xf)
}
}
func (body *B2Body) SetActive(flag bool) {
B2Assert(body.M_world.IsLocked() == false)
if flag == body.IsActive() {
return
}
if flag {
body.M_flags |= B2Body_Flags.E_activeFlag
// Create all proxies.
broadPhase := &body.M_world.M_contactManager.M_broadPhase
for f := body.M_fixtureList; f != nil; f = f.M_next {
f.CreateProxies(broadPhase, body.M_xf)
}
// Contacts are created the next time step.
} else {
body.M_flags &= ^B2Body_Flags.E_activeFlag
// Destroy all proxies.
broadPhase := &body.M_world.M_contactManager.M_broadPhase
for f := body.M_fixtureList; f != nil; f = f.M_next {
f.DestroyProxies(broadPhase)
}
// Destroy the attached contacts.
ce := body.M_contactList
for ce != nil {
ce0 := ce
ce = ce.Next
body.M_world.M_contactManager.Destroy(ce0.Contact)
}
body.M_contactList = nil
}
}
func (body *B2Body) SetFixedRotation(flag bool) {
status := (body.M_flags & B2Body_Flags.E_fixedRotationFlag) == B2Body_Flags.E_fixedRotationFlag
if status == flag {
return
}
if flag {
body.M_flags |= B2Body_Flags.E_fixedRotationFlag
} else {
body.M_flags &= ^B2Body_Flags.E_fixedRotationFlag
}
body.M_angularVelocity = 0.0
body.ResetMassData()
}
func (body *B2Body) Dump() {
bodyIndex := body.M_islandIndex
fmt.Print("{\n")
fmt.Print(" b2BodyDef bd;\n")
fmt.Print(fmt.Printf(" bd.type = b2BodyType(%d);\n", body.M_type))
fmt.Print(fmt.Printf(" bd.position.Set(%.15f, %.15f);\n", body.M_xf.P.X, body.M_xf.P.Y))
fmt.Print(fmt.Printf(" bd.angle = %.15f;\n", body.M_sweep.A))
fmt.Print(fmt.Printf(" bd.linearVelocity.Set(%.15f, %.15f);\n", body.M_linearVelocity.X, body.M_linearVelocity.Y))
fmt.Print(fmt.Printf(" bd.angularVelocity = %.15f;\n", body.M_angularVelocity))
fmt.Print(fmt.Printf(" bd.linearDamping = %.15f;\n", body.M_linearDamping))
fmt.Print(fmt.Printf(" bd.angularDamping = %.15f;\n", body.M_angularDamping))
fmt.Print(fmt.Printf(" bd.allowSleep = bool(%d);\n", body.M_flags&B2Body_Flags.E_autoSleepFlag))
fmt.Print(fmt.Printf(" bd.awake = bool(%d);\n", body.M_flags&B2Body_Flags.E_awakeFlag))
fmt.Print(fmt.Printf(" bd.fixedRotation = bool(%d);\n", body.M_flags&B2Body_Flags.E_fixedRotationFlag))
fmt.Print(fmt.Printf(" bd.bullet = bool(%d);\n", body.M_flags&B2Body_Flags.E_bulletFlag))
fmt.Print(fmt.Printf(" bd.active = bool(%d);\n", body.M_flags&B2Body_Flags.E_activeFlag))
fmt.Print(fmt.Printf(" bd.gravityScale = %.15f;\n", body.M_gravityScale))
fmt.Print(fmt.Printf(" bodies[%d] = body.M_world.CreateBody(&bd);\n", body.M_islandIndex))
fmt.Print("\n")
for f := body.M_fixtureList; f != nil; f = f.M_next {
fmt.Print(" {\n")
f.Dump(bodyIndex)
fmt.Print(" }\n")
}
fmt.Print("}\n")
}