Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Items Rendering Behind Map Layers?  (Read 585 times)
0 Members and 1 Guest are viewing this topic.
Offline coltonoscopy

Junior Member


Medals: 2



« Posted 2013-10-12 01:00:07 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
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

Straight flippin.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2013-10-12 02:11:39 »

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?

Offline coltonoscopy

Junior Member


Medals: 2



« Reply #2 - Posted 2013-10-12 02:43:23 »

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:

1  
2  
3  
4  
5  
...
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:

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

Straight flippin.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2013-10-12 02:46:42 »

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

Offline coltonoscopy

Junior Member


Medals: 2



« Reply #4 - Posted 2013-10-12 02:51:38 »

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

It's a very simple method:

1  
2  
3  
4  
5  
6  
7  
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):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
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:

1  
2  
3  
4  
5  
6  
7  
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();
}

Straight flippin.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2013-10-12 02:54:58 »

I don't entirely like the fact that your batch is directly handling the drawing of the sprites, use this instead:
1  
entity.getSprite().draw(batch);

And then just set the sprite's position with
1  
entity.getSprite().setPosition(x, y);

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

Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #6 - Posted 2013-10-12 02:56:54 »

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?

Offline coltonoscopy

Junior Member


Medals: 2



« Reply #7 - Posted 2013-10-12 03:10:27 »

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:

1  
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.

Straight flippin.
Offline coltonoscopy

Junior Member


Medals: 2



« Reply #8 - Posted 2013-10-12 09:23:50 »

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!

Straight flippin.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (20 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (28 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (64 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!