MCPcopy Create free account
hub / github.com/ByteArena/box2d / ResetMassData

Method ResetMassData

DynamicsB2Body.go:714–779  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

712}
713
714func (body *B2Body) ResetMassData() {
715
716 // Compute mass data from shapes. Each shape has its own density.
717 body.M_mass = 0.0
718 body.M_invMass = 0.0
719 body.M_I = 0.0
720 body.M_invI = 0.0
721 body.M_sweep.LocalCenter.SetZero()
722
723 // Static and kinematic bodies have zero mass.
724 if body.M_type == B2BodyType.B2_staticBody || body.M_type == B2BodyType.B2_kinematicBody {
725 body.M_sweep.C0 = body.M_xf.P
726 body.M_sweep.C = body.M_xf.P
727 body.M_sweep.A0 = body.M_sweep.A
728 return
729 }
730
731 B2Assert(body.M_type == B2BodyType.B2_dynamicBody)
732
733 // Accumulate mass over all fixtures.
734 localCenter := MakeB2Vec2(0, 0)
735 for f := body.M_fixtureList; f != nil; f = f.M_next {
736 if f.M_density == 0.0 {
737 continue
738 }
739
740 massData := NewMassData()
741 f.GetMassData(massData)
742 body.M_mass += massData.Mass
743 localCenter.OperatorPlusInplace(B2Vec2MulScalar(massData.Mass, massData.Center))
744 body.M_I += massData.I
745 }
746
747 // Compute center of mass.
748 if body.M_mass > 0.0 {
749 body.M_invMass = 1.0 / body.M_mass
750 localCenter.OperatorScalarMulInplace(body.M_invMass)
751 } else {
752 // Force all dynamic bodies to have a positive mass.
753 body.M_mass = 1.0
754 body.M_invMass = 1.0
755 }
756
757 if body.M_I > 0.0 && (body.M_flags&B2Body_Flags.E_fixedRotationFlag) == 0 {
758 // Center the inertia about the center of mass.
759 body.M_I -= body.M_mass * B2Vec2Dot(localCenter, localCenter)
760 B2Assert(body.M_I > 0.0)
761 body.M_invI = 1.0 / body.M_I
762
763 } else {
764 body.M_I = 0.0
765 body.M_invI = 0.0
766 }
767
768 // Move center of mass.
769 oldCenter := body.M_sweep.C
770 body.M_sweep.LocalCenter = localCenter
771 body.M_sweep.C0 = B2TransformVec2Mul(body.M_xf, body.M_sweep.LocalCenter)

Callers 4

SetTypeMethod · 0.95
CreateFixtureFromDefMethod · 0.95
DestroyFixtureMethod · 0.95
SetFixedRotationMethod · 0.95

Calls 12

B2AssertFunction · 0.85
MakeB2Vec2Function · 0.85
NewMassDataFunction · 0.85
B2Vec2MulScalarFunction · 0.85
B2Vec2DotFunction · 0.85
B2TransformVec2MulFunction · 0.85
B2Vec2CrossScalarVectorFunction · 0.85
B2Vec2SubFunction · 0.85
SetZeroMethod · 0.45
GetMassDataMethod · 0.45
OperatorPlusInplaceMethod · 0.45

Tested by

no test coverage detected