If you flatten a capsule collider so it’s height equals its diameter (2x it’s radius). i.e. the capsule is actually a sphere
In this case, Vertex0 and Vertex1 are equal which should still be in theory fine but unfortunately causes NaNs within the physics simulation/queries when doing convex convex collisions.
I first spotted this when I made a fork of physics a couple of weeks ago to use as a small spatial library and ran into this issue, patched it and totally forgot about it.
However someone reported NaNs in our physics at work earlier this week so I had a bit of a dive and after some debugging, noticed the problem again. Thankfully as mentioned I had already run into this a couple of weeks ago so was easy to realize problem and patch once i spotted it.
This picture is within a spherecast hitting the capsule, hopefully it explains the problem enough.
My workaround for this in baking is to check for it, and replace it with a sphere.
case ShapeType.Capsule:
{
res.CapsuleProperties = shape.GetCapsuleProperties()
.BakeToBodySpace(localToWorld, shapeToWorld)
.ToRuntime();
// If 2*radius == height then switch to a sphere to avoid nans
if (res.CapsuleProperties.Vertex0.Equals(res.CapsuleProperties.Vertex1))
{
res.ShapeType = ShapeType.Sphere;
res.SphereProperties =
shape.GetCapsuleSphereProperties(out orientation).BakeToBodySpace(localToWorld, shapeToWorld, ref orientation, bakeUniformScale);
}
break;
}
internal SphereGeometry GetCapsuleSphereProperties(out EulerAngles orientation)
{
orientation = m_PrimitiveOrientation;
return new SphereGeometry
{
Center = m_PrimitiveCenter,
Radius = m_Capsule.Radius,
};
}
i don’t know if this issue occurs with the regular CapsuleCollider or it’s limited to the older PhysicsShapeAuthoring. If it’s not limited to PhysicsShapeAuthoring then a better solution to fix it at runtime would be preferred though.