private ConvexHull2Test()
        {
            _pointCloud1 = new Vertices(PointCount);

            for (int i = 0; i < PointCount; i++)
            {
                float x = Rand.RandomFloat(-10, 10);
                float y = Rand.RandomFloat(-10, 10);

                _pointCloud1.Add(new Vector2(x, y));
            }

            _pointCloud2 = new Vertices(_pointCloud1);
            _pointCloud3 = new Vertices(_pointCloud1);

            //Melkman DOES NOT work on point clouds. It only works on simple polygons.
            _pointCloud1.Translate(new Vector2(-20, 30));
            _melkman = Melkman.GetConvexHull(_pointCloud1);

            //Giftwrap works on point clouds
            _pointCloud2.Translate(new Vector2(20, 30));
            _giftWrap = GiftWrap.GetConvexHull(_pointCloud2);

            //Chain hull also works on point clouds
            _pointCloud3.Translate(new Vector2(20, 10));
            _chainHull = ChainHull.GetConvexHull(_pointCloud3);
        }
Example #2
0
        private void canvas_MouseUp(object sender, MouseEventArgs e)
        {
            List <Vertices> _verts;
            Vertices        vrts = new Vertices();

            for (int i = 0; i < v.Count; i++)
            {
                vrts.Add(new Vector2((float)v[i].x, (float)v[i].y));
            }

            try
            {
                _verts = FarseerPhysics.Common.Decomposition.Triangulate.ConvexPartition(vrts, FarseerPhysics.Common.Decomposition.TriangulationAlgorithm.Bayazit);

                if (_verts.Count == 0)
                {
                    Vertices hull = Melkman.GetConvexHull(vrts);

                    v.Clear();

                    for (int i = 0; i < hull.Count; i++)
                    {
                        v.Add(new Vector(hull[i].X, hull[i].Y));
                    }
                }
            }
            catch
            {
                try
                {
                    Vertices hull = Melkman.GetConvexHull(vrts);

                    v.Clear();

                    for (int i = 0; i < hull.Count; i++)
                    {
                        v.Add(new Vector(hull[i].X, hull[i].Y));
                    }
                }
                catch
                {
                    v.Clear();
                }
            }
        }
        public static Fixture CreatePolygon(Texture2D texture, Vector2 scaling, BodyType bodyType, Vector2 position, float density)
        {
            uint[] data = new uint[texture.Width * texture.Height];
            texture.GetData(data);
            Vertices vertices      = PolygonTools.CreatePolygon(data, texture.Width, texture.Height, true);
            var      polygonOffset = new Vector2(-texture.Width / 2, -texture.Height / 2);

            vertices.Translate(ref polygonOffset);
            Vector2 scale = new Vector2(0.01f, 0.01f) * scaling;

            vertices.Scale(ref scale);

            Vertices temp    = Melkman.GetConvexHull(vertices);
            Fixture  fixture = FixtureFactory.CreatePolygon(Level.Physics, temp, density);

            fixture.Body.BodyType = bodyType;
            fixture.Body.Position = ToMeter(position);
            return(fixture);
        }
        // Anything that is a fixture must have an object type for collision logic purposes

        /*public enum ObjectTypes
         * {
         *  Bot = 1,
         *  Weapon = 2,  // Since weapons will be fixtures, they too need an object type.
         *  Bullet = 3,
         *  Wall = 4
         * }*/

        public SPRWorld(World world, int currentLevel)
        {
            int botHalfWidth = 31; // Half the bot's width (e.g. the distance from the centroid to the edge)

            m_World         = world;
            this.m_Entities = new SortedDictionary <ulong, Entity>();

            // Make polygons out of weapons and anything that needs collision.
            // NOTE: Stores the convex hull for each item, since collision detection
            //          relies upon these verticies being convex polygons.
            String[] toPreload = new String[] { "Gun", "Axe", "Shield" };

            foreach (String texture in toPreload)
            {
                Texture2D a    = TextureStatic.Get(texture);
                uint[]    data = new uint[a.Width * a.Height];
                a.GetData <uint>(data);
                Vertices v     = Melkman.GetConvexHull(PolygonTools.CreatePolygon(data, a.Width, a.Height));
                Vector2  scale = new Vector2(Settings.MetersPerPixel, Settings.MetersPerPixel);
                v.Scale(ref scale);
                if (!computedSpritePolygons.ContainsKey(texture))
                {
                    computedSpritePolygons.Add(texture, v);
                }
            }

            //walls
            Vector2[] outer = { new Vector2(0, 0) * Settings.MetersPerPixel, new Vector2(0, 1920) * Settings.MetersPerPixel, new Vector2(1080, 1920) * Settings.MetersPerPixel, new Vector2(1080, 0) * Settings.MetersPerPixel };
            Vector2[] inner = { new Vector2(200, 200) * Settings.MetersPerPixel, new Vector2(200, 1720) * Settings.MetersPerPixel, new Vector2(880, 1720) * Settings.MetersPerPixel, new Vector2(880, 200) * Settings.MetersPerPixel };

            FixtureFactory.CreateRectangle(world, 1920 * Settings.MetersPerPixel, 200 * Settings.MetersPerPixel, 1f, new Vector2(960, 100) * Settings.MetersPerPixel, "Wall").Body.BodyType  = BodyType.Static;
            FixtureFactory.CreateRectangle(world, 200 * Settings.MetersPerPixel, 1080 * Settings.MetersPerPixel, 1f, new Vector2(100, 540) * Settings.MetersPerPixel, "Wall").Body.BodyType  = BodyType.Static;
            FixtureFactory.CreateRectangle(world, 1920 * Settings.MetersPerPixel, 200 * Settings.MetersPerPixel, 1f, new Vector2(960, 980) * Settings.MetersPerPixel, "Wall").Body.BodyType  = BodyType.Static;
            FixtureFactory.CreateRectangle(world, 200 * Settings.MetersPerPixel, 1080 * Settings.MetersPerPixel, 1f, new Vector2(1820, 540) * Settings.MetersPerPixel, "Wall").Body.BodyType = BodyType.Static;

            Vector2[] edges = { new Vector2(-botHalfWidth, -botHalfWidth) * Settings.MetersPerPixel, new Vector2(botHalfWidth, -botHalfWidth) * Settings.MetersPerPixel, new Vector2(botHalfWidth, botHalfWidth) * Settings.MetersPerPixel, new Vector2(-botHalfWidth, botHalfWidth) * Settings.MetersPerPixel };

            this.battle = new Battle(this, botHalfWidth, world, currentLevel);
            GameWorld.audio.SongPlay("sprbattle");
        }