Oct 9, 2012 at 6:00 PM
Edited Oct 9, 2012 at 6: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)
//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();
//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)
//Create a single body with multiple fixtures
_compound = BodyFactory.CreateCompoundPolygon(world, list, 1f, BodyType.Dynamic);
_compound.BodyType = BodyType.Static;
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?
I have followed this thread without any progress: http://farseerphysics.codeplex.com/discussions/276029