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

make polygon from texture

Topics: Developer Forum, Project Management Forum, User Forum
Oct 22, 2010 at 2:15 PM

Hello everybody.

I wanna to make polygon from the polygon.

Here is my code.

uint[] data;
                    List<Fixture> lstFixture = new List<Fixture>();
                    List<Vertices> lstVerctices = new List<Vertices>();

                    texture = conManager.Load<Texture2D>(obstName);
                    data = new uint[texture.Width * texture.Height];
                    Vertices vertices = PolygonTools.CreatePolygon(data, texture.Width, texture.Height, false);
                    Vector2 origin = new Vector2(-texture.Width/2, -texture.Height/2);
                    vertices.Translate(ref origin);
                    Vector2 scale = new Vector2(0.03333333333f, 0.033333333333f);
                    vertices.Scale(ref scale);
                    lstVerctices = BayazitDecomposer.ConvexPartition(vertices);
                    lstFixture = FixtureFactory.CreateCompundPolygon(, lstVerctices, 1);
                    //CreateBody(false, float.MaxValue, float.MaxValue, 0.4f, 0);
                    body = lstFixture[0].Body;
                    body.Position = CmnRsc.ToWorldCoordinates(position);
                    body.BodyType = BodyType.Static;
                    fixture = lstFixture[0];
                    body.Rotation = 0;

But my body is drawn by fliping about X-axi.
What's wrong? 

Oct 22, 2010 at 3:03 PM


Vector2 scale = new Vector2(0.03333333333f, 0.033333333333f);


Vector2 scale = new Vector2(0.03333333333f, -0.033333333333f);

and try that.

Also some screen shots would be very helpful. You can take screen shots by hitting PrtScr button on keyboard and then pasting into you bitmap editor.
Oct 22, 2010 at 3:09 PM

I have already done like that.

But if that case, I can see the exception error.


Oct 22, 2010 at 3:12 PM

if you try it, you will be able to see the exception error.

Oct 22, 2010 at 3:46 PM

Try the EarclipDecomposer instead of the Bayazit.

Oct 22, 2010 at 4:00 PM

Wow, very thanks.

But the polygon is not matched with the texture extactly.

What's wrong?

Oct 22, 2010 at 7:56 PM

You may have to adjust the tolerance factor to something like 0.01f.

Also you need to loop through all the polygons and add them all to your body

Something like this -

            foreach (var item in lstVerctices)
                Vector2 origin = new Vector2(-texture.Width / 2, -texture.Height / 2);
                item.Translate(ref origin);
                Vector2 scale = new Vector2(1, -1);
                item.Scale(ref scale);
                b.CreateFixture(new PolygonShape(item));

However I think this snippet is from a slightly earlier version of the engine so the code might be a little bit different.

Oct 23, 2010 at 1:38 AM

Thank you for your kind advice.

And I have fixed all my problems about the polygon from the texture.

But I have another problem.

I will post it in another discussion title.

I hope to see you again there.

Thank you . . .