예제 #1
0
        public static BreakableBody CreateBreakableBody(World world, Vertices vertices, float density)
        {
            List <Vertices> triangles;

            if (vertices.Count <= 10)
            {
                triangles = DelauneyTriangulate(vertices);
            }
            else
            {
                if (rand.RandomBoolean)
                {
                    triangles = DelauneyTriangulate(vertices);
                }
                else
                {
                    triangles = EarclipDecomposer.ConvexPartition(vertices);
                }
            }

            BreakableBody breakableBody = new BreakableBody(triangles, world, density);

            world.AddBreakableBody(breakableBody);

            return(breakableBody);
        }
예제 #2
0
        public override Tuple <IEnumerable <Fixture>, Body, object> Build(World world)
        {
            BreakableBody breakableBody = CreateBreakableBody(world, this.Vertices, this.Density);

            this.InitBody(breakableBody.MainBody);

            return(Tuple.Create <IEnumerable <Fixture>, Body, object>(breakableBody.Parts, breakableBody.MainBody, breakableBody));
        }
예제 #3
0
        public static BreakableBody CreateBreakableBody(World world, IEnumerable <Shape> shapes, Vector2 position = new Vector2(), GGame.Math.Fix64 rotation = new Fix64())
        {
            BreakableBody breakableBody = new BreakableBody(world, shapes, position, rotation);

            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);
            return(breakableBody);
        }
예제 #4
0
        public static BreakableBody CreateBreakableBody(World world, IEnumerable <Shape> shapes, Vector2 position = new Vector2(), float rotation = 0)
        {
            var breakableBody = new BreakableBody(world, shapes, position, rotation);

            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);
            return(breakableBody);
        }
예제 #5
0
        public IList <Texture2D> BreakableTextureFragments(BreakableBody body, string textureName)
        {
            List <Texture2D> result = new List <Texture2D>();

            Vector2 scale = ConvertUnits.ToDisplayUnits(Vector2.One);

            foreach (Fixture f in body.Parts)
            {
                Vertices v = null;
                if (f.Shape is PolygonShape polygonShape)
                {
                    v = new Vertices(polygonShape.Vertices);
                    v.Scale(ref scale);
                }

                if (v != null)
                {
                    AABB            polygonBounds = v.GetAABB();
                    List <Vertices> decomposedVertices;
                    if (!v.IsConvex())
                    {
                        decomposedVertices = Triangulate.ConvexPartition(v, TriangulationAlgorithm.Bayazit);
                    }
                    else
                    {
                        decomposedVertices = new List <Vertices>();
                        decomposedVertices.Add(v);
                    }

                    List <VertexPositionColorTexture[]> verticesFill = new List <VertexPositionColorTexture[]>(decomposedVertices.Count);
                    for (int i = 0; i < decomposedVertices.Count; i++)
                    {
                        verticesFill.Add(new VertexPositionColorTexture[3 * (decomposedVertices[i].Count - 2)]);
                        for (int j = 0; j < decomposedVertices[i].Count - 2; j++)
                        {
                            // fill vertices
                            verticesFill[i][3 * j].Position     = new Vector3(decomposedVertices[i][0] - polygonBounds.Center, 0f);
                            verticesFill[i][3 * j + 1].Position = new Vector3(decomposedVertices[i].NextVertex(j) - polygonBounds.Center, 0f);
                            verticesFill[i][3 * j + 2].Position = new Vector3(decomposedVertices[i].NextVertex(j + 1) - polygonBounds.Center, 0f);

                            verticesFill[i][3 * j].TextureCoordinate     = new Vector2(decomposedVertices[i][0].X / _textureList[textureName].Width, decomposedVertices[i][0].Y / _textureList[textureName].Height - 1f);
                            verticesFill[i][3 * j + 1].TextureCoordinate = new Vector2(decomposedVertices[i].NextVertex(j).X / _textureList[textureName].Width, decomposedVertices[i].NextVertex(j).Y / _textureList[textureName].Height - 1f);
                            verticesFill[i][3 * j + 2].TextureCoordinate = new Vector2(decomposedVertices[i].NextVertex(j + 1).X / _textureList[textureName].Width, decomposedVertices[i].NextVertex(j + 1).Y / _textureList[textureName].Height - 1f);
                            verticesFill[i][3 * j].Color = verticesFill[i][3 * j + 1].Color = verticesFill[i][3 * j + 2].Color = Color.Transparent;
                        }
                    }

                    Vector2 vertsSize = new Vector2(polygonBounds.UpperBound.X - polygonBounds.LowerBound.X, polygonBounds.UpperBound.Y - polygonBounds.LowerBound.Y);
                    result.Add(RenderTexture((int)vertsSize.X, (int)vertsSize.Y, _textureList.ContainsKey(textureName) ? _textureList[textureName] : null, Color.White, verticesFill, Array.Empty <VertexPositionColor>()));
                }
                else
                {
                    result.Add(_textureList["Blank"]);
                }
            }

            return(result);
        }
