4
\$\begingroup\$

I have a query about how to create a 2d landscape level using libgdx/ box2d (Or possibly with Unity) for Android and iOs. Let me describe the goal and then the specific points I need help with.

What I'm trying to create is something like "Hill Climb Racing" where the player has to drive across a finite 2d free-form landscape, like the one shown here. Different areas may have different properties (friction, texture, restitution, etc) as shown. The player will be able to pinch and zoom out to see all or most of the map and pinch to zoom in to see details..

enter image description here

The level width will vary from game to game but at most it will be about 800 box2d meters wide. Now if I use 32px/m that requires an image 25,600 pixels across and this is where the issues start.. I can't tile it because that might preclude me from making free formed curves which I need to be able to do.

So what I need to know is:

  • How can I create a box2d body for the terrain?
  • What pixel/meter ratio should I use?
  • How many pixels wide should the image be?
  • Should I split the image up into sections and how should I chunk the images together?
  • What strategies have people used before to achieve this effect?

Hope I have been specific enough and thanks for all the help..

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I'm working on a little project that has a similar type of landscape to how you describe.

I use a Box2d chain shape for the physics. The Box2d chain shape just needs to follow the terrain edge, so it's dead simple, and it doesn't have issues with concave shapes and the restrictions Box2d polygons have.

For the graphics, I break down my terrain into triangles (once, at the start of the game), using an ear-clipping algorithm, and render the resulting mesh with OpenGL. You can render the mesh using solid colours, gradients, a single solid stretched texture, tiled textures, or whatever you can come up with via custom shaders and your imagination.

Using the above, you can use much smaller textures to 'fill' the terrain mesh, either via tiling or some other imaginative way. So there's not really any one size fits all, just use whatever you need to piece it all together. You can get away with using very little, if any at all, depending on your desired look.

An alternative that I considered but didn't explore further at the time, may be to break your terrain down into tiles of different type gradients, surfaces, corners, etc, in a way that you can make very flexible and unique looking landscapes but with just a couple of dozen small tiles or so.

As for your world being around 800 Box2d metres in size, I think you may get away with that but it's probably pushing it. I would test that idea out early on to make sure. I know I've read Box2d doesn't like very big dimensions, I'm just not sure offhand what those limits are.

Hope that helps.

\$\endgroup\$
4
  • \$\begingroup\$ Thanks for the advice, I think I'll try using your second method, that is to break the terrain down into more manageable images. I can then use a body-editor-loader to create the box2d bodies. This way I can set the properties of body separately.[aurelienribon.com/blog/projects/physics-body-editor/] \$\endgroup\$
    – AndyM
    Commented Nov 12, 2015 at 10:53
  • \$\begingroup\$ The thing to bear in mind with that approach, is that Box2d bodies can some times get stuck, or behave strangely, when moving across the 'join' of other, adjoining bodies. By using a single, continuous chain shape, you don't run into that issue (and as a bonus, you can make concave bodies unlike polygons). I have seen discussions on this matter so there may well be other workarounds. I'd be interested to hear how you get on if you go this route, because it does have some advantages (easier to do varying friction, etc). \$\endgroup\$ Commented Nov 12, 2015 at 13:34
  • \$\begingroup\$ There is a trick to prevent your character Box2D body getting stuck when moving across the join of adjoining bodies : You need to create a CircleShape foot for you character body. \$\endgroup\$
    – vdlmrc
    Commented Nov 12, 2015 at 14:07
  • \$\begingroup\$ CircleShape body for feet can work well, but if you have things like crates that can be pushed around, for example, they can have the same issue. \$\endgroup\$ Commented Nov 12, 2015 at 17:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .