Browse Source

Feeding a better initial penetration axis to GJK to improve performance (#41)

Jorrit Rouwe 3 years ago
parent
commit
6aa04652cd

+ 4 - 1
Jolt/Physics/Collision/CollideConvexVsTriangles.cpp

@@ -66,7 +66,10 @@ void CollideConvexVsTriangles::Collide(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2,
 	TriangleConvexSupport triangle(v0, v1, v2);
 	TriangleConvexSupport triangle(v0, v1, v2);
 
 
 	// Perform collision detection
 	// Perform collision detection
-	Vec3 penetration_axis = Vec3::sAxisX(), point1, point2;
+	// Note: As we don't remember the penetration axis from the last iteration, and it is likely that the shape (A) we're colliding the triangle (B) against is in front of the triangle,
+	// and the penetration axis is the shortest distance along to push B out of collision, we use the inverse of the triangle normal as an initial penetration axis. This has been seen
+	// to improve performance by approx. 5% over using a fixed axis like (1, 0, 0).
+	Vec3 penetration_axis = -triangle_normal, point1, point2;
 	EPAPenetrationDepth pen_depth;
 	EPAPenetrationDepth pen_depth;
 	EPAPenetrationDepth::EStatus status;
 	EPAPenetrationDepth::EStatus status;
 
 

+ 4 - 1
Jolt/Physics/Collision/Shape/ConvexShape.cpp

@@ -64,7 +64,10 @@ void ConvexShape::sCollideConvexVsConvex(const Shape *inShape1, const Shape *inS
 	if (!OrientedBox(transform_2_to_1, shape2_bbox).Overlaps(shape1_bbox))
 	if (!OrientedBox(transform_2_to_1, shape2_bbox).Overlaps(shape1_bbox))
 		return;
 		return;
 
 
-	Vec3 penetration_axis = Vec3::sAxisX(), point1, point2;
+	// Note: As we don't remember the penetration axis from the last iteration, and it is likely that shape2 is pushed out of
+	// collision relative to shape1 by comparing their COM's, we use that as an initial penetration axis: shape2.com - shape1.com
+	// This has been seen to improve performance by approx. 1% over using a fixed axis like (1, 0, 0).
+	Vec3 penetration_axis = transform_2_to_1.GetTranslation(), point1, point2;
 	EPAPenetrationDepth pen_depth;
 	EPAPenetrationDepth pen_depth;
 	EPAPenetrationDepth::EStatus status;
 	EPAPenetrationDepth::EStatus status;