Example #1
0
 public FarseerObject(FarseerWorld world, Shape shape, BodyType BodyType = BodyType.Dynamic)
 {
     body          = new Body(world.World);
     body.BodyType = BodyType;
     body.CreateFixture(shape);
     body.Enabled = false;
 }
Example #2
0
        public FarseerObject(FarseerWorld world, Vertices vertices, float density = 1, BodyType BodyType = BodyType.Dynamic)
        {
            Shape shape = new PolygonShape(vertices, density);

            body          = new Body(world.World);
            body.BodyType = BodyType;
            body.CreateFixture(shape);
            body.Enabled = false;
        }
Example #3
0
        public FarseerObject(FarseerWorld world, Texture2D texture, float density = 1, BodyType BodyType = BodyType.Dynamic, float colapseDistance = 4)
        {
            //Create an array to hold the data from the texture
            uint[] data = new uint[texture.Width * texture.Height];

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

            //Find the vertices that makes up the outline of the shape in the texture
            Vertices textureVertices = PolygonTools.CreatePolygon(data, texture.Width);

            //The tool return vertices as they were found in the texture.
            //We need to find the real center (centroid) of the vertices for 2 reasons:

            //1. To translate the vertices so the polygon is centered around the centroid.
            Vector2 centroid = -textureVertices.GetCentroid();

            textureVertices.Translate(ref centroid);

            //2. To draw the texture the correct place.
            Origin = -centroid - new Vector2(texture.Width / 2, texture.Height / 2);;

            //We simplify the vertices found in the texture.
            textureVertices = SimplifyTools.ReduceByDistance(textureVertices, colapseDistance);

            //Since it is a concave polygon, we need to partition it into several smaller convex polygons
            List <Vertices> list = BayazitDecomposer.ConvexPartition(textureVertices);

            //scale the vertices from graphics space to sim space
            Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1));

            foreach (Vertices vertices in list)
            {
                vertices.Scale(ref vertScale);
            }

            //Create a single body with multiple fixtures
            body                     = BodyFactory.CreateCompoundPolygon(world.World, list, density, BodyType);
            body.BodyType            = BodyType;
            body.CollisionCategories = Category.All;
            body.CollidesWith        = Category.All;
            body.Enabled             = false;
        }
Example #4
0
 public FarseerObject(FarseerWorld world, IModelo2D model, float density = 1, BodyType BodyType = BodyType.Dynamic)
     : this(world, model.Texture, density, BodyType)
 {
 }