Example #1
0
 // -----------------------
 // --- Package Methods ---
 // -----------------------
 public void Print()
 {
     for (int i = 0; i < m_List.Count; i++)
     {
         IPoly p = GetInnerPoly(i);
         Console.WriteLine("InnerPoly(" + i + ").hole=" + p.IsHole());
         for (int j = 0; j < p.PointCount; j++)
         {
             Console.WriteLine(p.GetX(j) + "  " + p.GetY(j));
         }
     }
 }
Example #2
0
        /// <summary>
        /// Recursively process the polygon to attempt to find a
        /// single polygon element that could be added to the physics
        /// layer without any holes or additional physics.
        /// </summary>
        private void CreateJunctionPhysics(
			int depth, IPoly poly, RectangleF bounds)
        {
            // Ignore empty polygons
            if (poly.InnerPolygonCount == 0)
                return;

            // See if we are a solid polygon
            double areaDifference = bounds.Width * bounds.Height - poly.Area;

            if (poly.InnerPolygonCount == 1)
            {
                if (poly.IsHole())
                    // Don't add holes
                    return;

                if (poly.PointCount == 4 && areaDifference <= 0.1f)
                    // We appear to be at least mostly solid, drop it
                    return;
            }

            // If we have more than one polygon, split it
            if (poly.InnerPolygonCount > 1 ||
                bounds.Width > Constants.MaximumJunctionPhysicsBlock ||
                bounds.Height > Constants.MaximumJunctionPhysicsBlock)
            {
                // We split the polygon into quads and process each
                // one to add it recursively.
                CreateJunctionPhysics(depth + 1, poly, bounds, 0, 0);
                CreateJunctionPhysics(depth + 1, poly, bounds, 0, 1);
                CreateJunctionPhysics(depth + 1, poly, bounds, 1, 1);
                CreateJunctionPhysics(depth + 1, poly, bounds, 1, 0);
                return;
            }

            // We should never get a hole
            if (poly.IsHole())
            {
                // We shouldn't get this
                Log.Error("Got a top-level polygon hole");
                return;
            }

            // Create a polygon shape as vectors
            LinkedList<Vector2D> vectors = new LinkedList<Vector2D>();

            for (int i = 0; i < poly.PointCount; i++)
            {
                // Get the coordinates
                float x = (float) poly.GetX(i);
                float y = (float) poly.GetY(i);

                // Create the vector
                vectors.Add(new Vector2D(x, y));
            }

            // Convert it into a physics2d polygon shape. Making the
            // PolygonShape second parameter too small makes the game
            // basically unusable in terms of stage generation but
            // more accurate for impacts with the side.
            Vector2D [] array = vectors.ToArray();
            IShape ps = new PolygonShape(array, 5f);
            physicsShapes.Add(ps);
        }
        /// <summary>
        /// Renders an arbitrary polygon to the context.
        /// </summary>
        private void RenderPolygon(
			Context g, IPoly poly, PointF point, Color color)
        {
            // Save the first point
            PointF firstPoint = PointF.Empty;

            // Go through the points of the polygon
            for (int i = 0; i < poly.PointCount; i++)
            {
                // Pull out the coordinates
                float x = (float) poly.GetX(i);
                float y = (float) poly.GetY(i);
                PointF p = new PointF(cx + point.X + x, cy + point.Y + y);

                // Either move or line
                if (firstPoint == PointF.Empty)
                {
                    firstPoint = p;
                    g.MoveTo(p.X, p.Y);
                }
                else
                {
                    g.LineTo(p.X, p.Y);
                }
            }

            // Finish up
            g.LineTo(firstPoint.X, firstPoint.Y);
            g.Color = color;
            g.Fill();
        }