public bool TestSegment(out float lambda, out Vec2 normal, Segment segment, float maxLambda)
{
lambda = 0f;
normal = new Vec2();
Vec2 s = segment.P1;
Vec2 r = segment.P2 - s;
Vec2 d = P2 - P1;
Vec2 n = Vec2.Cross(d, 1.0f);
float k_slop = 100.0f * Common.Settings.FLT_EPSILON;
float denom = -Vec2.Dot(r, n);
// Cull back facing collision and ignore parallel segments.
if (denom > k_slop)
{
// Does the segment intersect the infinite line associated with this segment?
Vec2 b = s - P1;
float a = Vec2.Dot(b, n);
if (0.0f <= a && a <= maxLambda * denom)
{
float mu2 = -r.X * b.Y + r.Y * b.X;
// Does the segment intersect this segment?
if (-k_slop * denom <= mu2 && mu2 <= denom * (1.0f + k_slop))
{
a /= denom;
n.Normalize();
lambda = a;
normal = n;
return true;
}
}
}
return false;
}