Exemplo n.º 1
0
        // Sutherland-Hodgman clipping.
        public static int ClipSegmentToLine(out ClipVertex[] /*2*/ vOut, ClipVertex[] /*2*/ vIn, Vec2 normal, float offset)
        {
            vOut = new ClipVertex[2];

            // 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++] = vIn[0];
            }
            if (distance1 <= 0.0f)
            {
                vOut[numOut++] = 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].ID = distance0 > 0.0f ? vIn[0].ID : vIn[1].ID;
                ++numOut;
            }

            return(numOut);
        }
Exemplo n.º 2
0
        public static void FindIncidentEdge(out ClipVertex[] c,
                                            PolygonShape poly1, XForm xf1, int edge1, PolygonShape poly2, XForm xf2)
        {
            int count1 = poly1._vertexCount;

            Vec2[] normals1 = poly1._normals;

            int count2 = poly2._vertexCount;

            Vec2[] vertices2 = poly2._vertices;
            Vec2[] normals2  = poly2._normals;

            Box2DNetDebug.Assert(0 <= edge1 && edge1 < count1);

            // Get the normal of the reference edge in poly2's frame.
            Vec2 normal1 = Common.MathB2.MulT(xf2.R, Common.MathB2.Mul(xf1.R, normals1[edge1]));

            // Find the incident edge on poly2.
            int   index  = 0;
            float minDot = Settings.FLT_MAX;

            for (int i = 0; i < count2; ++i)
            {
                float dot = Vec2.Dot(normal1, normals2[i]);
                if (dot < minDot)
                {
                    minDot = dot;
                    index  = i;
                }
            }

            // Build the clip vertices for the incident edge.
            int i1 = index;
            int i2 = i1 + 1 < count2 ? i1 + 1 : 0;

            c = new ClipVertex[2];

            c[0].V = Common.MathB2.Mul(xf2, vertices2[i1]);
            c[0].ID.Features.ReferenceEdge  = (byte)edge1;
            c[0].ID.Features.IncidentEdge   = (byte)i1;
            c[0].ID.Features.IncidentVertex = 0;

            c[1].V = Common.MathB2.Mul(xf2, vertices2[i2]);
            c[1].ID.Features.ReferenceEdge  = (byte)edge1;
            c[1].ID.Features.IncidentEdge   = (byte)i2;
            c[1].ID.Features.IncidentVertex = 1;
        }