Skip to main content
GameDev.net gamedev.net
🔒 Locked

[Microsoft.Xna SpriteBatch] How to Simulate Layers in a 2D Map grid?

Started by Zotesso Apr 4, 2025 at 1:58 AM 2 replies 2k views
Original Post
Zotesso
Zotesso

Hello, genuine question, I've been racking my brains, but still can't figure out a solution.
What I want: To simulate the Z vector (x,y,z) in a 2D rendering Game, using something like layers when drawing.
Context: Game Engine using Monogame Framework to deal with Graphics.
Using the 2D Monogame Library.
How it works:
It draws things in the order that was requested.
So, first of all I ask it to draw the map grid(with layers) after the player.

The problem: even though I can achieve something close to what I want, using this approach of layers in the DrawMapGrid.
When the player is rendered it always will be above it all in the same last level, and I want one player to be in the layer 1, and the second player to be in the layer 2.
For example: in a bridge, the player in layer 2 is above it, and the player in the layer 1 is below, the player layer 1 need to be hidden when is crossing the bridge from below.

I imagine that recording the player layer level, and using a for loop to render the player right before the map grid of that exact layer can work. But it seems so wrong to me, render the player together with the map grid.

Thank you.

           Game1.spriteBatch.Begin();
           DrawMapGrid();
           DrawPlayer(0);
           DrawPlayerName(0);
           Game1.spriteBatch.End();
           
   private static void DrawMapGrid()
        {
            byte maxMapLayer = 3;

            for (byte layer = 0; layer < maxMapLayer; layer++)
            {
                for (int x = 0; x < Constants.MAX_MAP_X; x++)
                {
                    for (int y = 0; y < Constants.MAX_MAP_Y; y++)
                    {
                        DrawTile(x * 32, y * 32);
                    }
                }
            }
        }
None
Alberth
Alberth

Ever since the ‘80s/’90s, games have drawn their graphics from back to front. And yes, from back to front everything, including graphics representing the player or opponents. So while it may feel weird to you, it's common, at least in historic perspective.

With a GPU that manages depth, you can draw everything in one loop, and the GPU will figure out what to display. For 3D it's essential since computing what to display exactly is too complicated or costly.

For “real” 2D games, depth makes no sense in many cases, so these are rendered in layers (or perhaps, in order of drawing graphics). Not having depth is also a little cheaper I imagine, since the GPU doesn't have to manage the depth array.

There is also the option to make a 3D game that looks like a 2D game, by using orthographic projection. You can pick arbitrary depths then. Since it manages depth, it's a little more expensive to compute compared with “real” 2D games, although that is perhaps not such a big problem.

TooOld2rock-nRoll
TooOld2rock-nRoll

Think of it like having a 3D matrix and each level is one map.

You must print it bottom up to active the result you are describing.

Layer 1 → L1 Objects → L2 → L2 Objs → etc

It makes no difference if it's flat, isometric or pseudo 3D, when rendering things in 2D, this is the usual order of operations.

"No, you're never too old to rock and roll
If you're too young to die"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.