5
\$\begingroup\$

From other frameworks I know methods for easy ordering sprites within a spritebatch (to determine if a sprite has to be drawn in front or behind another sprite). For example in MonoGame/XNA this is called Sprite.depth or Pyglet has its OrderedGroups.

But I cannot find anything like that in libgdx. I've read that libgdx' scene2d has something like that but I don't want to use that as I use a entity system (ashley) and want to keep rendering separeted from the other logic.

What is the appropriate way to set the draw-order of libgdx sprites?

\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

You are correct that libGDX doesn't contain any sort of built-in support for rendering sprites in a certain order. (If it's there, I haven't seen it, and I've been using libGDX for a number of years.)

What I do is create a SpriteComponent class that wraps the Texture object and emits x, y, and z attributes.

Then in my draw method, I sort all SpriteComponents by z (ascendingly) and draw them -- so lower-Z items draw first, then higher-Z items on top.

\$\endgroup\$
2
  • \$\begingroup\$ So libgdx sprites and textures are just drawn in the same order in which I call their draw methods? (I haven't done very much with that framework yet, I'm only evaluating so far) \$\endgroup\$
    – Sebastian
    Aug 2, 2014 at 20:02
  • 1
    \$\begingroup\$ That's correct. In that sense, you have full control over the drawing order. There just isn't a built-in Z ordering of sprites. \$\endgroup\$
    – ashes999
    Aug 3, 2014 at 0:58
4
\$\begingroup\$

For normal cases, sprite order is determined by the Spritebatch.draw() call order. If this is the case:

spritebatch.draw(apple);
spritebatch.draw(mango);

then apple will be drawn first and then mango will be drawn on top of that. So mango will overlap apple.

\$\endgroup\$

You must log in to answer this question.

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