Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip processing for non-convex MeshCollider #1657

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions Packages/Tracking/Physical Hands/Runtime/Scripts/ContactBone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,21 @@ private void UpdateObjectDistances(Collider[] colliderCache, int count)

if (IsPalm)
{
// Do palm logic
colliderPos = ContactUtils.ClosestPointEstimationFromRectangle(_palmColCenter, _palmColRotation, _palmExtentsReduced.xz(), collider);
bonePos = ContactUtils.ClosestPointToRectangleFace(_palmPoints, _palmPlane.ClosestPointOnPlane(colliderTransformPosition));
midPoint = colliderPos + (bonePos + ((bonePos - colliderPos).normalized * width) - colliderPos) / 2f;
if (collider is BoxCollider || collider is SphereCollider || collider is CapsuleCollider || (collider is MeshCollider meshCollider && meshCollider.convex))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce the number of type checks here, could you invert this logic to just check if (collider is MeshCollider meshCollider && !meshCollider.convex)?

{
// Do palm logic
colliderPos = ContactUtils.ClosestPointEstimationFromRectangle(_palmColCenter, _palmColRotation, _palmExtentsReduced.xz(), collider);
bonePos = ContactUtils.ClosestPointToRectangleFace(_palmPoints, _palmPlane.ClosestPointOnPlane(colliderTransformPosition));
midPoint = colliderPos + (bonePos + ((bonePos - colliderPos).normalized * width) - colliderPos) / 2f;

colliderPos = collider.ClosestPoint(midPoint);
bonePos = ContactUtils.ClosestPointToRectangleFace(_palmPoints, _palmPlane.ClosestPointOnPlane(midPoint));
colliderPos = collider.ClosestPoint(midPoint);
bonePos = ContactUtils.ClosestPointToRectangleFace(_palmPoints, _palmPlane.ClosestPointOnPlane(midPoint));
}
else
{
// Skip processing for non-convex MeshCollider
continue;
}
}
else
{
Expand All @@ -238,14 +246,22 @@ private void UpdateObjectDistances(Collider[] colliderCache, int count)
float lineLength = lineDir.magnitude;
lineDir.Normalize();

colliderPos = collider.ClosestPoint((boneTransformPosition + tipPosition) * 0.5f);
// Do joint logic
bonePos = ContactUtils.GetClosestPointOnFiniteLine(colliderTransformPosition, boneTransformPosition, lineDir, lineLength);
// We need to extrude the line to get the bone's edge
midPoint = colliderPos + (bonePos + ((bonePos - colliderPos).normalized * width) - colliderPos) / 2f;

colliderPos = collider.ClosestPoint(midPoint);
bonePos = ContactUtils.GetClosestPointOnFiniteLine(midPoint, boneTransformPosition, lineDir, lineLength);
if (collider is BoxCollider || collider is SphereCollider || collider is CapsuleCollider || (collider is MeshCollider meshCollider && meshCollider.convex))
{
colliderPos = collider.ClosestPoint((boneTransformPosition + tipPosition) * 0.5f);
// Do joint logic
bonePos = ContactUtils.GetClosestPointOnFiniteLine(colliderTransformPosition, boneTransformPosition, lineDir, lineLength);
// We need to extrude the line to get the bone's edge
midPoint = colliderPos + (bonePos + ((bonePos - colliderPos).normalized * width) - colliderPos) / 2f;

colliderPos = collider.ClosestPoint(midPoint);
bonePos = ContactUtils.GetClosestPointOnFiniteLine(midPoint, boneTransformPosition, lineDir, lineLength);
}
else
{
// Skip processing for non-convex MeshCollider
continue;
}
}

distance = Vector3.Distance(colliderPos, bonePos);
Expand Down