Skip to content

Commit

Permalink
Fix Issue : Crashes when updating the constraint limits using charact…
Browse files Browse the repository at this point in the history
…ers with multiple LODs #87

Added an Enable flag to each Limits to skip processing when DrivingBone is disabled due to LOD etc.
  • Loading branch information
[email protected] authored and [email protected] committed May 27, 2023
1 parent aa27aab commit aff5328
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ void FAnimNode_KawaiiPhysics::UpdateSphericalLimits(TArray<FSphericalLimit>& Lim
for (auto& Sphere : Limits)
{
SCOPE_CYCLE_COUNTER(STAT_KawaiiPhysics_UpdateSphericalLimit);

if (Sphere.DrivingBone.BoneIndex >= 0)
if (Sphere.DrivingBone.IsValidToEvaluate(BoneContainer))
{
const FCompactPoseBoneIndex CompactPoseIndex = Sphere.DrivingBone.GetCompactPoseIndex(BoneContainer);
FTransform BoneTransform = Output.Pose.GetComponentSpaceTransform(CompactPoseIndex);
Expand All @@ -441,10 +441,12 @@ void FAnimNode_KawaiiPhysics::UpdateSphericalLimits(TArray<FSphericalLimit>& Lim
FAnimationRuntime::ConvertBoneSpaceTransformToCS(ComponentTransform, Output.Pose, BoneTransform, CompactPoseIndex, BCS_BoneSpace);
Sphere.Location = BoneTransform.GetLocation();
Sphere.Rotation = BoneTransform.GetRotation();

Sphere.bEnable = true;
}
else
{
Sphere.Location = Sphere.OffsetLocation;
Sphere.bEnable = false;
}
}
}
Expand All @@ -457,7 +459,7 @@ void FAnimNode_KawaiiPhysics::UpdateCapsuleLimits(TArray<FCapsuleLimit>& Limits,
{
SCOPE_CYCLE_COUNTER(STAT_KawaiiPhysics_UpdateCapsuleLimit);

if (Capsule.DrivingBone.BoneIndex >= 0)
if (Capsule.DrivingBone.IsValidToEvaluate(BoneContainer))
{
const FCompactPoseBoneIndex CompactPoseIndex = Capsule.DrivingBone.GetCompactPoseIndex(BoneContainer);
FTransform BoneTransform = Output.Pose.GetComponentSpaceTransform(CompactPoseIndex);
Expand All @@ -469,11 +471,12 @@ void FAnimNode_KawaiiPhysics::UpdateCapsuleLimits(TArray<FCapsuleLimit>& Limits,
FAnimationRuntime::ConvertBoneSpaceTransformToCS(ComponentTransform, Output.Pose, BoneTransform, CompactPoseIndex, BCS_BoneSpace);
Capsule.Location = BoneTransform.GetLocation();
Capsule.Rotation = BoneTransform.GetRotation();

Capsule.bEnable = true;
}
else
{
Capsule.Location = Capsule.OffsetLocation;
Capsule.Rotation = Capsule.OffsetRotation.Quaternion();
Capsule.bEnable = false;
}
}
}
Expand All @@ -486,7 +489,7 @@ void FAnimNode_KawaiiPhysics::UpdatePlanerLimits(TArray<FPlanarLimit>& Limits, F
{
SCOPE_CYCLE_COUNTER(STAT_KawaiiPhysics_UpdatePlanerLimit);

if (Planar.DrivingBone.BoneIndex >= 0)
if (Planar.DrivingBone.IsValidToEvaluate(BoneContainer))
{
const FCompactPoseBoneIndex CompactPoseIndex = Planar.DrivingBone.GetCompactPoseIndex(BoneContainer);
FTransform BoneTransform = Output.Pose.GetComponentSpaceTransform(CompactPoseIndex);
Expand All @@ -500,13 +503,12 @@ void FAnimNode_KawaiiPhysics::UpdatePlanerLimits(TArray<FPlanarLimit>& Limits, F
Planar.Rotation = BoneTransform.GetRotation();
Planar.Rotation.Normalize();
Planar.Plane = FPlane(Planar.Location, Planar.Rotation.GetUpVector());

Planar.bEnable = true;
}
else
{
Planar.Location = Planar.OffsetLocation;
Planar.Rotation = Planar.OffsetRotation.Quaternion();
Planar.Rotation.Normalize();
Planar.Plane = FPlane(Planar.Location, Planar.Rotation.GetUpVector());
Planar.bEnable = false;
}
}
}
Expand Down Expand Up @@ -740,7 +742,7 @@ void FAnimNode_KawaiiPhysics::AdjustBySphereCollision(FKawaiiPhysicsModifyBone&
{
for (auto& Sphere : Limits)
{
if (Sphere.Radius <= 0.0f)
if (!Sphere.bEnable || Sphere.Radius <= 0.0f)
{
continue;
}
Expand Down Expand Up @@ -783,7 +785,7 @@ void FAnimNode_KawaiiPhysics::AdjustByCapsuleCollision(FKawaiiPhysicsModifyBone&
{
for (auto& Capsule : Limits)
{
if (Capsule.Radius <= 0 || Capsule.Length <= 0)
if (!Capsule.bEnable || Capsule.Radius <= 0 || Capsule.Length <= 0)
{
continue;
}
Expand All @@ -805,6 +807,10 @@ void FAnimNode_KawaiiPhysics::AdjustByPlanerCollision(FKawaiiPhysicsModifyBone&
{
for (auto& Planar : Limits)
{
if(!Planar.bEnable)
{
continue;
}
FVector PointOnPlane = FVector::PointPlaneProject(Bone.Location, Planar.Plane);
const float DistSquared = (Bone.Location - PointOnPlane).SizeSquared();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ struct FCollisionLimitBase

UPROPERTY()
FQuat Rotation = FQuat::Identity;

UPROPERTY()
bool bEnable = true;

#if WITH_EDITORONLY_DATA

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void FKawaiiPhysicsEditMode::RenderSphericalLimits(FPrimitiveDrawInterface* PDI)
for( int32 i=0; i< RuntimeNode->SphericalLimits.Num(); i++)
{
auto& Sphere = RuntimeNode->SphericalLimits[i];
if (Sphere.Radius > 0)
if (Sphere.bEnable && Sphere.Radius > 0)
{
PDI->SetHitProxy(new HKawaiiPhysicsHitProxy(ECollisionLimitType::Spherical, i));
DrawSphere(PDI, Sphere.Location, FRotator::ZeroRotator, FVector(Sphere.Radius), 24, 6,
Expand All @@ -141,7 +141,7 @@ void FKawaiiPhysicsEditMode::RenderSphericalLimits(FPrimitiveDrawInterface* PDI)
for (int32 i = 0; i < RuntimeNode->SphericalLimitsData.Num(); i++)
{
auto& Sphere = RuntimeNode->SphericalLimitsData[i];
if (Sphere.Radius > 0)
if (Sphere.bEnable && Sphere.Radius > 0)
{
PDI->SetHitProxy(new HKawaiiPhysicsHitProxy(ECollisionLimitType::Spherical, i, true));
DrawSphere(PDI, Sphere.Location, FRotator::ZeroRotator, FVector(Sphere.Radius), 24, 6,
Expand All @@ -160,7 +160,7 @@ void FKawaiiPhysicsEditMode::RenderCapsuleLimit(FPrimitiveDrawInterface* PDI) co
for (int32 i = 0; i < RuntimeNode->CapsuleLimits.Num(); i++)
{
auto& Capsule = RuntimeNode->CapsuleLimits[i];
if (Capsule.Radius > 0 && Capsule.Length > 0)
if (Capsule.bEnable && Capsule.Radius > 0 && Capsule.Length > 0)
{
FVector XAxis = Capsule.Rotation.GetAxisX();
FVector YAxis = Capsule.Rotation.GetAxisY();
Expand All @@ -185,7 +185,7 @@ void FKawaiiPhysicsEditMode::RenderCapsuleLimit(FPrimitiveDrawInterface* PDI) co
for (int32 i = 0; i < RuntimeNode->CapsuleLimitsData.Num(); i++)
{
auto& Capsule = RuntimeNode->CapsuleLimitsData[i];
if (Capsule.Radius > 0 && Capsule.Length > 0)
if (Capsule.bEnable && Capsule.Radius > 0 && Capsule.Length > 0)
{
FVector XAxis = Capsule.Rotation.GetAxisX();
FVector YAxis = Capsule.Rotation.GetAxisY();
Expand Down Expand Up @@ -216,6 +216,10 @@ void FKawaiiPhysicsEditMode::RenderPlanerLimit(FPrimitiveDrawInterface* PDI)
for (int32 i = 0; i < RuntimeNode->PlanarLimits.Num(); i++)
{
auto& Plane = RuntimeNode->PlanarLimits[i];
if(!Plane.bEnable)
{
continue;
}

FTransform PlaneTransform = FTransform(Plane.Rotation, Plane.Location);
PlaneTransform.NormalizeRotation();
Expand All @@ -228,7 +232,11 @@ void FKawaiiPhysicsEditMode::RenderPlanerLimit(FPrimitiveDrawInterface* PDI)
for (int32 i = 0; i < RuntimeNode->PlanarLimitsData.Num(); i++)
{
auto& Plane = RuntimeNode->PlanarLimitsData[i];

if(!Plane.bEnable)
{
continue;
}

FTransform PlaneTransform = FTransform(Plane.Rotation, Plane.Location);
PlaneTransform.NormalizeRotation();

Expand Down

0 comments on commit aff5328

Please sign in to comment.