예제 #6
0
        public override void LoadContent()
        {
            base.LoadContent();

            DebugView.AppendFlags(DebugViewFlags.Shape);

            World.Gravity = Vector2.Zero;

            _border          = new Border(World, ScreenManager, Camera);
            _breakableBodies = new List <BreakableBody>();

            Texture2D alphabet = ScreenManager.Content.Load <Texture2D>("Samples/alphabet");

            uint[] data = new uint[alphabet.Width * alphabet.Height];
            alphabet.GetData(data);

            List <Vertices> list = PolygonTools.CreatePolygon(data, alphabet.Width, 3.5f, 20, true, true);

            for (int i = 0; i < list.Count; i++)
            {
                list[i].Scale(new Vector2(1f, -1f)); // flip Vert
            }
            float yOffset = -5f;
            float xOffset = -14f;

            for (int i = 0; i < list.Count; i++)
            {
                if (i == 9)
                {
                    yOffset = 0f;
                    xOffset = -14f;
                }
                if (i == 18)
                {
                    yOffset = 5f;
                    xOffset = -12.25f;
                }
                Vertices polygon  = list[i];
                Vector2  centroid = -polygon.GetCentroid();
                polygon.Translate(ref centroid);
                polygon = SimplifyTools.CollinearSimplify(polygon);
                polygon = SimplifyTools.ReduceByDistance(polygon, 4);
                List <Vertices> triangulated = Triangulate.ConvexPartition(polygon, TriangulationAlgorithm.Bayazit);

                Vector2 vertScale = new Vector2(13.916667f, 23.25f) / new Vector2(alphabet.Width, alphabet.Height);
                foreach (Vertices vertices in triangulated)
                {
                    vertices.Scale(ref vertScale);
                }

                var breakableBody = new BreakableBody(World, triangulated, 1);
                breakableBody.MainBody.Position = new Vector2(xOffset, yOffset);
                breakableBody.Strength          = 100;
                _breakableBodies.Add(breakableBody);

                xOffset += 3.5f;
            }
        }
        public override void LoadContent()
        {
            base.LoadContent();

            DebugView.AppendFlags(DebugViewFlags.Shape);

            World.Gravity = Vector2.Zero;

            _border = new Border(World, ScreenManager, Camera);

            Texture2D alphabet = ScreenManager.Content.Load <Texture2D>("Samples/alphabet");

            uint[] data = new uint[alphabet.Width * alphabet.Height];
            alphabet.GetData(data);

            List <Vertices> list = PolygonTools.CreatePolygon(data, alphabet.Width, 3.5f, 20, true, true);

            float yOffset = -5f;
            float xOffset = -14f;

            for (int i = 0; i < list.Count; i++)
            {
                if (i == 9)
                {
                    yOffset = 0f;
                    xOffset = -14f;
                }
                if (i == 18)
                {
                    yOffset = 5f;
                    xOffset = -12.25f;
                }
                Vertices polygon  = list[i];
                Vector2  centroid = -polygon.GetCentroid();
                polygon.Translate(ref centroid);
                polygon = SimplifyTools.CollinearSimplify(polygon);
                polygon = SimplifyTools.ReduceByDistance(polygon, 4);
                List <Vertices> triangulated = Triangulate.ConvexPartition(polygon, TriangulationAlgorithm.Bayazit);

#if WINDOWS_PHONE
                const float scale = 0.6f;
#else
                const float scale = 1f;
#endif
                Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * scale;
                foreach (Vertices vertices in triangulated)
                {
                    vertices.Scale(ref vertScale);
                }

                BreakableBody breakableBody = new BreakableBody(triangulated, World, 1);
                breakableBody.MainBody.Position = new Vector2(xOffset, yOffset);
                breakableBody.Strength          = 100;
                World.AddBreakableBody(breakableBody);

                xOffset += 3.5f;
            }
        }
