示例#1
0
        public override void Initialize()
        {
            base.Initialize();

            var data = new uint[_subtexture.SourceRect.Width * _subtexture.SourceRect.Height];

            _subtexture.Texture2D.GetData(0, _subtexture.SourceRect, data, 0, data.Length);

            var verts = PolygonTools.CreatePolygonFromTextureData(data, _subtexture.SourceRect.Width);

            verts = SimplifyTools.DouglasPeuckerSimplify(verts, 2);

            var decomposedVerts = Triangulate.ConvexPartition(verts, TriangulationAlgorithm.Bayazit);

            for (var i = 0; i < decomposedVerts.Count; i++)
            {
                var polygon = decomposedVerts[i];
                polygon.Translate(-_subtexture.Center);
            }

            // add the fixtures
            var fixtures = Body.AttachCompoundPolygon(decomposedVerts, 1);

            // fetch all the Vertices and save a copy in case we need to scale them later
            foreach (var fixture in fixtures)
            {
                _verts.Add(new Vertices((fixture.Shape as PolygonShape).Vertices));
            }
        }
示例#2
0
        public override void Initialize()
        {
            base.Initialize();

            uint[] data = new uint[Sprite.SourceRect.Width * Sprite.SourceRect.Height];
            Sprite.Texture2D.GetData(0, Sprite.SourceRect, data, 0, data.Length);

            Vertices verts = PolygonTools.CreatePolygonFromTextureData(data, Sprite.SourceRect.Width);

            verts = SimplifyTools.DouglasPeuckerSimplify(verts, 2);

            List <Vertices> decomposedVerts = Triangulate.ConvexPartition(verts, TriangulationAlgorithm.Bayazit);

            for (int i = 0; i < decomposedVerts.Count; i++)
            {
                Vertices polygon = decomposedVerts[i];
                polygon.Translate(-Sprite.Center);
            }

            // add the fixtures
            List <FarseerPhysics.Dynamics.Fixture> fixtures = Body.AttachCompoundPolygon(decomposedVerts, 1);

            // fetch all the Vertices and save a copy in case we need to scale them later
            foreach (FarseerPhysics.Dynamics.Fixture fixture in fixtures)
            {
                _verts.Add(new Vertices((fixture.Shape as PolygonShape).Vertices));
            }
        }