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

Texture to polygon (in 3.0)

Topics: User Forum
Aug 27, 2010 at 2:26 PM


Im trying to wrap my head around 3.0 and creating a fixture/body from a texture. I'm not sure wether im doing this correctly, but i think not, since i get an error.

This is my code:

            Texture2D texture = Content.Load<Texture2D>("Bottom");
            uint[] data = new uint[texture.Width * texture.Height];
            //Calculate the vertices from the array
            Vertices verts = PolygonTools.CreatePolygon(data,texture.Width,texture.Height);

            BottomFixture = FixtureFactory.CreatePolygon(world,verts,3);

Anyways, when compiling this  i get error:
I should add that im using the HelloWorld example avail for download.
Any ideas?
Aug 28, 2010 at 2:39 PM

Check if your texture is concave - you will need to decompose it if that is the case. Try compiling the library in debugging mode (F5) and press the retry button.

Aug 29, 2010 at 10:05 AM


Thanks for the suggestion, but the texture is only a triangle laying flat on the bottom of the image.
Any other suggestions?

// Andreas

Aug 30, 2010 at 8:22 AM

It's hard to reproduce the bug without having the texture. I guess.

I don't know if I'll find any time to debug this (as I'm currently working 10h a day -.-), but maybe someone else finds some time and then he/she - probably - will need your texture.

Aug 30, 2010 at 9:44 PM
Edited Aug 30, 2010 at 9:51 PM

I tried using this and a few similar methods and I got this same crash.  The crash happens EVERY time that the number of vertices that FP3 calculates is greater than 8 (the MaxPolygonVertices number, because of the engine being set to call a non-named Exception in that case.  For me, no texture matters.  I even went as simple as to go into paintbrush and paint the entire rectangle black and nothing else.  It always detects between 14 and 17 vertices on an image, making the function never ever work.

The comments for the area in the code where it crashes don't make a bunch of sense, either.

It states:  

Debug.Assert(2 <= vertices.Count && vertices.Count <= Settings.MaxPolygonVertices);  

Another section of code in polygonTool.cs states:

//We need at least 8 vertices to create a rounded rectangle
Debug.Assert(Settings.MaxPolygonVertices >= 8);

As you can see, those two lines contradict each other.  It gives me question as to why someone would even use some polygons such as the Ellipse since an 8-sided ellipse cannot come remotely CLOSE to the shape of an ellipse.

Perhaps there's a bug in the library here?

I gave up on trying to use texture to polygon because of the 8 vertex limit (and the fact that it ALWAYS detects between 14 and 17 vertexes on the series of images I tried)


Aug 31, 2010 at 1:58 PM

I found a bug with this as well, to fix it I added the following before calling the CreateCompoundPolygon

list = BayazitDecomposer.ConvexPartition(verts);

List<Vertices> toRemove = new List<Vertices>();

foreach (Vertices item in list)      {        if (item.Count == 0)        {          toRemove.Add(item);        }      }
foreach (Vertices item in toRemove)      {        list.Remove(item);      }            

//Create a single body with multiple fixtures            
compund = FixtureFactory.CreateCompundPolygon(World, list, 1);


Basically the problem was the list contained Vertices with a count of 0, which was causing the CreateCompoundPolygon to crash.
Hope that helps you.