Skip to content

CastShape with a CylinderShape sometimes results in extremely large penetration depth #2103

Description

@exoticorn

When doing shape casts with a cylinder, I occasionally get a very large penetration depth when the cast cylinder starts out touching another body.

I have extracted the exact positions and shapes from one occurance into a unit test for an easy reproduction:

	TEST_CASE("TestCastShapeCylinderCapsule")
	{
		PhysicsTestContext c;
		CapsuleShapeSettings capsule_shape_settings(2.03114343f, 7.19732189f);
		capsule_shape_settings.SetEmbedded();
		c.CreateBody( &capsule_shape_settings, RVec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, EActivation::DontActivate);

		ShapeCastSettings settings;
		settings.mUseShrunkenShapeAndConvexRadius = true;

		Ref<Shape> cylinder_shape = new CylinderShape(0.3f, 0.4975f);
		RShapeCast shape_cast { cylinder_shape, Vec3::sOne(), RMat44::sTranslation(RVec3(-0.951660156f, -2.09155273f, -7.63574218)), Vec3(0.00244140625, -0.0068359375, 0.0029296875) };

		AllHitCollisionCollector<CastShapeCollector> collector;
		c.GetSystem()->GetNarrowPhaseQuery().CastShape(shape_cast, settings, RVec3::sZero(), collector);
		CHECK(collector.mHits.size() == 1);
		const ShapeCastResult &result = collector.mHits.front();
		CHECK(result.mPenetrationDepth < 0.01f);
	}

In this case, the cylinder starts touching the capsule (within some rounding error) and is cast by less than a centimeter. The penetration depth returned is 7.25m. (I have seen penetration depths > 15m returned previously at this situation in game.)
mContactPointOn1 is located far outside the cylinder shape (near the y axis).

result.mPenetrationDepth
(float) $0 = 7.24732208
result.mContactPointOn1
(JPH::Vec3) $1 = {
   = {
    mValue = (0.00618377374, -2, 0.0496161357, 1)
    mF32 = (0.00618377374, -2, 0.0496161357, 1)
  }
}
result.mContactPointOn2
(JPH::Vec3) $2 = {
   = {
    mValue = (-0.890132189, -2, -7.142066, 1)
    mF32 = (-0.890132189, -2, -7.142066, 1)
  }
}  

I tried setting settings.mReturnDeepestPoint = true, but that did not change anything in this case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions