Items Rendering Behind Map Layers?

Hey all! I’ve encountered an odd issue whereby the items I have spawned in the game world seem to be rendering behind the game map itself, despite their render calls taking place after the OrthogonalTiledMapRenderer I’m using. I took the SuperKoalio example as a foundation for the platforming code but changed it to fit more of an Entity-Component model, then added a List of what I’ve named ItemDrops that have physics and are supposed to render just a layer behind the player. However, they end up rendering behind any of the map layers as well, and when I jump, they seem to rise up and down a little bit, as if there is some sort of parallax effect involved; I don’t know if the OrthogonalTiledMapRenderer does anything like that. They’re using the same SpriteBatch as the player and the renderer. The higher-level flow is like the following, within my render method:


Gdx.gl.glClearColor(0.7f, 0.7f, 1.0f, 1);
Gdx.gl.glClear(GL10.COLOR_BUFFER_BIT);

// updates their position, applies gravity, etc.
level.updateEntities(delta);
player.update(delta);

// let the camera follow the player
camera.position.x = player.position.x;
camera.position.y = player.position.y;
camera.update();

// set the tile map renderer view based on what the camera sees and render the map
renderer.setView(camera);
renderer.render();
level.renderEntities();
player.render();

Render calls to the player and the entities are basically just acquiring the SpriteBatch from the same renderer as shown here and calling the needed batch.begin() and .end() calls, drawing the needed frames, and that’s it. The ItemDrop objects mentioned previously are updated and rendered in level.updateEntities(delta) and level.renderEntities(), successfully; the List resides within the Level object. Help is greatly appreciated! Willing to supply any code needed, but there’s quite a bit at this point; don’t want to bog down the post too much! Hoping the issue is something simple I don’t know about SpriteBatches or OrthogonalTiledMapRenderers! ;]

Colton

I wonder if it has something to do with your setting the camera coordinates and then rendering everything with that camera? Can I see the camera class please?

Hi opiop65,

Could be! I simply used an OrthographicCamera for that bit, and the relevant code is in the Screen constructor as such:


...
camera = new OrthographicCamera();
camera.setToOrtho(false, 30, 20);
camera.update();
...

For what it’s worth, I’m also using an OrthogonalTiledMapRenderer that renders the level’s TiledMap with a scale factor of 1/16f:


renderer = new OrthogonalTiledMapRenderer(level.map, 1/16f);

Hmm… no that all looks fine. Can I see level.renderEntities?

It’s a very simple method:


public void renderEntities(float delta)
{
   for (Entity e : entities)
   {
      e.render();
   }
}

This render method is polymorphic but for the sake of this desired outcome it looks like this (per ItemDrop, which is a subclass of type Entity):


public void render()
{
   if (renderComponent == null)
   {
      return;
   }
   else
   {
      renderComponent.update(0);
   }
}

RenderComponent is used here separate from being stored in each Entity’s list of other components because typically we will want the update logic decoupled from the render routine. Here’s the update() method for each ItemDrop’s RenderComponent:


public void update(float delta)
{
   SpriteBatch batch = renderer.getSpriteBatch();
   batch.begin();
   batch.draw(entity.getSprite(), entity.position.x, entity.position.y, entity.width, entity.height);
   batch.end();
}

I don’t entirely like the fact that your batch is directly handling the drawing of the sprites, use this instead:

entity.getSprite().draw(batch);

And then just set the sprite’s position with

entity.getSprite().setPosition(x, y);

I don’t know if that will do anything, but who knows.

Oh wait, I just realized you’re creating a side scroller type game, not a top down, which introduces a different set of problems. The objects come out of the background when you jump? Very strange, did you set up your camera with any sort of zoom?

No zoom; the objects render in the background when they’re supposed to be between the player and the map (not behind the map), which is visible given the order of the render calls. If the player jumps, the objects move faster on the Y axis than the environment, though it should just be the same, giving it a strange parallax effect. Interestingly, if I create a BitmapFont and try to render some text on the screen, it looks like it’s rendered relative to the world and not the screen:


font.draw(batch, "HP: " + player.stats.health, 10, 10);

That call draws that string in the world, so I can walk and see it as if it were written on the background. This is placed within a batch.begin() and batch.end() and the batch is acquired from the renderer. This and the other issue may be related.

Figured it out! Was forgetting to place the RenderComponent within the renderComponent field within the GameObject subclass, as opposed to just letting it be added to the components list like all of the other components, which caused the out of sync and background issue. Not too sure why text still renders super huge and as part of the game world instead of an overlay if used with that SpriteBatch, but I’ve circumvented that separate issue just by using a different SpriteBatch object entirely. Thanks for trying to help out regardless!