// Test between a polygon and a swept sphere with radius inRadius moving from inBegin to inBegin + inDelta
// If there is an intersection the intersection position is returned in outPoint and the center of the
// sphere is at inBegin + outFraction * inDelta when it collides
bool PolygonSweptSphereIntersect(const Plane &inPlane, const Vector2 *inVertices, int inNumVertices, const Vector3 &inBegin, const Vector3 &inDelta, float inRadius, Vector3 &outPoint, float &outFraction)
{
    // Determine the range over which the sphere intersects the plane
    float t1, t2;
    if (!PlaneSweptSphereIntersect(inPlane, inBegin, inDelta, inRadius, t1, t2))
        return false;

    // The radius of the circle is defined as: radius^2 = (sphere radius)^2 - (distance plane to center)^2
    // this can be written as: radius^2 = a * t^2 + b * t + c
    float n_dot_d = inPlane.mNormal.Dot(inDelta);
    float dist_to_b = inPlane.GetSignedDistance(inBegin);
    float a = -n_dot_d * n_dot_d;
    float b = -2.0f * n_dot_d * dist_to_b;
    float c = inRadius * inRadius - dist_to_b * dist_to_b;

    // Get basis
    Vector3 u, v;
    inPlane.GetBasisVectors(u, v);

    // Get begin and delta in plane space
    Vector2 begin = Plane::sConvertWorldToPlane(u, v, inBegin);
    Vector2 delta = Plane::sConvertWorldToPlane(u, v, inDelta);

    // Test if sphere intersects at t1
    Vector2 p;
    if (PolygonCircleIntersect(inVertices, inNumVertices, begin + delta * t1, a * t1 * t1 + b * t1 + c, p))
    {
        outFraction = t1;
        outPoint = inPlane.ConvertPlaneToWorld(u, v, p);
        return true;
    }

    // Test if sphere intersects with one of the edges or vertices
    if (SweptCircleEdgeVertexIntersect(inVertices, inNumVertices, begin, delta, a, b, c, p, outFraction))
    {
        outPoint = inPlane.ConvertPlaneToWorld(u, v, p);
        return true;
    }

    return false;
}
// Test intersection between a plane inPlane and a swept sphere with radius inRadius moving from inBegin to inBegin + inDelta
// If there is an intersection the function returns true and the intersection range is from
// inBegin + outT1 * inDelta to inBegin + outT2 * inDelta
bool PlaneSweptSphereIntersect(const Plane &inPlane, const Vector3 &inBegin, const Vector3 &inDelta, float inRadius, float &outT1, float &outT2)
{
    // If the center of the sphere moves like: center = inBegin + t * inDelta for t e [0, 1]
    // then the sphere intersects the plane if: -R <= distance plane to center <= R
    float n_dot_d = inPlane.mNormal.Dot(inDelta);
    float dist_to_b = inPlane.GetSignedDistance(inBegin);
    if (n_dot_d == 0.0f)
    {
        // The sphere is moving nearly parallel to the plane, check if the distance
        // is smaller than the radius
        if (Abs(dist_to_b) > inRadius)
            return false;

        // Intersection on the entire range
        outT1 = 0.0f;
        outT2 = 1.0f;
    }
    else
    {
        // Determine interval of intersection
        outT1 = (inRadius - dist_to_b) / n_dot_d;
        outT2 = (-inRadius - dist_to_b) / n_dot_d;

        // Order the results
        if (outT1 > outT2)
            Swap(outT1, outT2);

        // Early out if no hit possible
        if (outT1 > 1.0f || outT2 < 0.0f)
            return false;

        // Clamp it to the range [0, 1], the range of the swept sphere
        if (outT1 < 0.0f) outT1 = 0.0f;
        if (outT2 > 1.0f) outT2 = 1.0f;
    }

    return true;
}