Skip to content

Commit 2dcab94

Browse files
authored
Fixed infinite recursion when colliding a TriangleShape vs a TriangleShape. (jrouwe#1621)
Fixes jrouwe#1620
1 parent fd37495 commit 2dcab94

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

Docs/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ For breaking API changes see [this document](https://github.com/jrouwe/JoltPhysi
1111

1212
### Bug Fixes
1313

14+
* Fixed infinite recursion when colliding a `TriangleShape` vs a `TriangleShape`.
1415
* 32-bit MinGW g++ doesn't call the correct overload for the new operator when a type is 16 bytes aligned. This could cause unaligned read access violations.
1516
* Fixed compiling in double precision and fixed issues with floating point contraction that caused unit test failures on LoongArch architecture.
1617
* Added an epsilon to the `CastRay` / `CastShape` early out condition to avoid dividing by a very small number and overflowing to INF. This can cause a float overflow exception.

Jolt/Physics/Collision/Shape/TriangleShape.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,12 @@ void TriangleShape::sRegister()
414414
CollisionDispatch::sRegisterCollideShape(s, EShapeSubType::Triangle, sCollideConvexVsTriangle);
415415
CollisionDispatch::sRegisterCastShape(s, EShapeSubType::Triangle, sCastConvexVsTriangle);
416416

417-
CollisionDispatch::sRegisterCollideShape(EShapeSubType::Triangle, s, CollisionDispatch::sReversedCollideShape);
418-
CollisionDispatch::sRegisterCastShape(EShapeSubType::Triangle, s, CollisionDispatch::sReversedCastShape);
417+
// Avoid registering triangle vs triangle as a reversed test to prevent infinite recursion
418+
if (s != EShapeSubType::Triangle)
419+
{
420+
CollisionDispatch::sRegisterCollideShape(EShapeSubType::Triangle, s, CollisionDispatch::sReversedCollideShape);
421+
CollisionDispatch::sRegisterCastShape(EShapeSubType::Triangle, s, CollisionDispatch::sReversedCastShape);
422+
}
419423
}
420424

421425
// Specialized collision functions

UnitTests/Physics/CollideShapeTests.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,4 +536,25 @@ TEST_SUITE("CollideShapeTests")
536536
}
537537
}
538538
}
539+
540+
TEST_CASE("TestCollideTriangleVsTriangle")
541+
{
542+
constexpr float cPenetration = 0.01f;
543+
544+
// A triangle centered around the origin in the XZ plane
545+
RefConst<Shape> t1 = new TriangleShape(Vec3(-1, 0, 1), Vec3(1, 0, 1), Vec3(0, 0, -1));
546+
547+
// A triangle in the XY plane with its tip just pointing in the origin
548+
RefConst<Shape> t2 = new TriangleShape(Vec3(-1, 1, 0), Vec3(1, 1, 0), Vec3(0, -cPenetration, 0));
549+
550+
CollideShapeSettings collide_settings;
551+
ClosestHitCollisionCollector<CollideShapeCollector> collector;
552+
CollisionDispatch::sCollideShapeVsShape(t1, t2, Vec3::sOne(), Vec3::sOne(), Mat44::sIdentity(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), collide_settings, collector);
553+
554+
CHECK(collector.HadHit());
555+
CHECK_APPROX_EQUAL(collector.mHit.mContactPointOn1, Vec3::sZero());
556+
CHECK_APPROX_EQUAL(collector.mHit.mContactPointOn2, Vec3(0, -cPenetration, 0));
557+
CHECK_APPROX_EQUAL(collector.mHit.mPenetrationDepth, cPenetration);
558+
CHECK_APPROX_EQUAL(collector.mHit.mPenetrationAxis.Normalized(), Vec3(0, 1, 0));
559+
}
539560
}

0 commit comments

Comments
 (0)