예제 #8
0
        public static BreakableBody CreateBreakableBody(World world, IEnumerable <Shape> shapes, Vector2 position)
        {
            BreakableBody breakableBody = new BreakableBody(shapes, world);

            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);

            return(breakableBody);
        }
예제 #9
0
        public static BreakableBody CreateBreakableBody(World world, Vertices vertices, GGame.Math.Fix64 density, Vector2 position = new Vector2(), GGame.Math.Fix64 rotation = new Fix64())
        {
            //TODO: Implement a Voronoi diagram algorithm to split up the vertices
            List <Vertices> triangles = Triangulate.ConvexPartition(vertices, TriangulationAlgorithm.Earclip, true, 0.001f);

            BreakableBody breakableBody = new BreakableBody(world, triangles, density, position, rotation);

            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);
            return(breakableBody);
        }
예제 #10
0
        /// <summary>
        /// Creates a breakable body. You would want to remove collinear points before using this.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="vertices">The vertices.</param>
        /// <param name="density">The density.</param>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public static BreakableBody CreateBreakableBody(World world, Vertices vertices, float density, Vector2 position)
        {
            List <Vertices> triangles = Triangulate.ConvexPartition(vertices, TriangulationAlgorithm.Earclip);

            BreakableBody breakableBody = new BreakableBody(triangles, world, density);

            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);

            return(breakableBody);
        }
예제 #11
0
        public static BreakableBody CreateBreakableBody(World world, Vertices vertices, float density, Vector2 position = new Vector2(), float rotation = 0)
        {
            //TODO: Implement a Voronoi diagram algorithm to split up the vertices
            var triangles = Triangulate.ConvexPartition(vertices, TriangulationAlgorithm.Earclip);

            var breakableBody = new BreakableBody(world, triangles, density, position, rotation);

            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);
            return(breakableBody);
        }
예제 #12
0
        /// <summary>
        /// Creates a breakable body. You would want to remove collinear points before using this.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="vertices">The vertices.</param>
        /// <param name="density">The density.</param>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public static BreakableBody CreateBreakableBody(World world, Vertices vertices, float density, Vector2 position,
                                                        object userData)
        {
            List <Vertices> triangles = EarclipDecomposer.ConvexPartition(vertices);

            BreakableBody breakableBody = new BreakableBody(triangles, world, density, userData);

            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);

            return(breakableBody);
        }
예제 #13
0
        public BreakableBody CreateBreakable(World world)
        {
            List <Shape> shapes = new List <Shape>();

            foreach (FixtureTemplate f in Fixtures)
            {
                shapes.Add(f.Shape);
            }

            BreakableBody body = new BreakableBody(world, shapes);

            world.AddBreakableBody(body);

            return(body);
        }
예제 #14
0
        public override void Initialize()
        {
            //load texture that will represent the physics body
            Texture2D polygonTexture = GameInstance.Content.Load <Texture2D>("Rock");

            //Create an array to hold the data from the texture
            uint[] data = new uint[polygonTexture.Width * polygonTexture.Height];

            //Transfer the texture data to the array
            polygonTexture.GetData(data);

            Vertices verts = PolygonTools.CreatePolygon(data, polygonTexture.Width);
            Vector2  scale = new Vector2(0.07f, 0.07f);

            verts.Scale(ref scale);

            _breakableBody = new BreakableBody(World, verts, 50, new Vector2(-10, 25));

            base.Initialize();
        }
