浏览代码

Fixed an assert when trying to get the body creation settings of a body that was created as dynamic and then turned into a static object

Jorrit Rouwe 3 年之前
父节点
当前提交
cb2bc4f67d
共有 3 个文件被更改,包括 14 次插入6 次删除
  1. 2 2
      Jolt/Physics/Body/Body.cpp
  2. 4 0
      Jolt/Physics/Body/MotionProperties.h
  3. 8 4
      Jolt/Physics/Body/MotionProperties.inl

+ 2 - 2
Jolt/Physics/Body/Body.cpp

@@ -319,8 +319,8 @@ BodyCreationSettings Body::GetBodyCreationSettings() const
 	result.mMaxAngularVelocity = mMotionProperties != nullptr? mMotionProperties->GetMaxAngularVelocity() : 0.0f;
 	result.mGravityFactor = mMotionProperties != nullptr? mMotionProperties->GetGravityFactor() : 1.0f;
 	result.mOverrideMassProperties = EOverrideMassProperties::MassAndInertiaProvided;
-	result.mMassPropertiesOverride.mMass = mMotionProperties != nullptr? 1.0f / mMotionProperties->GetInverseMass() : FLT_MAX;
-	result.mMassPropertiesOverride.mInertia = mMotionProperties != nullptr? mMotionProperties->GetLocalSpaceInverseInertia().Inversed3x3() : Mat44::sIdentity();
+	result.mMassPropertiesOverride.mMass = mMotionProperties != nullptr? 1.0f / mMotionProperties->GetInverseMassUnchecked() : FLT_MAX;
+	result.mMassPropertiesOverride.mInertia = mMotionProperties != nullptr? mMotionProperties->GetLocalSpaceInverseInertiaUnchecked().Inversed3x3() : Mat44::sIdentity();
 	result.SetShape(GetShape());
 
 	return result;

+ 4 - 0
Jolt/Physics/Body/MotionProperties.h

@@ -75,6 +75,7 @@ public:
 
 	/// Get inverse mass (1 / mass). Should only be called on a dynamic object (static or kinematic bodies have infinite mass so should be treated as 1 / mass = 0)
 	inline float			GetInverseMass() const											{ JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic); return mInvMass; }
+	inline float			GetInverseMassUnchecked() const									{ return mInvMass; }
 	void					SetInverseMass(float inInverseMass)								{ mInvMass = inInverseMass; }
 
 	/// Diagonal of inverse inertia matrix: D. Should only be called on a dynamic object (static or kinematic bodies have infinite mass so should be treated as D = 0)
@@ -89,6 +90,9 @@ public:
 	/// Get inverse inertia matrix (\f$I_{body}^{-1}\f$). Will be a matrix of zeros for a static or kinematic object.
 	inline Mat44 			GetLocalSpaceInverseInertia() const;
 
+	/// Same as GetLocalSpaceInverseInertia() but doesn't check if the body is dynamic
+	inline Mat44 			GetLocalSpaceInverseInertiaUnchecked() const;
+
 	/// Get inverse inertia matrix (\f$I^{-1}\f$) for a given object rotation (translation will be ignored). Zero if object is static or kinematic.
 	inline Mat44			GetInverseInertiaForRotation(Mat44Arg inRotation) const;
 

+ 8 - 4
Jolt/Physics/Body/MotionProperties.inl

@@ -64,16 +64,20 @@ void MotionProperties::ClampAngularVelocity()
 		mAngularVelocity *= mMaxAngularVelocity / sqrt(len_sq); 
 }
 
-inline Mat44 MotionProperties::GetLocalSpaceInverseInertia() const
+inline Mat44 MotionProperties::GetLocalSpaceInverseInertiaUnchecked() const
 { 
-	JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic);
-
 	Mat44 rotation = Mat44::sRotation(mInertiaRotation);
 	Mat44 rotation_mul_scale_transposed(mInvInertiaDiagonal.SplatX() * rotation.GetColumn4(0), mInvInertiaDiagonal.SplatY() * rotation.GetColumn4(1), mInvInertiaDiagonal.SplatZ() * rotation.GetColumn4(2), Vec4(0, 0, 0, 1));
 	return rotation.Multiply3x3RightTransposed(rotation_mul_scale_transposed);
 }
 
-Mat44 MotionProperties::GetInverseInertiaForRotation(Mat44Arg inRotation) const		
+inline Mat44 MotionProperties::GetLocalSpaceInverseInertia() const
+{
+	JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic);
+	return GetLocalSpaceInverseInertiaUnchecked();
+}
+
+Mat44 MotionProperties::GetInverseInertiaForRotation(Mat44Arg inRotation) const
 { 
 	JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic);