} //testPolygonVsPolygon /** Internal api - test a ray against a circle */ public static bool TestRayVsCircle(Ray ray, Circle circle, out RayCollision rayCollision) { Vector2 delta = ray.End - ray.Start; Vector2 ray2Circle = ray.Start - circle.Position; float a = delta.LengthSquared(); float b = 2 * Vector2.Dot(delta, ray2Circle); float c = Vector2.Dot(ray2Circle, ray2Circle) - (circle.Radius * circle.Radius); float d = b * b - 4 * a * c; if (d >= 0) { d = (float)System.Math.Sqrt(d); float t1 = (-b - d) / (2 * a); float t2 = (-b + d) / (2 * a); if (ray.IsInfinite || (t1 <= 1.0 && t1 >= 0.0)) { rayCollision = new RayCollision { Shape = circle, Ray = ray, Start = t1, End = t2 }; return(true); } // } //d >= 0 rayCollision = new RayCollision(); return(false); }
/** Internal api - test a ray against a polygon */ public static bool TestRayVsPolygon(Ray ray, Polygon polygon, out RayCollision rayCollision) { float minU = float.PositiveInfinity; float maxU = 0.0f; float startX = ray.Start.X; float startY = ray.Start.Y; float deltaX = ray.End.X - startX; float deltaY = ray.End.Y - startY; var verts = polygon.TransformedVertices; Vector2 v1 = verts[^ 1];