From d945a031c1b97fd7fd7a9d11ae8e75a2a9ceb83c Mon Sep 17 00:00:00 2001 From: greerdv Date: Wed, 10 Nov 2021 19:30:12 +0000 Subject: [PATCH] prevent twist limits being created with equal lower and upper limits Signed-off-by: greerdv --- .../Code/Source/Joint/PhysXJointUtils.cpp | 18 ++++++++++++++++-- Gems/PhysX/Code/Source/Joint/PhysXJointUtils.h | 4 +++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Gems/PhysX/Code/Source/Joint/PhysXJointUtils.cpp b/Gems/PhysX/Code/Source/Joint/PhysXJointUtils.cpp index 3558c9fb56..e69fb70fb2 100644 --- a/Gems/PhysX/Code/Source/Joint/PhysXJointUtils.cpp +++ b/Gems/PhysX/Code/Source/Joint/PhysXJointUtils.cpp @@ -224,8 +224,22 @@ namespace PhysX { physx::PxJointLimitCone limitCone(swingLimitY, swingLimitZ); joint->setSwingLimit(limitCone); - const float twistLower = AZ::DegToRad(AZStd::GetMin(configuration.m_twistLimitLower, configuration.m_twistLimitUpper)); - const float twistUpper = AZ::DegToRad(AZStd::GetMax(configuration.m_twistLimitLower, configuration.m_twistLimitUpper)); + float twistLower = AZ::DegToRad(AZStd::GetMin(configuration.m_twistLimitLower, configuration.m_twistLimitUpper)); + float twistUpper = AZ::DegToRad(AZStd::GetMax(configuration.m_twistLimitLower, configuration.m_twistLimitUpper)); + // make sure there is at least a small difference between the lower and upper limits to avoid problems in PhysX + const float minSwingLimitRangeRadians = AZ::DegToRad(JointConstants::MinTwistLimitRangeDegrees); + if (const float twistLimitRange = twistUpper - twistLower; + twistLimitRange < minSwingLimitRangeRadians) + { + if (twistUpper > 0.0f) + { + twistLower -= (minSwingLimitRangeRadians - twistLimitRange); + } + else + { + twistUpper += (minSwingLimitRangeRadians - twistLimitRange); + } + } physx::PxJointAngularLimitPair twistLimitPair(twistLower, twistUpper); joint->setTwistLimit(twistLimitPair); diff --git a/Gems/PhysX/Code/Source/Joint/PhysXJointUtils.h b/Gems/PhysX/Code/Source/Joint/PhysXJointUtils.h index 7a30473dac..a99eb7aacb 100644 --- a/Gems/PhysX/Code/Source/Joint/PhysXJointUtils.h +++ b/Gems/PhysX/Code/Source/Joint/PhysXJointUtils.h @@ -18,9 +18,11 @@ namespace PhysX { namespace JointConstants { - // Setting swing limits to very small values can cause extreme stability problems, so clamp above a small + // Setting joint limits to very small values can cause extreme stability problems, so clamp above a small // threshold. static const float MinSwingLimitDegrees = 1.0f; + // Minimum range between lower and upper twist limits. + static const float MinTwistLimitRangeDegrees = 1.0f; } // namespace JointConstants namespace Utils