First off heres what I’m doing:
I create a tilemap in Tiled, and one of the layers is the “collision mask”, I draw the collision mask onto a rendertarget/texture and pass it into a texture to body method(will post the method later in this post) and then it turns my mask into a polygon in which my player can now walk on, great!.
The problem:
I was using my actual tileset that should be drawn as the the graphic not the collision mask, which has a lot of ruff edges on the textures, I decided for maybe some extra performance I would create a mask(both pictures will be uploaded later in the post), once I set the collision mask as a actual collision mask I got an error in the farseer physics class “TextureConverter” “Nullable object must have a value.”. I decided to replace the collision mask back to the normal tileset, and it worked again, I then placed a break point where I got this error and changed back to the collision mask and it never gets called… The line is 395. The variable “polygonEntrance” is null but the Value part of it says the error message, so I’m decently sure its coming from this variable. I noticed when the texture doesn’t have anything actually on it I also get this error, but I can confirm that stuff is getting drawn.
Now for the pictures!
Collision mask:
http://postimg.org/image/yf5gnh0ab/15744f53/
Tile set:
http://postimg.org/image/7b9lkrndp/6e5cbff0/
make sure to click on the image before saving if you do choose to download
The texture to body(I am using the most up to date source code of farseer physics, however edited but all I did was made the classes needed for this method public in order to work) Code and method is from http://scott-franks.blogspot.com/2011/12/creating-farseer-physics-body-from.html however updated to tilesharp)
public static Body LayerToBody(Texture2D texture, GraphicsDevice device, World world, float meterInPixels, string layer) {
var polygonTexture = texture;
var data = new uint[polygonTexture.Width * polygonTexture.Height];
polygonTexture.GetData(data);
var verts = PolygonTools.CreatePolygon(data, polygonTexture.Width, true);
//These 2 seem to work the best with tile maps
verts = SimplifyTools.CollinearSimplify(verts);
verts = SimplifyTools.DouglasPeuckerSimplify(verts, 0f);
var list = BayazitDecomposer.ConvexPartition(verts);
var vertScale = new Vector2(1f / meterInPixels);
foreach (var vertices in list) {
vertices.Scale(ref vertScale);
}
//Create a body from the poly
var body = BodyFactory.CreateCompoundPolygon(world, list, 1);
return body;
}
Greatly appreciate anyone who is willing to help