Box2D.Collision.Collision.ClipSegmentToLine C# (CSharp) Method

ClipSegmentToLine() public static method

Clipping for contact manifolds. Sutherland-Hodgman clipping.
public static ClipSegmentToLine ( ClipVertex vOut, ClipVertex vIn, Vec2 normal, float offset, int vertexIndexA ) : int
vOut ClipVertex
vIn ClipVertex
normal Box2D.Common.Vec2
offset float
vertexIndexA int
return int
        public static int ClipSegmentToLine(ClipVertex[] vOut, ClipVertex[] vIn, Vec2 normal, float offset, int vertexIndexA)
        {
            // Start with no output points
            int numOut = 0;

            // Calculate the distance of end points to the line
            float distance0 = Vec2.Dot(normal, vIn[0].V) - offset;
            float distance1 = Vec2.Dot(normal, vIn[1].V) - offset;

            // If the points are behind the plane
            if (distance0 <= 0.0f)
            {
                vOut[numOut++].Set(vIn[0]);
            }
            if (distance1 <= 0.0f)
            {
                vOut[numOut++].Set(vIn[1]);
            }

            // If the points are on different sides of the plane
            if (distance0 * distance1 < 0.0f)
            {
                // Find intersection point of edge and plane
                float interp = distance0 / (distance0 - distance1);
                // vOut[numOut].v = vIn[0].v + interp * (vIn[1].v - vIn[0].v);
                vOut[numOut].V.Set(vIn[1].V).SubLocal(vIn[0].V).MulLocal(interp).AddLocal(vIn[0].V);

                // VertexA is hitting edgeB.
                vOut[numOut].Id.IndexA = (sbyte)vertexIndexA;
                vOut[numOut].Id.IndexB = vIn[0].Id.IndexB;
                vOut[numOut].Id.TypeA = (sbyte)ContactID.Type.Vertex;
                vOut[numOut].Id.TypeB = (sbyte)ContactID.Type.Face;
                ++numOut;
            }

            return numOut;
        }