Insurmountable Problem with 2d game z-ordering?

I have a problem related to z-ordering of various game elements that I think is insurmountable but thought I’d post it in case someone has a great suggestion!

I have a 2d side-scrolling game.
There is a ‘subterranean’ layer at z-order 10 - this contains a large texture of ‘rocks’
There is a ‘surface’ layer on top of that at z-order 20 - this contains the surface on which the hero is walking (e.g. a path)
I have a ‘particle’ layer with various particle effects emanating from the player.
The particle layer needs to be in front of the surface, but behind the sub-terrain, so that the particle effects are in front of the surface but behind the sub-terrain.

I can’t see any way of achieving this as z-order 0 is behind both, 15 is on top of sub-terrain and behind surface, and 25 is in front of both.

Any suggestions would be appreciated!

Screenshot?

Here’s a screenshot. The subterrain is the lava, the path and fence are the surface

There’s no particles in the right place in the screenshot - but imagine the pig has a large particle effect, I want it to appear in front of the fence (surface layer) but behind the lava) sub-terrain layer.

Obviously while the terrain i s flat like this it would be relatively simple to mask it or something - but the terrain is pretty varied (hills, holes, valleys etc.)

Here’s a screenshot with particles on top of the subterrain and the surface - I want them behind the subterrain.

I don’t quite understand it, the particle layer’s z-order is between terrain and surface, right? Then it should work.

The particle z order needs to be in front of surface and behind terrain.
But the surface z order needs to be in front of terrain

So for particle I want (from far to near)
surface
particle
terrain

But for surface I want
terrain
surface

So I can’t put particle in the 2nd list because terrain and surface need to be reversed.

(This is so hard to explain!)

If i understand correctly you can do it like this:

  • disable z testing
  • draw terrain
  • draw surface
  • enable z testing
  • draw particle

The z value is that: terrain > particle > surface.

Interesting! i didn’t know you could disable z-testing.
Could you tell me how to do so?

thanks!

  • disable z test: glDisable(GL_DEPTH_TEST);
  • enable z test: glEnable(GL_DEPTH_TEST);
1 Like

Why not:

(from closest to farthest)

lava/subterrain
pooperpig
particles
ground+fence
sand dunes
moon, sky and stars

I would like the moon, sky and stars separate from the sand dunes so you can get some parallax action going!

The only reason is that the top of the subterrain is very ‘flat’ - i.e. is comprises a series of straight lines filled with texture, so with it in front of everytihng there is no way to disguise that.
By putting the ground on top of it I can overlay them slightly, and so it doesn’t look quite as ‘manufactured’ (the bottom of the road, for example, is not flat, but slightly ‘lumpy’)
Moon, sky and stars and dunes are in fact separate and have awesome parallax action!!

Cheers for the suggestions.

ahhh I understand what you are going for now. OK two more suggestions then. One easy thing is you use the ordering I suggested but also just make the top of the subterrain layer a little jagged instead of straight. But what I really think you should do is modify the particle code so that particles never go lower than the y-value of Pooper’s feet when she is standing on the surface - so the particles would actually stop at the vertical midpoint of the road. When a particle’s y-value is lower than that - clamp it to that midpoint and set the delta-y value to 0, but let it keep moving horizontally. Like when a rocket goes up the engine smoke goes down until it hits the ground and then it spreads out (it doesnt just go down and disappear through the ground)

I should add that those suggestions will only work if the ground in your game stays flat. I dont know if you plan on having hills and valley and such.

Unfortunately yes - see the 2nd picture I uploaded.
But great suggestion - I could look at the particle code and see if its easy enough to at least set a lower y limit - even if the particle Y stopped at that value, it would only overlap terrain in a valley so it might look awesome.
Thanks!

Thanks for your suggestions on this - I changed the particle code to stop movement in the Y direction below a height - and it looked great on flat terrain!
but too hard to do all the calculations needed to cope with hills and valleys :frowning:

So instead I have moved by terrain (ground) behind the subterrain (lava) and added a ‘foregroundterrain’ in front of everything.

this new layer helps ‘soften’ the harsh surface of the subterrain, and allows the particles to be on front of the surface but behind the subterrain.

Again - thanks for your help and advice.

very happy that things worked out - good luck with PooperPig!

Not as happy as I am :stuck_out_tongue_winking_eye: