How to represent terrain as physics objects in Farseer

Topics: Developer Forum
Jun 1, 2012 at 7:39 AM

I've got a 2D hilly terrain - much like the terrain of the game "Tiny Wings" and I'm not sure how to represent that best as physics objects.

There is a method to generate polygons from a texture described in the Farseer documentation: http://farseerphysics.codeplex.com/documentation (Section "Texture to polygon")

But actually I don't need the terrain as polygon. I only need the surface-line. Representing the terrain via polynomials would be great but I couldn't find any support for polynomials in Farseer.

Do I have to work with Edges and LineArcs to model my hills, or is there anything that would be better suited, or better in terms of performance?

Jun 1, 2012 at 1:34 PM

Why don't you want to use polygons? That is the simplest solution.  Heres an early thread from someone who did just that. It has a video of his efforts.

http://farseerphysics.codeplex.com/discussions/352278

Jun 3, 2012 at 1:44 AM
Engicoder wrote:

Why don't you want to use polygons? That is the simplest solution.  Heres an early thread from someone who did just that. It has a video of his efforts.

http://farseerphysics.codeplex.com/discussions/352278

Could he/she not use Polylines instead of polygons?

Jun 6, 2012 at 8:29 AM

Thanks for your replies!

I thought, representing the terrain via a polnomial would have 2 advantages:
1) Probably faster than terrain as polygons
2) No discontinuities, which you have with a polygons or polylines (from one polygon/polyline to the next) 

Jun 10, 2012 at 11:18 PM
j00hi wrote:

Thanks for your replies!

I thought, representing the terrain via a polnomial would have 2 advantages:
1) Probably faster than terrain as polygons
2) No discontinuities, which you have with a polygons or polylines (from one polygon/polyline to the next) 

Why would polygons be faster?

Jun 10, 2012 at 11:22 PM
Edited Jun 20, 2012 at 10:43 PM

No, I meant a polynomial (x^3 + x^2 + x + c) would be faster than a polygon for representing the terrain?!

Is there any support for this? Or does Farseer support Splines or anything similar?

Jun 11, 2012 at 12:37 AM
Edited Jun 11, 2012 at 12:43 AM

There is no direct support for spline segments, only for lines (EdgeShape).

To use a polynomial you would have to sample it and construct the vertices from which to build the lines. If you already have a few sample points but can't get more (don't have the exact math equation like e.g. when you are tracing a sprite) XNA itself provides several interpolation functions in MathHelper - linear, cubic, Hermite and Catmull-Rom to approximate the spline. There is, however, no benefit from it being a spline in any way since at the end you  just get a bunch of lines.

With EdgeShapes/ChainShape the discontinuities between neighbouring segments that can interfere with collisions are dealt with by having automatic "ghost" vertices. It's an ad-hoc solution by Erin Catto in Box2D. Great explanation and video of this: http://www.iforce2d.net/b2dtut/ghost-vertices

Jun 20, 2012 at 10:57 PM

Thanks for pointing out EdgeShape/ChainShape. There are also 2 sections in the Box2D user manual devoted to those two: 
http://www.box2d.org/manual.html
4.5 Edge Shapes
4.6 Chain Shapes 

I've also found a nice blogpost which deals exactly with terrain I would like to create. Looks like the game "Tiny Wings" also uses small line segments to represent the terrain, and not a polynomial or spline:
http://www.emanueleferonato.com/2011/07/14/create-a-terrain-like-the-one-in-tiny-wings-with-flash-and-box2d/