11

I'm new in Android Game Development and after I started with libgdx ShapeRenderer and did a little more search, I became confused if I started with the right foot.

So, what I really would like to know is when should I use ShapeRenderer, Mesh + SpriteBatch, Box2D and Scene2D.

Cristiano Santos
  • 2,157
  • 2
  • 35
  • 53

1 Answers1

25

LibGDX has quite a lot of (mostly orthogonal) APIs for rendering. I'm still learning my way around many of them, but I can give an overview of the different parts.

ShapeRenderer lets you quickly and easily put basic flat-colored polygons and lines on the screen. Its not particularly efficient (it uploads a lot of vertex data on each render). But it can be very quick to get going. Its oriented strongly to the screen (it defaults to an orthographic project on the full screen and its units are generally pixels).

The Mesh class gives you more direct control over geometry, but you're exposed to a lot more of the details (like encoding colors as floats and storing vertex data, color data, and texture data all in the same float array, and the concept of index arrays).

The SpriteBatch is oriented around "sprites" (generally rectangular areas with a texture in them). This hides some of the complexity (and flexibility) of texturing with meshes.

Box2D is for physics modeling, and is not like any of the other API parts you mentioned (which are mostly about rendering).

Finally, Scene2D is mostly about creating animated user-interface elements like buttons, lists, and sliders. But its powerful enough (and has enough neat and flexible support for animations and interaction events) that you can implement quite a lot of "game" in it too.

You can also use raw "OpenGL" commands (see GL20), too.

You can totally mix-and-match these different primitives (one caveat is that you generally must "flush" and "end" whatever ShapeRenderer/SpriteBatch/Mesh render before starting a different flavor). So, for example, you might use Scene2D for your "main menu" and some meta actions in your game, a SpriteBatch to render the core of your game, and use a ShapeRenderer to generate debug overlays (think visualizing bounding boxes, etc).

Also note that most of libGDX is geared towards 2D. Raw OpenGL and the Mesh APIs can also be used to build 3D objects.

Check the gdx-invaders example to see how it uses Mesh, OpenGL, and SpriteBatch. (I think this example pre-dates Scene2d, so there is none of that...). Its also worthwhile to look over the implementation of these classes to see what they're doing underneath the covers.

P.T.
  • 24,557
  • 7
  • 64
  • 95
  • Hmm, thanks. Now I see the differences =). Just one last question: What's better for collision detection? Box2D, Intersector or any other that I don't know? – Cristiano Santos Feb 21 '13 at 09:40
  • 1
    `Intersector` has all the tools you need to do collision detection yourself. Box2D will do collision detection, but it is a complete 2D physics simulation, so it does a lot more. If you're just intersecting bullets with enemies (or players with floors), you're probabaly better off rolling your own. – P.T. Feb 21 '13 at 16:18
  • @P.T. Rolling your own may be not so easy when you must continuously check collisions between N bullets and M enemies when these numbers are not small... and properly blocking player movement with floors and walls is also not so easy to do right. – Display Name Mar 10 '14 at 21:16