Skip to main content
gamedev.net
🔒 Locked

z-sorting

Started by DauntlessCrowd 17 replies 1.7k views
Original Post
DauntlessCrowd
DauntlessCrowd
Hi, Does anyone know a good tutorial about z-sorting?? I have been searching for about 5 hours now and I really can't find a good tutorial about the subject... Maybe "z-sorting" is a less popular name, but I don't have a clue as to what a more popular name would be. Does anyone have any keywords / tutorials ? Ps: I have read the articles about isometric here on gamedev.net, but none of them explain it well... Kind regards, Dauntless
streamer
streamer
But Z-sorting is very simple.
If you use DirectX and orthogonal projection, just set the Z value of position vector between 0 and 1, when 0 is nearest and 1 is farest position and you'll get your sorting.
DauntlessCrowd
DauntlessCrowd
I'm not building my game in Java/C/C++/... But in flash. That's why i need a general article about z-sorting.

(Flash doesnt have a pre-build 3d engine)
JeremyCAFE
JeremyCAFE
All you have to do is assign values to everything that needs to be drawn on the screen. When you go to the draw phase you need to draw things in the order of their z value. You can simply sort the objects based on their z value before drawing, or store each different z value in its own container. Those might night be the optomized way of doing it, but that is the idea.
DauntlessCrowd
DauntlessCrowd
And how do you decide which tile has which z-value ? What is the formula ??
streamer
streamer
Tile which is in top left corner is first do draw so it has greatest z value, while tile which is in bottom rihgt corner will be drawn on the end(smallest z value). But you don't need any z sorting for doing that. Just draw map from top-left corner, drawing to the right, to bottom-right corner of screen. Every second row is drawn half tile width to the left, and half tile height to top. So if your tile is 64 pixels wide, and 32 pixels high, you will draw every second row with -32 offset to left, and -16 offset to top.
DauntlessCrowd
DauntlessCrowd
It's not the building of the map that's so hard, it's moving a character on the map... Deciding where he has to be (behind or before any objects) is what's troubling me.

(In flash you have 'depths' to arrange everything. The tiles with a high depth will appear in front of tiles with a lower depth)
streamer
streamer
When you draw tile, you draw everything on the tile. So if your character is on tile x,y then when you draw tile x,y you draw character also. If there's on tile a tree, wall, or any object, you draw that also when you draw a tile.

Looak at this link: flash iso example with fla
DauntlessCrowd
DauntlessCrowd
I already knew tonypas (and klas kroon's) tutorials, but to be honest: they are really buggy... (The isometric part sucks, the rest is fine).

Since I couldn't find a decent system on the internet, I built my own system and it works like a charm:

http://www.dauntless.be/Projects/Isometric/index.swf

Maybe this little demo makes it more clear what the problem was... When you move from 1 tile to another, the character has to appear in front of the tile it's facing and behind the tile on its right...
streamer
streamer
Hm. On my computer your flash seems to work perfectly. I don't see what the problem is.
DauntlessCrowd
DauntlessCrowd
That Flash file is something I put together myself after searching for Z-systems all over the internet. I couldn't find any, so I finally made my own little alrogithm...

Quote:
Layer1 (The ground) Draw this first.

Layer2 (Sprites) Draw this second.

Layer3 (Foreground) Draw this last.

How is this possible? Look dat my file: The hero is a sprite (I guess) and all those dark squares are foreground. If I was to draw the hero on layer2 and the black tiles on layer 3 (above the hero), you wouldn't be able to see the hero...
streamer
streamer
Well. First of all, you need to draw a map with a tiles. Tiles are drawn in specific order, from top to down. When you draw a tile, draw everything thats on that tile. In this case you don't need any sorting among z acsis.
I don't know anything about flash scripts and I'll try to make explain how to draw isometric map.

Now your tile size is:

tile_width = 64;
tile_height = 32;

and map drawing is:

for X = from 0 to 10
for Y = from 0 to 10

XDRAW = (X * (tile_width/2))-( Y * (tile_width/2))
YDRAW = (X * (tile_height/2)) + ( Y * (tile_height/2 ))

DrawTile 200+ XDRAW, 200 + YDRAW

end for
end for

Something like this [smile]

off course you can change tile_width/2 with 32 and tile_height/2 with 16
and you'll get

XDRAW = (X * 32) - (Y * 32)
YDRAW = (X * 16) + (Y * 16)

and in DrawTile function as a said draw everything on that tile.
First background, then players and enemies, and then static objects on map, like walls.



DauntlessCrowd
DauntlessCrowd
But that would require me to redraw the map every frame ... ?
streamer
streamer
No. You need to redraw everything if something changes on the map. For example if player position changes, enemy position changes or any other event that will change order of drawing on the map.
streamer
streamer
Then yes [smile] you need to redraw map every frame.
JeremyCAFE
JeremyCAFE
depending on how you draw things, you could only clear the sections in which something has changed and only re-draw in the tiles that have changed.

Topic Locked

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

Sign in to reply to this topic.