Distance from point to collider?

  • I have a point in the world
  • I have a collider
  • I’d like to know the distance from point to collider (especially the negative distance if there is penetration)

I’m trying to do this like so:

ConvexCollider* convexCollider = (ConvexCollider*)physicsCollider.ColliderPtr;

PointDistanceInput pointDistanceInput = new PointDistanceInput
{
    Filter = convexCollider->Filter,
    MaxDistance = 0f,
    Position = myWorldSpacePoint,
};
convexCollider->CalculateDistance(pointDistanceInput, out DistanceHit pointDistanceHit);

But I always get a distance of zero. What am I doing wrong?

  • Is my cast to ConvexCollider valid? (“physicsCollider” here is a capsule primitive collider, of type “PhysicsCollider”)
  • Is my “Position” assignment in PointDistanceInput valid? Should it be in worldSpace?

MaxDistance=0 means the query will not return a hit unless the point is inside the collider. CalculateDistance() returns a bool indicating whether it found a hit within the specified MaxDistance or not. The purpose is to let the query early out without finding the closest point if it knows there is none closer than MaxDistance, but I don’t think any ConvexCollider implements any early out yet, so it’s just as well to set MaxDistance = float.MaxValue.

Position should be in the ConvexCollider’s local space.

1 Like

Also, you may not want to set the filter like that. CalculateDistance() will test the filter on the query against the filter on the collider. So if the filter on the collider disables self-collision, then this query would not return a hit.

1 Like