Browse Source

Soft body: Implement allow sleeping flag (#665)

Jorrit Rouwe 2 years ago
parent
commit
153291b7c6

+ 1 - 1
Jolt/Physics/Body/BodyManager.cpp

@@ -256,7 +256,7 @@ Body *BodyManager::AllocateSoftBody(const SoftBodyCreationSettings &inSoftBodyCr
 	mp->SetAngularVelocity(Vec3::sZero());
 	mp->SetGravityFactor(inSoftBodyCreationSettings.mGravityFactor);
 	mp->mMotionQuality = EMotionQuality::Discrete;
-	mp->mAllowSleeping = false;
+	mp->mAllowSleeping = inSoftBodyCreationSettings.mAllowSleeping;
 	JPH_IF_ENABLE_ASSERTS(mp->mCachedBodyType = body->mBodyType;)
 	JPH_IF_ENABLE_ASSERTS(mp->mCachedMotionType = body->mMotionType;)
 	mp->Initialize(inSoftBodyCreationSettings);

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

@@ -36,6 +36,9 @@ public:
 	/// Get the allowed degrees of freedom that this body has (this can be changed by calling SetMassProperties)
 	inline EAllowedDOFs		GetAllowedDOFs() const											{ return mAllowedDOFs; }
 
+	/// If this body can go to sleep.
+	inline bool				GetAllowSleeping() const										{ return mAllowSleeping; }
+
 	/// Get world space linear velocity of the center of mass
 	inline Vec3				GetLinearVelocity() const										{ JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::Read)); return mLinearVelocity; }
 

+ 3 - 0
Jolt/Physics/SoftBody/SoftBodyCreationSettings.cpp

@@ -28,6 +28,7 @@ JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL(SoftBodyCreationSettings)
 	JPH_ADD_ATTRIBUTE(SoftBodyCreationSettings, mGravityFactor)
 	JPH_ADD_ATTRIBUTE(SoftBodyCreationSettings, mUpdatePosition)
 	JPH_ADD_ATTRIBUTE(SoftBodyCreationSettings, mMakeRotationIdentity)
+	JPH_ADD_ATTRIBUTE(SoftBodyCreationSettings, mAllowSleeping)
 }
 
 void SoftBodyCreationSettings::SaveBinaryState(StreamOut &inStream) const
@@ -46,6 +47,7 @@ void SoftBodyCreationSettings::SaveBinaryState(StreamOut &inStream) const
 	inStream.Write(mGravityFactor);
 	inStream.Write(mUpdatePosition);
 	inStream.Write(mMakeRotationIdentity);
+	inStream.Write(mAllowSleeping);
 }
 
 void SoftBodyCreationSettings::RestoreBinaryState(StreamIn &inStream)
@@ -64,6 +66,7 @@ void SoftBodyCreationSettings::RestoreBinaryState(StreamIn &inStream)
 	inStream.Read(mGravityFactor);
 	inStream.Read(mUpdatePosition);
 	inStream.Read(mMakeRotationIdentity);
+	inStream.Read(mAllowSleeping);
 }
 
 void SoftBodyCreationSettings::SaveWithChildren(StreamOut &inStream, SharedSettingsToIDMap *ioSharedSettingsMap, MaterialToIDMap *ioMaterialMap, GroupFilterToIDMap *ioGroupFilterMap) const

+ 1 - 0
Jolt/Physics/SoftBody/SoftBodyCreationSettings.h

@@ -67,6 +67,7 @@ public:
 	float				mGravityFactor = 1.0f;				///< Value to multiply gravity with for this body
 	bool				mUpdatePosition = true;				///< Update the position of the body while simulating (set to false for something that is attached to the static world)
 	bool				mMakeRotationIdentity = true;		///< Bake specified mRotation in the vertices and set the body rotation to identity (simulation is slightly more accurate if the rotation of a soft body is kept to identity)
+	bool				mAllowSleeping = true;				///< If this body can go to sleep or not
 };
 
 JPH_NAMESPACE_END

+ 3 - 0
Jolt/Physics/SoftBody/SoftBodyMotionProperties.cpp

@@ -475,6 +475,9 @@ ECanSleep SoftBodyMotionProperties::Update(float inDeltaTime, Body &inSoftBody,
 			body_interface.SetLinearAndAngularVelocity(cs.mBodyID, body_transform.Multiply3x3(cs.mLinearVelocity), body_transform.Multiply3x3(cs.mAngularVelocity));
 
 	// Test if we should go to sleep
+	if (!GetAllowSleeping())
+		return ECanSleep::CannotSleep;
+
 	const PhysicsSettings &physics_settings = inSystem.GetPhysicsSettings();
 	if (max_v_sq > physics_settings.mPointVelocitySleepThreshold)
 	{