Beispiel #1
0
        /// <summary>
        /// Creates a new Polygon Instance.
        /// </summary>
        /// <param name="vertexes">the vertexes that make up the shape of the Polygon</param>
        /// <param name="gridSpacing">
        /// How large a grid cell is. Usualy you will want at least 2 cells between major vertexes.
        /// The smaller this is the better precision you get, but higher cost in memory.
        /// The larger the less precision and if it's to high collision detection may fail completely.
        public PolygonShape(Vector2D[] vertexes, Scalar gridSpacing)
        {
            if (vertexes == null)
            {
                throw new ArgumentNullException("vertexes");
            }
            if (vertexes.Length < 3)
            {
                throw new ArgumentException("too few", "vertexes");
            }
            if (gridSpacing <= 0)
            {
                throw new ArgumentOutOfRangeException("gridSpacing");
            }
            this.vertexes      = vertexes;
            this.grid          = new DistanceGrid(this, gridSpacing);
            this.vertexNormals = VertexHelper.GetVertexNormals(this.vertexes);
            VertexInfo info = VertexHelper.GetVertexInfo(vertexes);

            this.inertia  = info.Inertia;
            this.centroid = info.Centroid;
            this.area     = info.Area;
        }
        public MultiPolygonShape(Vector2D[][] polygons, Scalar gridSpacing)
        {
            if (gridSpacing <= 0)
            {
                throw new ArgumentOutOfRangeException("gridSpacing");
            }
            if (polygons == null)
            {
                throw new ArgumentNullException("polygons");
            }
            if (polygons.Length == 0)
            {
                throw new ArgumentOutOfRangeException("polygons");
            }
            this.polygons      = polygons;
            this.vertexes      = ConcatVertexes(polygons);
            this.vertexNormals = CalculateNormals();
            this.grid          = new DistanceGrid(this, gridSpacing);
            VertexInfo info = VertexHelper.GetVertexInfoOfRange(polygons);

            this.inertia  = info.Inertia;
            this.centroid = info.Centroid;
            this.area     = info.Area;
        }