Help with tile-based z-layer

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
AngryPie
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 4
Joined: Mon May 18, 2009 5:59 pm
Current Project: Designing RPG system
Favorite Gaming Platforms: SNES, XBox, PC
Programming Language of Choice: C / C++

Help with tile-based z-layer

Post by AngryPie »

I've used multiple layer maps for tile based games(really just walkabout demos with simple command scripting) before using Allegro/SDL/OpenGL.

But the way I usualy do it is simply by drawing one layer on top of the other like:

Code: Select all

int map[2][32][32];

for (int x = 0; x < width; x++)
{
     for (int y = 0; y < height; y++)
     {
          for (int l = 0; l < layers; l++)
          {
               drawTile(x, y, map[l][x][y]);
          }
     }
}

While that get the job done most of the time, I run in to problems with things like walls, trees or "over-tiles". For these I can either make a special object or a new layer but this is unintuitive and very memory wasteful.

The one thing I have learned about recently is a z-layer, which is like a tile based depth-buffer.
This is used in games like Chrono Trigger and I think in Elysian Shadows.

In this layer you can have a layer 1, layer 2, transition tile and a neutral tile.
To move between these tiles you use transition tiles like stairs which have info about the z-coordinate you move to.
A neutral tile is used for ridges or bridges.

It is also used for deciding where a sprite is drawn.

I tried messing around in Temporal Flux (Chrono Trigger Editor) but it was slightly confusing because of the layout of the editor.

The problem is that I have only a vague idea of how to implement this but it would be nice I could see some actual code or just get more info about it.

Also, when iterating through a three-dimensional array, what would be the most suitable order for a row-major language like C?

Any feedback will be greatly appreciated
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Help with tile-based z-layer

Post by MarauderIIC »

For arrays laid out as myArray[a][c], you should iterate as

Code: Select all

for (...a...) {
    for (...b...) {
        for (...c...) {
            //do something
        }
    }
}
Because of the ways arrays are laid out in memory, this minimizes page faults (which causes swapping of elements into and out of cache and memory), which increases run speed.

Layers in the editor are pretty much drawn as you described. Not sure what the problem with trees are... If trees are "objects" then you have the draw order of tile1, object1, tile2, object2, and this should work fine? The actual question here wasn't very well presented. Were you looking for help on transition tiles? All that this tile would do would be to change the player's z-coordinate. It would have to be flag on a tile, though, I think - when the player moves onto a tile with this flag set, if the player's previous z-coordinate was A then the z-coordinate should now be B, if it was B then it should now be A. A & B don't change. Not sure if we handle this right in ES yet.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Help with tile-based z-layer

Post by Falco Girgis »

You're talking about a fence where (for example) Crono's head is above the fence, but his feet are below, correct?

Sprites are rendered in two parts. Crono's legs, the fence, Crono's head. You have to have an object/z layer that is also drawn between the player's upper and lower halves.
AngryPie
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 4
Joined: Mon May 18, 2009 5:59 pm
Current Project: Designing RPG system
Favorite Gaming Platforms: SNES, XBox, PC
Programming Language of Choice: C / C++

Re: Help with tile-based z-layer

Post by AngryPie »

Yes, I also want to use this for being able to walk over the same tile that you can walk under, like a bridge. Oh and my problem with trees was not having an over layer and having to implement each tile as an object with a flag that would override y-sorting and place them on top of all other objects.

And what I'm mainly confused about is solid transition tiles. And I really just want to see if any examples exist because all the guides on using tiles are just very simple.

Also would this be easiest to implement in OpenGL? When drawing tiles with different z-values would I use a different 3d z-value? disabled depth buffer? multitexturing?

Well whatever, again any feedback is appreciated.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Help with tile-based z-layer

Post by MarauderIIC »

AngryPie wrote:Yes, I also want to use this for being able to walk over the same tile that you can walk under, like a bridge.
Right, you have to change the player's z-coordinate.
Oh and my problem with trees was not having an over layer and having to implement each tile as an object with a flag that would override y-sorting and place them on top of all other objects.
Okay. Well, you should keep your trees on the appropriate layer? The stump is on z-coordinate 30, the leaves are on z-coordinate 32, and the player is on z-coordinate 31, would work, I think?
And what I'm mainly confused about is solid transition tiles. And I really just want to see if any examples exist because all the guides on using tiles are just very simple.
Say you have layers drawn at z-coordinates 30, 32, 34, and 36, where higher numbers are drawn on top of lower numbers. Then your player is initially on 31. So he walks under the bridge drawn on 34. When he gets to the transition tile which, then the transition tile goes, "okay, if his z-coordinate was 31, then I'm going to change his z-coordinate to 35, and if his z-coordinate was 35, I'm going to change it to 31." And then since he was on 31, he's now on 35, so when he walks on the bridge he's drawn on top of it, instead of behind it.

This might have to be implemented using multiple tiles and multiple z-coordinates so that you can detect direction.
Also would this be easiest to implement in OpenGL? When drawing tiles with different z-values would I use a different 3d z-value? disabled depth buffer? multitexturing?
Different 3d z-value. I think the depth buffer has to be on for z-values to mean anything.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Help with tile-based z-layer

Post by MadPumpkin »

MarauderIIC wrote:Different 3d z-value. I think the depth buffer has to be on for z-values to mean anything.
that is correct
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Help with tile-based z-layer

Post by Falco Girgis »

MarauderIIC wrote:
Oh and my problem with trees was not having an over layer and having to implement each tile as an object with a flag that would override y-sorting and place them on top of all other objects.
Okay. Well, you should keep your trees on the appropriate layer? The stump is on z-coordinate 30, the leaves are on z-coordinate 32, and the player is on z-coordinate 31, would work, I think?
Yeah, but that's not really addressing his problem. He's talking about objects like a fence where the player is in front of and behind the same object.

Lets say that the stump is the fence on Z coordinate 30. Then the player's feet have to be 29, and his head has to be 31.
Also would this be easiest to implement in OpenGL? When drawing tiles with different z-values would I use a different 3d z-value? disabled depth buffer? multitexturing?
Multitexturing? You won't be applying multiple textures to one polygon. You will be drawing multiple polygons (each with their own respective textures) at various depths (or z coords).

As far as transition tiles, that is another thing that is on my vast to-do list for the engine. I'm guessing Kendall will probably wind up doing it, actually. (So I have no source for you).
AngryPie
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 4
Joined: Mon May 18, 2009 5:59 pm
Current Project: Designing RPG system
Favorite Gaming Platforms: SNES, XBox, PC
Programming Language of Choice: C / C++

Re: Help with tile-based z-layer

Post by AngryPie »

Ok thanks for for the info. I only though multitexturing would be good because it would avoid z-fighting(because I didn't think using a small z-coordinate would work)
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Help with tile-based z-layer

Post by MarauderIIC »

So use a big one?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply