Editable Geometries and Bodies

Topics: Developer Forum, User Forum
Nov 6, 2010 at 1:16 PM


I have an idea for a game, but not sure if it is possible to implement it. Imagine a side-scroller where the player begins with a block on the bottom of the screen. That block would show several points (vertex) that the player could pick and change the size, shape, of the block in order to create bumps. hills, slopes, etc.

This would allow the agent to easily create the topography of the level in runtime. I would like to know if is possible then to save the level to a file, and the load it again?

Thanks in advance!

Nov 6, 2010 at 11:56 PM

The only part that FPE is interested in is the vertices that makes up your terrain. You can create a level class in your project that contains the vertices. You can use serialization to write the object to a file. Once you load the level again, you can extract the vertices collection and supply it to the physics engine.

Nov 7, 2010 at 12:54 AM

Thanks Genbox, for the hint on saving my level. Now the question is if it is possible to change the shape and size of the terrain by moving the vertices with the mouse?


Nov 7, 2010 at 1:05 AM

Indeed it is. You have all the tools needed to check if the mouse is near a vertex and then you can move the vertex to the new mouse position. You need to convert the mouse position to physics world coordinates, then you check for nearby physics shapes and then you check each vertex on the shape until you reach one that is near the mouse position.

Once you have moved the vertex, you simply remove the old fixture and create a new one using the old body and the new shape.

Nov 7, 2010 at 4:54 PM

Ok, from your explanation seems simple, let's see how it goes, thanks again.

Nov 7, 2010 at 5:26 PM

Last time i worked with Farseer was with version 2.X, now i'm seeing the samples from 3.0 and it seems a bit different. Could you please give some guidance on which classes and methods i should use in order to change the shape of a rectangle? Initially it should only have 4 vertex, but to make a more complex geometry, it would be interesting for the user to add vertices. If that is not possible, then a rectangle would be created with a lot of vertices, allowing the player to pull them and making the terrain of the level.

I have the idea, but don´t know what tools to use to make it work.


Nov 8, 2010 at 1:14 PM

Start out with a 4 vertex polygon instead. Understand that you can now only have 8 vertices per polygon. So you will have to make sure to decompose your polygon every so often or maybe just create new polygons as the user move along.

Nov 9, 2010 at 6:17 PM

Hi, i was working with FP3.0 but have a deadline for Saturday, so i moved my project back to FP2.13. Anyway, i already have the level creation completed, the user draws paths with the mouse in order to create the terrain and also add some objects like ramps, etc.

The thing is, using FP2.13 which is the best way to save the level created by the user?  Genbox has already told me about serialization, but i don´t know how to use it in order to save all the objects of the world.

Any hint on how i could do this?

Thanks again.


Nov 9, 2010 at 7:12 PM

You save the position, rotation and properties (if they are different from the defaults) of all bodies. Then you can use serialization to save the data to disk. The loading of a level is just the opposite: you load the data, create the objects and populate a World object with the bodies (that have the saved position, rotation and properties).

I hope to support full World serialization in FPE 3.2