Monday, December 12, 2011

Creating a Farseer Physics Body from a TiledLib map

(Please note I have found a much better way to do this. In fact I don't recommend you use this method at all. Expect a new post eventually.) I've been playing with TiledLib and Farseer and while doing so wrote some extensions to make integrating the two a bit easier.

The idea is to render a layer of a TiledLib Map to an image and then use Farseer's texture to poly tools to create a collision body. To do this I extended the TiledLib Map class to allow you to draw individual layers and save a rendering of the whole map to disk. Then I added a function that automatically creates a physics body from any giver layer.

There are some limitations to the above for example it only works on 1 object per layer, so split up your collision layers so that there is only one polygon on each layer. Fortunately, Farseer's textures to polygon tools seem to really like the consistency and uniformity that comes with a typical tilemap. Because of this I have had no problems so far with the tools failing to create a physics body.

Below is the whole class of extensions. Feel free to use it just keep the copyright notice up top.

PasteBin.com Class File

2 comments:

  1. Just wanted to give you a quick shout to say thanks for the Ruminate GUI library. Downloaded it today, had a poke around. We must think alike, because when I look at your code I nod and think it's how I would have done it - yours is the first XNA gui lib I've looked at that doesn't cause me to want to tear out my eyeballs.

    So, thanks!

    ReplyDelete
  2. Thanks man means a lot that people are enjoying it. Glad you found it readable, I figure that its pointless to release OSS software if no one else can read it.

    ReplyDelete