This project has moved and is read-only. For the latest updates, please go here.

Wrong scale using BayazitDecomposer

Topics: Developer Forum
Jun 13, 2012 at 3:00 PM

Hi all,

Now i finally could manage the use of Farseer, i have found myself with another problem. Ii have used the code inside the Advanced Sample1 to create a compound body with the BayazitDecomposer, but the scale of the body created is diferent as the one i have in the texture.

This is an example screenshot:

-The red dots are the edges (vertices) of the body created. It´s a simple Debug help that i have created just as i don´t know how to use your debug view (i know, i am the worst programmer ever....:/ )

-The violet body is the texture

-And the little person is a simple squared body which will be a player in the future. Just to see.

Even i have tried to use the original texture of the example to prove if there as any problem with the tipe of file or how it was drawed, but it results the same:

Now,i don´t knkow how to proceed. I´ve been changing all the parameters of the functions called just to see what were the problem etc...And even use some parameters to scale the body by myself, but nothing happened.

Could you please help me out?

Thanks a lot.


P.S.: Here is my code:

void LoadScenBody(World world, int scenNum, ContentManager Content)
            switch (scenNum)
                case 1:
                        // Bayazit algorithm prove

                        //load texture that will represent the physics body
                        Texture = Content.Load<Texture2D>("images/object");

                        //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

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

                        //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;

			_scale = 1f;
                        //We simplify the vertices found in the texture.
                        textureVertices = SimplifyTools.ReduceByDistance(textureVertices, 4f)*_scale;

                        //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));

                        int j = 0;

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

			    //Debug purpose                            

                            for (int i = 0; i < vertices.Count; i++)
                                Dot[j].Position = vertices[i]*MeterInPixels;
                                Dot[j].Position.X = Dot[j].Position.X + 512;
                                Dot[j].Position.Y = Dot[j].Position.Y + 340;

                        //Create a single body with multiple fixtures
                        _compound = BodyFactory.CreateCompoundPolygon(world, list, 1f, new Vector2(512, 340) / MeterInPixels);
                        _compound.IsStatic = true;
                        _compound.Restitution = 0.0f;
                        _compound.Friction = 0.2f;