Path: blob/master/thirdparty/jolt_physics/Jolt/Geometry/EPAPenetrationDepth.h
21731 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Core/StaticArray.h>7#include <Jolt/Core/Profiler.h>8#include <Jolt/Geometry/GJKClosestPoint.h>9#include <Jolt/Geometry/EPAConvexHullBuilder.h>1011//#define JPH_EPA_PENETRATION_DEPTH_DEBUG1213JPH_NAMESPACE_BEGIN1415/// Implementation of Expanding Polytope Algorithm as described in:16///17/// Proximity Queries and Penetration Depth Computation on 3D Game Objects - Gino van den Bergen18///19/// The implementation of this algorithm does not completely follow the article, instead of splitting20/// triangles at each edge as in fig. 7 in the article, we build a convex hull (removing any triangles that21/// are facing the new point, thereby avoiding the problem of getting really oblong triangles as mentioned in22/// the article).23///24/// The algorithm roughly works like:25///26/// - Start with a simplex of the Minkowski sum (difference) of two objects that was calculated by GJK27/// - This simplex should contain the origin (or else GJK would have reported: no collision)28/// - In cases where the simplex consists of 1 - 3 points, find some extra support points (of the Minkowski sum) to get to at least 4 points29/// - Convert this into a convex hull with non-zero volume (which includes the origin)30/// - A: Calculate the closest point to the origin for all triangles of the hull and take the closest one31/// - Calculate a new support point (of the Minkowski sum) in this direction and add this point to the convex hull32/// - This will remove all faces that are facing the new point and will create new triangles to fill up the hole33/// - Loop to A until no closer point found34/// - The closest point indicates the position / direction of least penetration35class EPAPenetrationDepth36{37private:38// Typedefs39static constexpr int cMaxPoints = EPAConvexHullBuilder::cMaxPoints;40static constexpr int cMaxPointsToIncludeOriginInHull = 32;41static_assert(cMaxPointsToIncludeOriginInHull < cMaxPoints);4243using Triangle = EPAConvexHullBuilder::Triangle;44using Points = EPAConvexHullBuilder::Points;4546/// The GJK algorithm, used to start the EPA algorithm47GJKClosestPoint mGJK;4849#ifdef JPH_ENABLE_ASSERTS50/// Tolerance as passed to the GJK algorithm, used for asserting.51float mGJKTolerance = 0.0f;52#endif // JPH_ENABLE_ASSERTS5354/// A list of support points for the EPA algorithm55class SupportPoints56{57public:58/// List of support points59Points mY;60Vec3 mP[cMaxPoints];61Vec3 mQ[cMaxPoints];6263/// Calculate and add new support point to the list of points64template <typename A, typename B>65Vec3 Add(const A &inA, const B &inB, Vec3Arg inDirection, int &outIndex)66{67// Get support point of the minkowski sum A - B68Vec3 p = inA.GetSupport(inDirection);69Vec3 q = inB.GetSupport(-inDirection);70Vec3 w = p - q;7172// Store new point73outIndex = mY.size();74mY.push_back(w);75mP[outIndex] = p;76mQ[outIndex] = q;7778return w;79}80};8182public:83/// Return code for GetPenetrationDepthStepGJK84enum class EStatus85{86NotColliding, ///< Returned if the objects don't collide, in this case outPointA/outPointB are invalid87Colliding, ///< Returned if the objects penetrate88Indeterminate ///< Returned if the objects penetrate further than the convex radius. In this case you need to call GetPenetrationDepthStepEPA to get the actual penetration depth.89};9091/// Calculates penetration depth between two objects, first step of two (the GJK step)92///93/// @param inAExcludingConvexRadius Object A without convex radius.94/// @param inBExcludingConvexRadius Object B without convex radius.95/// @param inConvexRadiusA Convex radius for A.96/// @param inConvexRadiusB Convex radius for B.97/// @param ioV Pass in previously returned value or (1, 0, 0). On return this value is changed to direction to move B out of collision along the shortest path (magnitude is meaningless).98/// @param inTolerance Minimal distance before A and B are considered colliding.99/// @param outPointA Position on A that has the least amount of penetration.100/// @param outPointB Position on B that has the least amount of penetration.101/// Use |outPointB - outPointA| to get the distance of penetration.102template <typename AE, typename BE>103EStatus GetPenetrationDepthStepGJK(const AE &inAExcludingConvexRadius, float inConvexRadiusA, const BE &inBExcludingConvexRadius, float inConvexRadiusB, float inTolerance, Vec3 &ioV, Vec3 &outPointA, Vec3 &outPointB)104{105JPH_IF_ENABLE_ASSERTS(mGJKTolerance = inTolerance;)106107// Don't supply a zero ioV, we only want to get points on the hull of the Minkowsky sum and not internal points.108//109// Note that if the assert below triggers, it is very likely that you have a MeshShape that contains a degenerate triangle (e.g. a sliver).110// Go up a couple of levels in the call stack to see if we're indeed testing a triangle and if it is degenerate.111// If this is the case then fix the triangles you supply to the MeshShape.112JPH_ASSERT(!ioV.IsNearZero());113114// Get closest points115float combined_radius = inConvexRadiusA + inConvexRadiusB;116float combined_radius_sq = combined_radius * combined_radius;117float closest_points_dist_sq = mGJK.GetClosestPoints(inAExcludingConvexRadius, inBExcludingConvexRadius, inTolerance, combined_radius_sq, ioV, outPointA, outPointB);118if (closest_points_dist_sq > combined_radius_sq)119{120// No collision121return EStatus::NotColliding;122}123if (closest_points_dist_sq > 0.0f)124{125// Collision within convex radius, adjust points for convex radius126float v_len = sqrt(closest_points_dist_sq); // GetClosestPoints function returns |ioV|^2 when return value < FLT_MAX127outPointA += ioV * (inConvexRadiusA / v_len);128outPointB -= ioV * (inConvexRadiusB / v_len);129return EStatus::Colliding;130}131132return EStatus::Indeterminate;133}134135/// Calculates penetration depth between two objects, second step (the EPA step)136///137/// @param inAIncludingConvexRadius Object A with convex radius138/// @param inBIncludingConvexRadius Object B with convex radius139/// @param inTolerance A factor that determines the accuracy of the result. If the change of the squared distance is less than inTolerance * current_penetration_depth^2 the algorithm will terminate. Should be bigger or equal to FLT_EPSILON.140/// @param outV Direction to move B out of collision along the shortest path (magnitude is meaningless)141/// @param outPointA Position on A that has the least amount of penetration142/// @param outPointB Position on B that has the least amount of penetration143/// Use |outPointB - outPointA| to get the distance of penetration144///145/// @return False if the objects don't collide, in this case outPointA/outPointB are invalid.146/// True if the objects penetrate147template <typename AI, typename BI>148bool GetPenetrationDepthStepEPA(const AI &inAIncludingConvexRadius, const BI &inBIncludingConvexRadius, float inTolerance, Vec3 &outV, Vec3 &outPointA, Vec3 &outPointB)149{150JPH_PROFILE_FUNCTION();151152// Check that the tolerance makes sense (smaller value than this will just result in needless iterations)153JPH_ASSERT(inTolerance >= FLT_EPSILON);154155// Fetch the simplex from GJK algorithm156SupportPoints support_points;157mGJK.GetClosestPointsSimplex(support_points.mY.data(), support_points.mP, support_points.mQ, support_points.mY.GetSizeRef());158159// Fill up the amount of support points to 4160switch (support_points.mY.size())161{162case 1:163{164// 1 vertex, which must be at the origin, which is useless for our purpose165JPH_ASSERT(support_points.mY[0].IsNearZero(Square(mGJKTolerance)));166support_points.mY.pop_back();167168// Add support points in 4 directions to form a tetrahedron around the origin169int p1, p2, p3, p4;170(void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, Vec3(0, 1, 0), p1);171(void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, Vec3(-1, -1, -1), p2);172(void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, Vec3(1, -1, -1), p3);173(void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, Vec3(0, -1, 1), p4);174JPH_ASSERT(p1 == 0);175JPH_ASSERT(p2 == 1);176JPH_ASSERT(p3 == 2);177JPH_ASSERT(p4 == 3);178break;179}180181case 2:182{183// Two vertices, create 3 extra by taking perpendicular axis and rotating it around in 120 degree increments184Vec3 axis = (support_points.mY[1] - support_points.mY[0]).Normalized();185Mat44 rotation = Mat44::sRotation(axis, DegreesToRadians(120.0f));186Vec3 dir1 = axis.GetNormalizedPerpendicular();187Vec3 dir2 = rotation * dir1;188Vec3 dir3 = rotation * dir2;189int p1, p2, p3;190(void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, dir1, p1);191(void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, dir2, p2);192(void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, dir3, p3);193JPH_ASSERT(p1 == 2);194JPH_ASSERT(p2 == 3);195JPH_ASSERT(p3 == 4);196break;197}198199case 3:200case 4:201// We already have enough points202break;203}204205// Create hull out of the initial points206JPH_ASSERT(support_points.mY.size() >= 3);207EPAConvexHullBuilder hull(support_points.mY);208#ifdef JPH_EPA_CONVEX_BUILDER_DRAW209hull.DrawLabel("Build initial hull");210#endif211#ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG212Trace("Init: num_points = %u", (uint)support_points.mY.size());213#endif214hull.Initialize(0, 1, 2);215for (typename Points::size_type i = 3; i < support_points.mY.size(); ++i)216{217float dist_sq;218Triangle *t = hull.FindFacingTriangle(support_points.mY[i], dist_sq);219if (t != nullptr)220{221EPAConvexHullBuilder::NewTriangles new_triangles;222if (!hull.AddPoint(t, i, FLT_MAX, new_triangles))223{224// We can't recover from a failure to add a point to the hull because the old triangles have been unlinked already.225// Assume no collision. This can happen if the shapes touch in 1 point (or plane) in which case the hull is degenerate.226return false;227}228}229}230231#ifdef JPH_EPA_CONVEX_BUILDER_DRAW232hull.DrawLabel("Complete hull");233234// Generate the hull of the Minkowski difference for visualization235MinkowskiDifference diff(inAIncludingConvexRadius, inBIncludingConvexRadius);236DebugRenderer::GeometryRef geometry = DebugRenderer::sInstance->CreateTriangleGeometryForConvex([&diff](Vec3Arg inDirection) { return diff.GetSupport(inDirection); });237hull.DrawGeometry(geometry, Color::sYellow);238239hull.DrawLabel("Ensure origin in hull");240#endif241242// Loop until we are sure that the origin is inside the hull243for (;;)244{245// Get the next closest triangle246Triangle *t = hull.PeekClosestTriangleInQueue();247248// Don't process removed triangles, just free them (because they're in a heap we don't remove them earlier since we would have to rebuild the sorted heap)249if (t->mRemoved)250{251hull.PopClosestTriangleFromQueue();252253// If we run out of triangles, we couldn't include the origin in the hull so there must be very little penetration and we report no collision.254if (!hull.HasNextTriangle())255return false;256257hull.FreeTriangle(t);258continue;259}260261// If the closest to the triangle is zero or positive, the origin is in the hull and we can proceed to the main algorithm262if (t->mClosestLenSq >= 0.0f)263break;264265#ifdef JPH_EPA_CONVEX_BUILDER_DRAW266hull.DrawLabel("Next iteration");267#endif268#ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG269Trace("EncapsulateOrigin: verts = (%d, %d, %d), closest_dist_sq = %g, centroid = (%g, %g, %g), normal = (%g, %g, %g)",270t->mEdge[0].mStartIdx, t->mEdge[1].mStartIdx, t->mEdge[2].mStartIdx,271t->mClosestLenSq,272t->mCentroid.GetX(), t->mCentroid.GetY(), t->mCentroid.GetZ(),273t->mNormal.GetX(), t->mNormal.GetY(), t->mNormal.GetZ());274#endif275276// Remove the triangle from the queue before we start adding new ones (which may result in a new closest triangle at the front of the queue)277hull.PopClosestTriangleFromQueue();278279// Add a support point to get the origin inside the hull280int new_index;281Vec3 w = support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, t->mNormal, new_index);282283#ifdef JPH_EPA_CONVEX_BUILDER_DRAW284// Draw the point that we're adding285hull.DrawMarker(w, Color::sRed, 1.0f);286hull.DrawWireTriangle(*t, Color::sRed);287hull.DrawState();288#endif289290// Add the point to the hull, if we fail we terminate and report no collision291EPAConvexHullBuilder::NewTriangles new_triangles;292if (!t->IsFacing(w) || !hull.AddPoint(t, new_index, FLT_MAX, new_triangles))293return false;294295// The triangle is facing the support point "w" and can now be safely removed296JPH_ASSERT(t->mRemoved);297hull.FreeTriangle(t);298299// If we run out of triangles or points, we couldn't include the origin in the hull so there must be very little penetration and we report no collision.300if (!hull.HasNextTriangle() || support_points.mY.size() >= cMaxPointsToIncludeOriginInHull)301return false;302}303304#ifdef JPH_EPA_CONVEX_BUILDER_DRAW305hull.DrawLabel("Main algorithm");306#endif307308// Current closest distance to origin309float closest_dist_sq = FLT_MAX;310311// Remember last good triangle312Triangle *last = nullptr;313314// If we want to flip the penetration depth315bool flip_v_sign = false;316317// Loop until closest point found318do319{320// Get closest triangle to the origin321Triangle *t = hull.PopClosestTriangleFromQueue();322323// Don't process removed triangles, just free them (because they're in a heap we don't remove them earlier since we would have to rebuild the sorted heap)324if (t->mRemoved)325{326hull.FreeTriangle(t);327continue;328}329330#ifdef JPH_EPA_CONVEX_BUILDER_DRAW331hull.DrawLabel("Next iteration");332#endif333#ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG334Trace("FindClosest: verts = (%d, %d, %d), closest_len_sq = %g, centroid = (%g, %g, %g), normal = (%g, %g, %g)",335t->mEdge[0].mStartIdx, t->mEdge[1].mStartIdx, t->mEdge[2].mStartIdx,336t->mClosestLenSq,337t->mCentroid.GetX(), t->mCentroid.GetY(), t->mCentroid.GetZ(),338t->mNormal.GetX(), t->mNormal.GetY(), t->mNormal.GetZ());339#endif340// Check if next triangle is further away than closest point, we've found the closest point341if (t->mClosestLenSq >= closest_dist_sq)342break;343344// Replace last good with this triangle345if (last != nullptr)346hull.FreeTriangle(last);347last = t;348349// Add support point in direction of normal of the plane350// Note that the article uses the closest point between the origin and plane, but this always has the exact same direction as the normal (if the origin is behind the plane)351// and this way we do less calculations and lose less precision352int new_index;353Vec3 w = support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, t->mNormal, new_index);354355// Project w onto the triangle normal356float dot = t->mNormal.Dot(w);357358// Check if we just found a separating axis. This can happen if the shape shrunk by convex radius and then expanded by359// convex radius is bigger then the original shape due to inaccuracies in the shrinking process.360if (dot < 0.0f)361return false;362363// Get the distance squared (along normal) to the support point364float dist_sq = Square(dot) / t->mNormal.LengthSq();365366#ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG367Trace("FindClosest: w = (%g, %g, %g), dot = %g, dist_sq = %g",368w.GetX(), w.GetY(), w.GetZ(),369dot, dist_sq);370#endif371#ifdef JPH_EPA_CONVEX_BUILDER_DRAW372// Draw the point that we're adding373hull.DrawMarker(w, Color::sPurple, 1.0f);374hull.DrawWireTriangle(*t, Color::sPurple);375hull.DrawState();376#endif377378// If the error became small enough, we've converged379if (dist_sq - t->mClosestLenSq < t->mClosestLenSq * inTolerance)380{381#ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG382Trace("Converged");383#endif // JPH_EPA_PENETRATION_DEPTH_DEBUG384break;385}386387// Keep track of the minimum distance388closest_dist_sq = min(closest_dist_sq, dist_sq);389390// If the triangle thinks this point is not front facing, we've reached numerical precision and we're done391if (!t->IsFacing(w))392{393#ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG394Trace("Not facing triangle");395#endif // JPH_EPA_PENETRATION_DEPTH_DEBUG396break;397}398399// Add point to hull400EPAConvexHullBuilder::NewTriangles new_triangles;401if (!hull.AddPoint(t, new_index, closest_dist_sq, new_triangles))402{403#ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG404Trace("Could not add point");405#endif // JPH_EPA_PENETRATION_DEPTH_DEBUG406break;407}408409// If the hull is starting to form defects then we're reaching numerical precision and we have to stop410bool has_defect = false;411for (const Triangle *nt : new_triangles)412if (nt->IsFacingOrigin())413{414has_defect = true;415break;416}417if (has_defect)418{419#ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG420Trace("Has defect");421#endif // JPH_EPA_PENETRATION_DEPTH_DEBUG422// When the hull has defects it is possible that the origin has been classified on the wrong side of the triangle423// so we do an additional check to see if the penetration in the -triangle normal direction is smaller than424// the penetration in the triangle normal direction. If so we must flip the sign of the penetration depth.425Vec3 w2 = inAIncludingConvexRadius.GetSupport(-t->mNormal) - inBIncludingConvexRadius.GetSupport(t->mNormal);426float dot2 = -t->mNormal.Dot(w2);427if (dot2 < dot)428flip_v_sign = true;429break;430}431}432while (hull.HasNextTriangle() && support_points.mY.size() < cMaxPoints);433434// Determine closest points, if last == null it means the hull was a plane so there's no penetration435if (last == nullptr)436return false;437438#ifdef JPH_EPA_CONVEX_BUILDER_DRAW439hull.DrawLabel("Closest found");440hull.DrawWireTriangle(*last, Color::sWhite);441hull.DrawArrow(last->mCentroid, last->mCentroid + last->mNormal.NormalizedOr(Vec3::sZero()), Color::sWhite, 0.1f);442hull.DrawState();443#endif444445// Calculate penetration by getting the vector from the origin to the closest point on the triangle:446// distance = (centroid - origin) . normal / |normal|, closest = origin + distance * normal / |normal|447outV = (last->mCentroid.Dot(last->mNormal) / last->mNormal.LengthSq()) * last->mNormal;448449// If penetration is near zero, treat this as a non collision since we cannot find a good normal450if (outV.IsNearZero())451return false;452453// Check if we have to flip the sign of the penetration depth454if (flip_v_sign)455outV = -outV;456457// Use the barycentric coordinates for the closest point to the origin to find the contact points on A and B458Vec3 p0 = support_points.mP[last->mEdge[0].mStartIdx];459Vec3 p1 = support_points.mP[last->mEdge[1].mStartIdx];460Vec3 p2 = support_points.mP[last->mEdge[2].mStartIdx];461462Vec3 q0 = support_points.mQ[last->mEdge[0].mStartIdx];463Vec3 q1 = support_points.mQ[last->mEdge[1].mStartIdx];464Vec3 q2 = support_points.mQ[last->mEdge[2].mStartIdx];465466if (last->mLambdaRelativeTo0)467{468// y0 was the reference vertex469outPointA = p0 + last->mLambda[0] * (p1 - p0) + last->mLambda[1] * (p2 - p0);470outPointB = q0 + last->mLambda[0] * (q1 - q0) + last->mLambda[1] * (q2 - q0);471}472else473{474// y1 was the reference vertex475outPointA = p1 + last->mLambda[0] * (p0 - p1) + last->mLambda[1] * (p2 - p1);476outPointB = q1 + last->mLambda[0] * (q0 - q1) + last->mLambda[1] * (q2 - q1);477}478479return true;480}481482/// This function combines the GJK and EPA steps and is provided as a convenience function.483/// Note: less performant since you're providing all support functions in one go484/// Note 2: You need to initialize ioV, see documentation at GetPenetrationDepthStepGJK!485template <typename AE, typename AI, typename BE, typename BI>486bool GetPenetrationDepth(const AE &inAExcludingConvexRadius, const AI &inAIncludingConvexRadius, float inConvexRadiusA, const BE &inBExcludingConvexRadius, const BI &inBIncludingConvexRadius, float inConvexRadiusB, float inCollisionToleranceSq, float inPenetrationTolerance, Vec3 &ioV, Vec3 &outPointA, Vec3 &outPointB)487{488// Check result of collision detection489switch (GetPenetrationDepthStepGJK(inAExcludingConvexRadius, inConvexRadiusA, inBExcludingConvexRadius, inConvexRadiusB, inCollisionToleranceSq, ioV, outPointA, outPointB))490{491case EPAPenetrationDepth::EStatus::Colliding:492return true;493494case EPAPenetrationDepth::EStatus::NotColliding:495return false;496497case EPAPenetrationDepth::EStatus::Indeterminate:498return GetPenetrationDepthStepEPA(inAIncludingConvexRadius, inBIncludingConvexRadius, inPenetrationTolerance, ioV, outPointA, outPointB);499}500501JPH_ASSERT(false);502return false;503}504505/// Test if a cast shape inA moving from inStart to lambda * inStart.GetTranslation() + inDirection where lambda e [0, ioLambda> intersects inB506///507/// @param inStart Start position and orientation of the convex object508/// @param inDirection Direction of the sweep (ioLambda * inDirection determines length)509/// @param inCollisionTolerance The minimal distance between A and B before they are considered colliding510/// @param inPenetrationTolerance A factor that determines the accuracy of the result. If the change of the squared distance is less than inTolerance * current_penetration_depth^2 the algorithm will terminate. Should be bigger or equal to FLT_EPSILON.511/// @param inA The convex object A, must support the GetSupport(Vec3) function.512/// @param inB The convex object B, must support the GetSupport(Vec3) function.513/// @param inConvexRadiusA The convex radius of A, this will be added on all sides to pad A.514/// @param inConvexRadiusB The convex radius of B, this will be added on all sides to pad B.515/// @param inReturnDeepestPoint If the shapes are initially intersecting this determines if the EPA algorithm will run to find the deepest point516/// @param ioLambda The max fraction along the sweep, on output updated with the actual collision fraction.517/// @param outPointA is the contact point on A518/// @param outPointB is the contact point on B519/// @param outContactNormal is either the contact normal when the objects are touching or the penetration axis when the objects are penetrating at the start of the sweep (pointing from A to B, length will not be 1)520///521/// @return true if the a hit was found, in which case ioLambda, outPointA, outPointB and outSurfaceNormal are updated.522template <typename A, typename B>523bool CastShape(Mat44Arg inStart, Vec3Arg inDirection, float inCollisionTolerance, float inPenetrationTolerance, const A &inA, const B &inB, float inConvexRadiusA, float inConvexRadiusB, bool inReturnDeepestPoint, float &ioLambda, Vec3 &outPointA, Vec3 &outPointB, Vec3 &outContactNormal)524{525JPH_IF_ENABLE_ASSERTS(mGJKTolerance = inCollisionTolerance;)526527// First determine if there's a collision at all528if (!mGJK.CastShape(inStart, inDirection, inCollisionTolerance, inA, inB, inConvexRadiusA, inConvexRadiusB, ioLambda, outPointA, outPointB, outContactNormal))529return false;530531// When our contact normal is too small, we don't have an accurate result532bool contact_normal_invalid = outContactNormal.IsNearZero(Square(inCollisionTolerance));533534if (inReturnDeepestPoint535&& ioLambda == 0.0f // Only when lambda = 0 we can have the bodies overlap536&& (inConvexRadiusA + inConvexRadiusB == 0.0f // When no convex radius was provided we can never trust contact points at lambda = 0537|| contact_normal_invalid))538{539// If we're initially intersecting, we need to run the EPA algorithm in order to find the deepest contact point540AddConvexRadius add_convex_a(inA, inConvexRadiusA);541AddConvexRadius add_convex_b(inB, inConvexRadiusB);542TransformedConvexObject transformed_a(inStart, add_convex_a);543if (!GetPenetrationDepthStepEPA(transformed_a, add_convex_b, inPenetrationTolerance, outContactNormal, outPointA, outPointB))544return false;545}546else if (contact_normal_invalid)547{548// If we weren't able to calculate a contact normal, use the cast direction instead549outContactNormal = inDirection;550}551552return true;553}554};555556JPH_NAMESPACE_END557558559