Pārlūkot izejas kodu

Moved soft body vertex radius from SoftBodySharedSettings to SoftBodyCreationSettings (#1649)

See godotengine/godot#106697
Jorrit Rouwe 3 mēneši atpakaļ
vecāks
revīzija
f3d906f8c0

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

@@ -415,6 +415,7 @@ SoftBodyCreationSettings Body::GetSoftBodyCreationSettings() const
 	result.mGravityFactor = mp->GetGravityFactor();
 	result.mPressure = mp->GetPressure();
 	result.mUpdatePosition = mp->GetUpdatePosition();
+	result.mVertexRadius = mp->GetVertexRadius();
 	result.mSettings = mp->GetSettings();
 
 	return result;

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

@@ -26,6 +26,7 @@ JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL(SoftBodyCreationSettings)
 	JPH_ADD_ATTRIBUTE(SoftBodyCreationSettings, mFriction)
 	JPH_ADD_ATTRIBUTE(SoftBodyCreationSettings, mPressure)
 	JPH_ADD_ATTRIBUTE(SoftBodyCreationSettings, mGravityFactor)
+	JPH_ADD_ATTRIBUTE(SoftBodyCreationSettings, mVertexRadius)
 	JPH_ADD_ATTRIBUTE(SoftBodyCreationSettings, mUpdatePosition)
 	JPH_ADD_ATTRIBUTE(SoftBodyCreationSettings, mMakeRotationIdentity)
 	JPH_ADD_ATTRIBUTE(SoftBodyCreationSettings, mAllowSleeping)
@@ -45,6 +46,7 @@ void SoftBodyCreationSettings::SaveBinaryState(StreamOut &inStream) const
 	inStream.Write(mFriction);
 	inStream.Write(mPressure);
 	inStream.Write(mGravityFactor);
+	inStream.Write(mVertexRadius);
 	inStream.Write(mUpdatePosition);
 	inStream.Write(mMakeRotationIdentity);
 	inStream.Write(mAllowSleeping);
@@ -64,6 +66,7 @@ void SoftBodyCreationSettings::RestoreBinaryState(StreamIn &inStream)
 	inStream.Read(mFriction);
 	inStream.Read(mPressure);
 	inStream.Read(mGravityFactor);
+	inStream.Read(mVertexRadius);
 	inStream.Read(mUpdatePosition);
 	inStream.Read(mMakeRotationIdentity);
 	inStream.Read(mAllowSleeping);

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

@@ -65,6 +65,7 @@ public:
 	float				mFriction = 0.2f;					///< Friction coefficient when colliding
 	float				mPressure = 0.0f;					///< n * R * T, amount of substance * ideal gas constant * absolute temperature, see https://en.wikipedia.org/wiki/Pressure
 	float				mGravityFactor = 1.0f;				///< Value to multiply gravity with for this body
+	float				mVertexRadius = 0.0f;				///< How big the particles are, can be used to push the vertices a little bit away from the surface of other bodies to prevent z-fighting
 	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

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

@@ -63,6 +63,7 @@ void SoftBodyMotionProperties::Initialize(const SoftBodyCreationSettings &inSett
 	mNumIterations = inSettings.mNumIterations;
 	mPressure = inSettings.mPressure;
 	mUpdatePosition = inSettings.mUpdatePosition;
+	SetVertexRadius(inSettings.mVertexRadius);
 
 	// Initialize vertices
 	mVertices.resize(inSettings.mSettings->mVertices.size());
@@ -236,7 +237,7 @@ void SoftBodyMotionProperties::DetermineCollidingShapes(const SoftBodyUpdateCont
 	// Calculate local bounding box
 	AABox local_bounds = mLocalBounds;
 	local_bounds.Encapsulate(mLocalPredictedBounds);
-	local_bounds.ExpandBy(Vec3::sReplicate(mSettings->mVertexRadius));
+	local_bounds.ExpandBy(Vec3::sReplicate(mVertexRadius));
 
 	// Calculate world space bounding box
 	AABox world_bounds = local_bounds.Transformed(inContext.mCenterOfMassTransform);
@@ -702,7 +703,7 @@ void SoftBodyMotionProperties::ApplyCollisionConstraintsAndUpdateVelocities(cons
 
 	float dt = inContext.mSubStepDeltaTime;
 	float restitution_threshold = -2.0f * inContext.mGravity.Length() * dt;
-	float vertex_radius = mSettings->mVertexRadius;
+	float vertex_radius = mVertexRadius;
 	for (Vertex &v : mVertices)
 		if (v.mInvMass > 0.0f)
 		{

+ 5 - 0
Jolt/Physics/SoftBody/SoftBodyMotionProperties.h

@@ -93,6 +93,10 @@ public:
 	float								GetSkinnedMaxDistanceMultiplier() const		{ return mSkinnedMaxDistanceMultiplier; }
 	void								SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier) { mSkinnedMaxDistanceMultiplier = inSkinnedMaxDistanceMultiplier; }
 
+	/// How big the particles are, can be used to push the vertices a little bit away from the surface of other bodies to prevent z-fighting
+	float								GetVertexRadius() const						{ return mVertexRadius; }
+	void								SetVertexRadius(float inVertexRadius)		{ JPH_ASSERT(mVertexRadius >= 0.0f); mVertexRadius = inVertexRadius; }
+
 	/// Get local bounding box
 	const AABox &						GetLocalBounds() const						{ return mLocalBounds; }
 
@@ -314,6 +318,7 @@ private:
 	uint								mNumSensors;								///< Workaround for TSAN false positive: store mCollidingSensors.size() in a separate variable.
 	float								mPressure;									///< n * R * T, amount of substance * ideal gas constant * absolute temperature, see https://en.wikipedia.org/wiki/Pressure
 	float								mSkinnedMaxDistanceMultiplier = 1.0f;		///< Multiplier applied to Skinned::mMaxDistance to allow tightening or loosening of the skin constraints
+	float								mVertexRadius = 0.0f;						///< How big the particles are, can be used to push the vertices a little bit away from the surface of other bodies to prevent z-fighting
 	bool								mUpdatePosition;							///< Update the position of the body while simulating (set to false for something that is attached to the static world)
 	atomic<bool>						mNeedContactCallback = false;				///< True if the soft body has collided with anything in the last update
 	bool								mEnableSkinConstraints = true;				///< If skin constraints are enabled

+ 0 - 4
Jolt/Physics/SoftBody/SoftBodySharedSettings.cpp

@@ -106,7 +106,6 @@ JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL(SoftBodySharedSettings)
 	JPH_ADD_ATTRIBUTE(SoftBodySharedSettings, mRodStretchShearConstraints)
 	JPH_ADD_ATTRIBUTE(SoftBodySharedSettings, mRodBendTwistConstraints)
 	JPH_ADD_ATTRIBUTE(SoftBodySharedSettings, mMaterials)
-	JPH_ADD_ATTRIBUTE(SoftBodySharedSettings, mVertexRadius)
 }
 
 void SoftBodySharedSettings::CalculateClosestKinematic()
@@ -1199,7 +1198,6 @@ Ref<SoftBodySharedSettings> SoftBodySharedSettings::Clone() const
 	clone->mRodStretchShearConstraints = mRodStretchShearConstraints;
 	clone->mRodBendTwistConstraints = mRodBendTwistConstraints;
 	clone->mMaterials = mMaterials;
-	clone->mVertexRadius = mVertexRadius;
 	clone->mUpdateGroups = mUpdateGroups;
 	return clone;
 }
@@ -1214,7 +1212,6 @@ void SoftBodySharedSettings::SaveBinaryState(StreamOut &inStream) const
 	inStream.Write(mSkinnedConstraints);
 	inStream.Write(mSkinnedConstraintNormals);
 	inStream.Write(mLRAConstraints);
-	inStream.Write(mVertexRadius);
 	inStream.Write(mUpdateGroups);
 
 	// Can't write mRodStretchShearConstraints directly because the class contains padding
@@ -1250,7 +1247,6 @@ void SoftBodySharedSettings::RestoreBinaryState(StreamIn &inStream)
 	inStream.Read(mSkinnedConstraints);
 	inStream.Read(mSkinnedConstraintNormals);
 	inStream.Read(mLRAConstraints);
-	inStream.Read(mVertexRadius);
 	inStream.Read(mUpdateGroups);
 
 	inStream.Read(mRodStretchShearConstraints, [](StreamIn &inS, RodStretchShear &outElement) {

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

@@ -355,7 +355,6 @@ public:
 	Array<RodStretchShear>	mRodStretchShearConstraints;			///< The list of Cosserat rod constraints that connect two vertices and that limit stretch and shear
 	Array<RodBendTwist>	mRodBendTwistConstraints;					///< The list of Cosserat rod constraints that connect two rods and limit the bend and twist
 	PhysicsMaterialList mMaterials { PhysicsMaterial::sDefault };	///< The materials of the faces of the body, referenced by Face::mMaterialIndex
-	float				mVertexRadius = 0.0f;						///< How big the particles are, can be used to push the vertices a little bit away from the surface of other bodies to prevent z-fighting
 
 private:
 	friend class SoftBodyMotionProperties;

+ 2 - 2
Samples/Tests/SoftBody/SoftBodyVertexRadiusTest.cpp

@@ -27,12 +27,12 @@ void SoftBodyVertexRadiusTest::Initialize()
 
 	// Create cloth with specified vertex radius
 	mSharedSettings = SoftBodyCreator::CreateCloth(30, 30, 0.5f);
-	mSharedSettings->mVertexRadius = sVertexRadius;
 	SoftBodyCreationSettings cloth(mSharedSettings, RVec3(0, 5, 0), Quat::sRotation(Vec3::sAxisY(), 0.25f * JPH_PI), Layers::MOVING);
+	cloth.mVertexRadius = sVertexRadius;
 	mBodyInterface->CreateAndAddSoftBody(cloth, EActivation::Activate);
 }
 
 void SoftBodyVertexRadiusTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
 {
-	inUI->CreateSlider(inSubMenu, "Vertex Radius", sVertexRadius, 0.0f, 0.5f, 0.01f, [this](float inValue) { sVertexRadius = inValue; mSharedSettings->mVertexRadius = inValue; });
+	inUI->CreateSlider(inSubMenu, "Vertex Radius", sVertexRadius, 0.0f, 0.5f, 0.01f, [](float inValue) { sVertexRadius = inValue; });
 }