public static void ComputeDistance(out DistanceOutput output, out SimplexCache cache, DistanceInput input) { cache = new SimplexCache(); /* * if (Settings.EnableDiagnostics) //FPE: We only gather diagnostics when enabled ++GJKCalls; */ // Initialize the simplex. Simplex simplex = new Simplex(); simplex.ReadCache(ref cache, input.ProxyA, ref input.TransformA, input.ProxyB, ref input.TransformB); // These store the vertices of the last simplex so that we // can check for duplicates and prevent cycling. var saveA = new int[3]; var saveB = new int[3]; //float distanceSqr1 = Settings.MaxFloat; // Main iteration loop. int iter = 0; while (iter < MaxGJKIterations) { // Copy simplex so we can identify duplicates. int saveCount = simplex.Count; for (var i = 0; i < saveCount; ++i) { saveA[i] = simplex.V[i].IndexA; saveB[i] = simplex.V[i].IndexB; } switch (simplex.Count) { case 1: break; case 2: simplex.Solve2(); break; case 3: simplex.Solve3(); break; default: throw new ArgumentOutOfRangeException(); } // If we have 3 points, then the origin is in the corresponding triangle. if (simplex.Count == 3) { break; } //FPE: This code was not used anyway. // Compute closest point. //Vector2 p = simplex.GetClosestPoint(); //float distanceSqr2 = p.LengthSquared(); // Ensure progress //if (distanceSqr2 >= distanceSqr1) //{ //break; //} //distanceSqr1 = distanceSqr2; // Get search direction. Vector2 d = simplex.GetSearchDirection(); // Ensure the search direction is numerically fit. if (d.LengthSquared < float.Epsilon * float.Epsilon) { // The origin is probably contained by a line segment // or triangle. Thus the shapes are overlapped. // We can't return zero here even though there may be overlap. // In case the simplex is a point, segment, or triangle it is difficult // to determine if the origin is contained in the CSO or very close to it. break; } // Compute a tentative new simplex vertex using support points. SimplexVertex vertex = simplex.V[simplex.Count]; vertex.IndexA = input.ProxyA.GetSupport(Transform.MulT(input.TransformA.Quaternion2D, -d)); vertex.WA = Transform.Mul(input.TransformA, input.ProxyA.Vertices[vertex.IndexA]); vertex.IndexB = input.ProxyB.GetSupport(Transform.MulT(input.TransformB.Quaternion2D, d)); vertex.WB = Transform.Mul(input.TransformB, input.ProxyB.Vertices[vertex.IndexB]); vertex.W = vertex.WB - vertex.WA; simplex.V[simplex.Count] = vertex; // Iteration count is equated to the number of support point calls. ++iter; /* * if (Settings.EnableDiagnostics) //FPE: We only gather diagnostics when enabled ++GJKIters; */ // Check for duplicate support points. This is the main termination criteria. bool duplicate = false; for (int i = 0; i < saveCount; ++i) { if (vertex.IndexA == saveA[i] && vertex.IndexB == saveB[i]) { duplicate = true; break; } } // If we found a duplicate support point we must exit to avoid cycling. if (duplicate) { break; } // New vertex is ok and needed. ++simplex.Count; } // Prepare output. simplex.GetWitnessPoints(out output.PointA, out output.PointB); output.Distance = (output.PointA - output.PointB).Length; output.Iterations = iter; // Cache the simplex. simplex.WriteCache(ref cache); // Apply radii if requested. if (input.UseRadii) { float rA = input.ProxyA.Radius; float rB = input.ProxyB.Radius; if (output.Distance > rA + rB && output.Distance > float.Epsilon) { // Shapes are still no overlapped. // Move the witness points to the outer surface. output.Distance -= rA + rB; Vector2 normal = output.PointB - output.PointA; normal = normal.Normalized; output.PointA += normal * rA; output.PointB -= normal * rB; } else { // Shapes are overlapped when radii are considered. // Move the witness points to the middle. Vector2 p = (output.PointA + output.PointB) * 0.5f; output.PointA = p; output.PointB = p; output.Distance = 0.0f; } } }
public static void ComputeDistance(ref DistanceInput input, out DistanceOutput output, out SimplexCache cache) { cache = new SimplexCache(); if (Settings.EnableDiagnostics) //Velcro: We only gather diagnostics when enabled { ++GJKCalls; } // Initialize the simplex. Simplex simplex = new Simplex(); simplex.ReadCache(ref cache, ref input.ProxyA, ref input.TransformA, ref input.ProxyB, ref input.TransformB); // These store the vertices of the last simplex so that we // can check for duplicates and prevent cycling. FixedArray3 <int> saveA = new FixedArray3 <int>(); FixedArray3 <int> saveB = new FixedArray3 <int>(); // Main iteration loop. int iter = 0; while (iter < Settings.MaxGJKIterations) { // Copy simplex so we can identify duplicates. int saveCount = simplex.Count; for (int i = 0; i < saveCount; ++i) { saveA[i] = simplex.V[i].IndexA; saveB[i] = simplex.V[i].IndexB; } switch (simplex.Count) { case 1: break; case 2: simplex.Solve2(); break; case 3: simplex.Solve3(); break; default: Debug.Assert(false); break; } // If we have 3 points, then the origin is in the corresponding triangle. if (simplex.Count == 3) { break; } // Get search direction. Vector2 d = simplex.GetSearchDirection(); // Ensure the search direction is numerically fit. if (d.LengthSquared() < MathConstants.Epsilon * MathConstants.Epsilon) { // The origin is probably contained by a line segment // or triangle. Thus the shapes are overlapped. // We can't return zero here even though there may be overlap. // In case the simplex is a point, segment, or triangle it is difficult // to determine if the origin is contained in the CSO or very close to it. break; } // Compute a tentative new simplex vertex using support points. SimplexVertex vertex = simplex.V[simplex.Count]; vertex.IndexA = input.ProxyA.GetSupport(MathUtils.MulT(input.TransformA.q, -d)); vertex.WA = MathUtils.Mul(ref input.TransformA, input.ProxyA.Vertices[vertex.IndexA]); vertex.IndexB = input.ProxyB.GetSupport(MathUtils.MulT(input.TransformB.q, d)); vertex.WB = MathUtils.Mul(ref input.TransformB, input.ProxyB.Vertices[vertex.IndexB]); vertex.W = vertex.WB - vertex.WA; simplex.V[simplex.Count] = vertex; // Iteration count is equated to the number of support point calls. ++iter; if (Settings.EnableDiagnostics) //Velcro: We only gather diagnostics when enabled { ++GJKIters; } // Check for duplicate support points. This is the main termination criteria. bool duplicate = false; for (int i = 0; i < saveCount; ++i) { if (vertex.IndexA == saveA[i] && vertex.IndexB == saveB[i]) { duplicate = true; break; } } // If we found a duplicate support point we must exit to avoid cycling. if (duplicate) { break; } // New vertex is OK and needed. ++simplex.Count; } if (Settings.EnableDiagnostics) //Velcro: We only gather diagnostics when enabled { GJKMaxIters = Math.Max(GJKMaxIters, iter); } // Prepare output. simplex.GetWitnessPoints(out output.PointA, out output.PointB); output.Distance = (output.PointA - output.PointB).Length(); output.Iterations = iter; // Cache the simplex. simplex.WriteCache(ref cache); // Apply radii if requested. if (input.UseRadii) { float rA = input.ProxyA.Radius; float rB = input.ProxyB.Radius; if (output.Distance > rA + rB && output.Distance > MathConstants.Epsilon) { // Shapes are still no overlapped. // Move the witness points to the outer surface. output.Distance -= rA + rB; Vector2 normal = output.PointB - output.PointA; normal.Normalize(); output.PointA += rA * normal; output.PointB -= rB * normal; } else { // Shapes are overlapped when radii are considered. // Move the witness points to the middle. Vector2 p = 0.5f * (output.PointA + output.PointB); output.PointA = p; output.PointB = p; output.Distance = 0.0f; } } }
// GJK-raycast // Algorithm by Gino van den Bergen. // "Smooth Mesh Contacts with GJK" in Game Physics Pearls. 2010 /// <summary> /// Perform a linear shape cast of shape B moving and shape A fixed. Determines the hit point, normal, and /// translation fraction. /// </summary> /// <returns>true if hit, false if there is no hit or an initial overlap</returns> public static bool ShapeCast(ref ShapeCastInput input, out ShapeCastOutput output) { output = new ShapeCastOutput(); output.Iterations = 0; output.Lambda = 1.0f; output.Normal = Vector2.Zero; output.Point = Vector2.Zero; DistanceProxy proxyA = input.ProxyA; DistanceProxy proxyB = input.ProxyB; float radiusA = MathUtils.Max(proxyA._radius, Settings.PolygonRadius); float radiusB = MathUtils.Max(proxyB._radius, Settings.PolygonRadius); float radius = radiusA + radiusB; Transform xfA = input.TransformA; Transform xfB = input.TransformB; Vector2 r = input.TranslationB; Vector2 n = new Vector2(0.0f, 0.0f); float lambda = 0.0f; // Initial simplex Simplex simplex = new Simplex(); simplex.Count = 0; // Get simplex vertices as an array. //SimplexVertex vertices = simplex.V.Value0; //Velcro: we don't need this as we have an indexer instead // Get support point in -r direction int indexA = proxyA.GetSupport(MathUtils.MulT(xfA.q, -r)); Vector2 wA = MathUtils.Mul(ref xfA, proxyA.GetVertex(indexA)); int indexB = proxyB.GetSupport(MathUtils.MulT(xfB.q, r)); Vector2 wB = MathUtils.Mul(ref xfB, proxyB.GetVertex(indexB)); Vector2 v = wA - wB; // Sigma is the target distance between polygons float sigma = MathUtils.Max(Settings.PolygonRadius, radius - Settings.PolygonRadius); float tolerance = 0.5f * Settings.LinearSlop; // Main iteration loop. int iter = 0; //Velcro: We have moved the max iterations into settings while (iter < Settings.MaxGJKIterations && v.Length() - sigma > tolerance) { Debug.Assert(simplex.Count < 3); output.Iterations += 1; // Support in direction -v (A - B) indexA = proxyA.GetSupport(MathUtils.MulT(xfA.q, -v)); wA = MathUtils.Mul(ref xfA, proxyA.GetVertex(indexA)); indexB = proxyB.GetSupport(MathUtils.MulT(xfB.q, v)); wB = MathUtils.Mul(ref xfB, proxyB.GetVertex(indexB)); Vector2 p = wA - wB; // -v is a normal at p v.Normalize(); // Intersect ray with plane float vp = MathUtils.Dot(ref v, ref p); float vr = MathUtils.Dot(ref v, ref r); if (vp - sigma > lambda * vr) { if (vr <= 0.0f) { return(false); } lambda = (vp - sigma) / vr; if (lambda > 1.0f) { return(false); } n = -v; simplex.Count = 0; } // Reverse simplex since it works with B - A. // Shift by lambda * r because we want the closest point to the current clip point. // Note that the support point p is not shifted because we want the plane equation // to be formed in unshifted space. SimplexVertex vertex = simplex.V[simplex.Count]; vertex.IndexA = indexB; vertex.WA = wB + lambda * r; vertex.IndexB = indexA; vertex.WB = wA; vertex.W = vertex.WB - vertex.WA; vertex.A = 1.0f; simplex.V[simplex.Count] = vertex; //Velcro: we have to copy the value back simplex.Count += 1; switch (simplex.Count) { case 1: break; case 2: simplex.Solve2(); break; case 3: simplex.Solve3(); break; default: Debug.Assert(false); break; } // If we have 3 points, then the origin is in the corresponding triangle. if (simplex.Count == 3) { // Overlap return(false); } // Get search direction. v = simplex.GetClosestPoint(); // Iteration count is equated to the number of support point calls. ++iter; } if (iter == 0) { // Initial overlap return(false); } // Prepare output. simplex.GetWitnessPoints(out _, out Vector2 pointB); if (v.LengthSquared() > 0.0f) { n = -v; n.Normalize(); } output.Point = pointB + radiusA * n; output.Normal = n; output.Lambda = lambda; output.Iterations = iter; return(true); }
public int indexB; // wB index #endregion Fields #region Methods public virtual void set_Renamed(SimplexVertex sv) { wA.set_Renamed(sv.wA); wB.set_Renamed(sv.wB); w.set_Renamed(sv.w); a = sv.a; indexA = sv.indexA; indexB = sv.indexB; }
public int IndexB; // wB index #endregion Fields #region Methods public void Set(SimplexVertex sv) { WA.Set(sv.WA); WB.Set(sv.WB); W.Set(sv.W); A = sv.A; IndexA = sv.IndexA; IndexB = sv.IndexB; }
public Vec2 wB = new Vec2(); // support point in shapeB #endregion Fields #region Methods public void set(SimplexVertex sv) { wA.set(sv.wA); wB.set(sv.wB); w.set(sv.w); a = sv.a; indexA = sv.indexA; indexB = sv.indexB; }
/// <summary> /// Compute the closest points between two shapes. Supports any combination of: CircleShape and /// PolygonShape. The simplex cache is input/output. On the first call set SimplexCache.count to /// zero. /// </summary> /// <param name="output"></param> /// <param name="cache"></param> /// <param name="input"></param> public void GetDistance(DistanceOutput output, SimplexCache cache, DistanceInput input) { GJK_CALLS++; DistanceProxy proxyA = input.ProxyA; DistanceProxy proxyB = input.ProxyB; Transform transformA = input.TransformA; Transform transformB = input.TransformB; // Initialize the simplex. simplex.ReadCache(cache, proxyA, transformA, proxyB, transformB); // Get simplex vertices as an array. SimplexVertex[] vertices = simplex.Vertices; // These store the vertices of the last simplex so that we // can check for duplicates and prevent cycling. // (pooled above) simplex.GetClosestPoint(closestPoint); float distanceSqr1 = closestPoint.LengthSquared(); // Main iteration loop int iter = 0; while (iter < GJK_MAX_ITERS) { // Copy simplex so we can identify duplicates. int saveCount = simplex.Count; for (int i = 0; i < saveCount; i++) { saveA[i] = vertices[i].IndexA; saveB[i] = vertices[i].IndexB; } switch (simplex.Count) { case 1: break; case 2: simplex.Solve2(); break; case 3: simplex.Solve3(); break; default: Debug.Assert(false); break; } // If we have 3 points, then the origin is in the corresponding triangle. if (simplex.Count == 3) { break; } // Compute closest point. simplex.GetClosestPoint(closestPoint); float distanceSqr2 = closestPoint.LengthSquared(); // ensure progress if (distanceSqr2 >= distanceSqr1) { // break; } distanceSqr1 = distanceSqr2; // get search direction; simplex.GetSearchDirection(d); // Ensure the search direction is numerically fit. if (d.LengthSquared() < Settings.EPSILON * Settings.EPSILON) { // The origin is probably contained by a line segment // or triangle. Thus the shapes are overlapped. // We can't return zero here even though there may be overlap. // In case the simplex is a point, segment, or triangle it is difficult // to determine if the origin is contained in the CSO or very close to it. break; } /* * SimplexVertex* vertex = vertices + simplex.m_count; vertex.indexA = * proxyA.GetSupport(MulT(transformA.R, -d)); vertex.wA = Mul(transformA, * proxyA.GetVertex(vertex.indexA)); Vec2 wBLocal; vertex.indexB = * proxyB.GetSupport(MulT(transformB.R, d)); vertex.wB = Mul(transformB, * proxyB.GetVertex(vertex.indexB)); vertex.w = vertex.wB - vertex.wA; */ // Compute a tentative new simplex vertex using support points. SimplexVertex vertex = vertices[simplex.Count]; Rot.MulTransUnsafe(transformA.Q, d.NegateLocal(), temp); vertex.IndexA = proxyA.GetSupport(temp); Transform.MulToOutUnsafe(transformA, proxyA.GetVertex(vertex.IndexA), vertex.WA); // Vec2 wBLocal; Rot.MulTransUnsafe(transformB.Q, d.NegateLocal(), temp); vertex.IndexB = proxyB.GetSupport(temp); Transform.MulToOutUnsafe(transformB, proxyB.GetVertex(vertex.IndexB), vertex.WB); vertex.W.Set(vertex.WB).SubLocal(vertex.WA); // Iteration count is equated to the number of support point calls. ++iter; ++GJK_ITERS; // Check for duplicate support points. This is the main termination criteria. bool duplicate = false; for (int i = 0; i < saveCount; ++i) { if (vertex.IndexA == saveA[i] && vertex.IndexB == saveB[i]) { duplicate = true; break; } } // If we found a duplicate support point we must exit to avoid cycling. if (duplicate) { break; } // New vertex is ok and needed. ++simplex.Count; } GJK_MAX_ITERS = MathUtils.Max(GJK_MAX_ITERS, iter); // Prepare output. simplex.GetWitnessPoints(output.PointA, output.PointB); output.Distance = MathUtils.Distance(output.PointA, output.PointB); output.Iterations = iter; // Cache the simplex. simplex.WriteCache(cache); // Apply radii if requested. if (input.UseRadii) { float rA = proxyA.Radius; float rB = proxyB.Radius; if (output.Distance > rA + rB && output.Distance > Settings.EPSILON) { // Shapes are still no overlapped. // Move the witness points to the outer surface. output.Distance -= (rA + rB); normal.Set(output.PointB).SubLocal(output.PointA); normal.Normalize(); temp.Set(normal).MulLocal(rA); output.PointA.AddLocal(temp); temp.Set(normal).MulLocal(rB); output.PointB.SubLocal(temp); } else { // Shapes are overlapped when radii are considered. // Move the witness points to the middle. // Vec2 p = 0.5f * (output.pointA + output.pointB); output.PointA.AddLocal(output.PointB).MulLocal(.5f); output.PointB.Set(output.PointA); output.Distance = 0.0f; } } }