Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Texture rendering order in LibGDX

Tags:

android

libgdx

I am porting a game from iOS and Windows Phone to Android. Most of the code I am following though for the port is from the Windows Phone version, just because that is most similar to Java. My question relates to the OrthographicCamera class in LibGDX.

I am using the Game Object as Component container method for this game. This can be read about here: http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/ just so you have an idea what I am talking about. So, pretty much everything in the game extends the Component component class. And each game object i.e. a ball is a GameObject which constists of Components, i.e. Sprite, RigidBody (which is for the physics), Transform2 (position, rotation, etc.) This includes the Camera2 class, which is just wrapping an OrthographicCamera member.

I have two issues, and I have research it to no end. I cannot figure out how to do layering. The game has a drawer, that the user can swipe down to drag open. This should be on top of anything else in the game. The way that the guy who developed the XNA version accomplished this was by having multiple SpriteBatches and essentially treated each SpriteBatch as a layer. This doesn't work in LibGDX, and it's bad practice to use multiple SpriteBatches. The issue is, the game objects are organized as a tree, and they are rendered according to the traversal of the tree, so ordering ends up being based on when the object was inserted into the tree. I have looked into DecalBatch so that I could use a Z ordering, but it would require allot of refactoring as the game is very nearly done. As I said I tried using multiple SpriteBatches but for whatever reason this doesn't work either.

As this is already a long post, I will ask my second issue in a separate post.

like image 923
weaverx9x9 Avatar asked Apr 19 '26 00:04

weaverx9x9


1 Answers

I figured this out. I sent Mario from LibGDX a message and he suggested that I sort the objects just before rendering them. I wasn't really sure how to do this, as it's in a tree, I know how to sort a tree, but the ordering isn't there. I added a field for layer, and made the GameObject class implement Comparable, then, in the visitor patterns accept method, I sort all of that nodes children according to their layer. It is working perfectly now.

like image 179
weaverx9x9 Avatar answered Apr 20 '26 13:04

weaverx9x9