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

PolygonTools.CreatePolygon issue (MonoGame) Farseer 3.3.1

Topics: Developer Forum
Oct 9, 2012 at 7:00 PM
Edited Oct 9, 2012 at 7:03 PM

I began playing around with Farseer a few days ago and I love it so far, it has worked like a charm with MonoGame.

However, I have run into a problem, I've googled it for a few hours now and found that I'm not the only one with this issue but I couldn't find any answers.

The same code seems to work fine for some people and not work at all for others.

I'm trying to create a polygon from a texture object, I'm currently using the code from the AdvancedDemo1 but with no success.

This is how it looks:


	public Body ImageToPolygonBody(Texture2D texture, World world)
            Vector2 _origin;
            float _scale;
            Body _compound;

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

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

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

            //scale the vertices from graphics space to sim space
            Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * _scale;
            foreach (Vertices vertices in list)
                vertices.Scale(ref vertScale);

            //Create a single body with multiple fixtures
            _compound = BodyFactory.CreateCompoundPolygon(world, list, 1f, BodyType.Dynamic);
            _compound.BodyType = BodyType.Static;
            return _compound;

I've tried with many different images, and at the moment I'm using the ones provided in the Farseer sample so there shouldn't be anything wrong with the images.

Whenever I run this I get following error:

InvalidOperationException at line 459 in TextureConverter.cs

if (_multipartDetection || polygon.Count <= 2)
                    if (SearchNextHullEntrance(detectedPolygons, polygonEntrance.Value, out polygonEntrance)) <--- THIS
                        searchOn = true;


polygonEntrance is null for some reason.

I have included Faarser Physics XNA into my project (necessary for MonoGame), perhaps this causes it?

Any ideas?

I have followed this thread without any progress:


- Markus 


Mar 13, 2013 at 8:35 PM
you ever figure this out I'm having same issue.
Mar 13, 2013 at 9:46 PM
I'm afraid not.
I am creating all bodies manually in my current project.
Mar 13, 2013 at 10:15 PM
I changed your second code block to this and it works for me
 if (_multipartDetection || polygon.Count <= 2)
                    if (polygonEntrance==null)
                    polygonEntrance= Vector2.Zero;
                    if (SearchNextHullEntrance(detectedPolygons, polygonEntrance.Value, out polygonEntrance))
                        searchOn = true;

Aug 1, 2013 at 7:42 PM
I'm guessing you haven't found a solution to this problem yet eh?
I have the same problem and it's driving me mad. This for me happens in my metro app project on Monogame.
Running the exact code on XNA doesn't result in an error.
Through debugging I found that the texture data in Monogame is different than that of the XNA's, the GetData<uint>.
Aug 3, 2013 at 11:38 AM
Since the code works in XNA, and not in MonoGame, it seems to be a problem with MonoGame. This is not a support forum for MonoGame, but curiosity got the better of me, so I tried to replicate the problem. I'm not sure what I am doing wrong (or right...) as I am unable to reproduce the error.

I've created 3 projects with MonoGame: the debugview, FPE 3.5 from source control and the Testbed. It works fine on Texture.png that is included in the testbed.
I tried with FPE 3.3.1 too, and that works as well.

If someone provides a test that reproduce the problem, I can take a look at it.
Aug 5, 2013 at 7:05 PM
Yeah, my project has since been enable to run on my Surface but not on my other machines. I use the Team Foundation so the code is the same across all devices.
I don't know how to figure out what causes the problem, and it's annoying.