예제 #15
0
        } // _calcSize(vertices)

        #endregion

        #region CreateBodyByList
        /// <summary>
        /// Creates a breakable Body from a list of Vertices
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private Body CreateBodyByList(List <Vertices> list)
        {
            // if we already have a breakable body, clear it up!
            if (_breakableBody != null)
            {
                PhysicsGameScreen.World.BreakableBodyList.Remove(_breakableBody);
                _breakableBody.MainBody.Dispose();
                _breakableBody = null;
            }

            // this object is not broken initially
            _broken                      = false;
            _breakableBody               = new BreakableBody(list, PhysicsGameScreen.World, 10);
            _breakableBody.Strength      = float.MaxValue;              // don't break when falling down, we use hitpoints!
            _breakableBody.MainBody.Mass = 10000f;
            PhysicsGameScreen.World.AddBreakableBody(_breakableBody);



            return(_breakableBody.MainBody);
        } // CreateBodyByList(list)
        private BreakableBody CreateBreakable(List <VerticesExt> ext)
        {
            List <PolygonShape> polygons = new List <PolygonShape>();

            foreach (VerticesExt ve in ext)
            {
                Vertices simple = SimplifyTools.DouglasPeuckerSimplify(ve, 0.1f);

                List <Vertices> list = Triangulate.ConvexPartition(simple, TriangulationAlgorithm.Bayazit);

                foreach (Vertices v in list)
                {
                    PolygonShape s = new PolygonShape(v, 1f);
                    polygons.Add(s);
                }
            }

            BreakableBody body = new BreakableBody(World, polygons);

            World.AddBreakableBody(body);

            return(body);
        }
예제 #17
0
        bool SFXPlayed(BreakableBody.BodyMaterial material)
        {
            switch (material)
            {
                case BreakableBody.BodyMaterial.CONCRETE:
                    return cementSFX;
                case BreakableBody.BodyMaterial.WOOD:
                    return woodSFX;
                case BreakableBody.BodyMaterial.GLASS:
                    return glassSFX;
            }

            return true;
        }
        public override void LoadContent()
        {
            base.LoadContent();

            World.Gravity = Vector2.Zero;

            _border          = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
            _breakableObject = new List <List <Vertices> >();

            Texture2D alphabet = ScreenManager.Content.Load <Texture2D>("Samples/alphabet");

            uint[] data = new uint[alphabet.Width * alphabet.Height];
            alphabet.GetData(data);

            List <Vertices> list = PolygonTools.CreatePolygon(data, alphabet.Width, 3.5f, 20, true, true);

            float yOffset = -5f;
            float xOffset = -14f;

            for (int i = 0; i < list.Count; i++)
            {
                if (i == 9)
                {
                    yOffset = 0f;
                    xOffset = -14f;
                }
                if (i == 18)
                {
                    yOffset = 5f;
                    xOffset = -12.25f;
                }
                Vertices polygon  = list[i];
                Vector2  centroid = -polygon.GetCentroid();
                polygon.Translate(ref centroid);
                //polygon = SimplifyTools.CollinearSimplify(polygon); // this breaks the split hole function
                polygon = SimplifyTools.ReduceByDistance(polygon, 4);
                List <Vertices> triangulated = BayazitDecomposer.ConvexPartition(polygon);

#if WINDOWS_PHONE
                const float scale = 0.6f;
#else
                const float scale = 1f;
#endif
                Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * scale;
                foreach (Vertices vertices in triangulated)
                {
                    vertices.Scale(ref vertScale);
                }

                BreakableBody breakableBody = new BreakableBody(triangulated, World, 1);
                breakableBody.MainBody.Position = new Vector2(xOffset, yOffset);
                breakableBody.Strength          = 100;
                breakableBody.MainBody.UserData = i;
                World.AddBreakableBody(breakableBody);

                polygon.Scale(ref vertScale);
                _breakableObject.Add(polygon.SplitAtHoles());

                xOffset += 3.5f;
            }
        }
예제 #19
0
 void SFXPlayed(BreakableBody.BodyMaterial material, bool hit)
 {
     switch (material)
     {
         case BreakableBody.BodyMaterial.CONCRETE:
             cementSFX = hit;
             cementTime = timeToWaitForSFX;
             break;
         case BreakableBody.BodyMaterial.WOOD:
             woodSFX = hit;
             woodTime = timeToWaitForSFX;
             break;
         case BreakableBody.BodyMaterial.GLASS:
             glassSFX = hit;
             glassTime = timeToWaitForSFX;
             break;
     }
 }
예제 #20
0
        float SFXTime(BreakableBody.BodyMaterial material)
        {
            switch (material)
            {
                case BreakableBody.BodyMaterial.CONCRETE:
                    return cementTime;
                case BreakableBody.BodyMaterial.WOOD:
                    return woodTime;
                case BreakableBody.BodyMaterial.GLASS:
                    return glassTime;
            }

            return 1;
        }