From c5ecba0327a22348333e8b377bb6d9bb827c0a16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Leone=20G=C3=A1mez?= Date: Sat, 2 Mar 2019 18:49:32 +0100 Subject: [PATCH 1/2] Fix Raycast reporting false positives for rays that cross a bounding box but not the shape that is inside it, thanks to analyzing BEPU Physics raycasting source code https://github.com/bepu/bepuphysics1/blob/e0438719412e2eab8683d5ca65c1b0bb0e9b177a/BEPUphysics/CollisionTests/CollisionAlgorithms/GJK/GJKToolbox.cs#L347 --- source/Jitter/Collision/GJKCollide.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/source/Jitter/Collision/GJKCollide.cs b/source/Jitter/Collision/GJKCollide.cs index 044842d6..23b1c9f6 100644 --- a/source/Jitter/Collision/GJKCollide.cs +++ b/source/Jitter/Collision/GJKCollide.cs @@ -346,6 +346,12 @@ public static bool Raycast(ISupportMappable support, ref JMatrix orientation, re else { lambda = lambda - VdotW / VdotR; + if (lambda > 1) + { + //If we've gone beyond where the ray can reach, there's obviously no hit. + simplexSolverPool.GiveBack(simplexSolver); + return false; + } JVector.Multiply(ref r, lambda, out x); JVector.Add(ref origin, ref x, out x); JVector.Subtract(ref x, ref p, out w); From 598e60e1f7576230745ddca7d064cbb78f636c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Mon, 3 Jun 2019 04:36:56 +0200 Subject: [PATCH 2/2] Fix random flickering of the hit point along the ray. --- source/Jitter/Collision/GJKCollide.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/source/Jitter/Collision/GJKCollide.cs b/source/Jitter/Collision/GJKCollide.cs index 23b1c9f6..fff3228e 100644 --- a/source/Jitter/Collision/GJKCollide.cs +++ b/source/Jitter/Collision/GJKCollide.cs @@ -334,6 +334,11 @@ public static bool Raycast(ISupportMappable support, ref JMatrix orientation, re float VdotW = JVector.Dot(ref v, ref w); + if (lambda > 1.0f) + { + return false; + } + if (VdotW > 0.0f) { VdotR = JVector.Dot(ref v, ref r); @@ -346,12 +351,6 @@ public static bool Raycast(ISupportMappable support, ref JMatrix orientation, re else { lambda = lambda - VdotW / VdotR; - if (lambda > 1) - { - //If we've gone beyond where the ray can reach, there's obviously no hit. - simplexSolverPool.GiveBack(simplexSolver); - return false; - } JVector.Multiply(ref r, lambda, out x); JVector.Add(ref origin, ref x, out x); JVector.Subtract(ref x, ref p, out w); @@ -367,13 +366,16 @@ public static bool Raycast(ISupportMappable support, ref JMatrix orientation, re // Giving back the fraction like this *should* work // but is inaccurate against large objects: - // fraction = lambda; + fraction = lambda; + /* + // Fraction will be used because calculating fraction using this code results in random flickering of the hit point along the ray. JVector p1, p2; simplexSolver.ComputePoints(out p1, out p2); p2 = p2 - origin; fraction = p2.Length() / direction.Length(); + */ #endregion