Sign in to follow this  
Martijnvdc

Depth of objects

Recommended Posts

Hi, i am currently making a isometric city builder. My problem is that i don't know how to calculate wich object should be rendered in front of wich. The buildings can stand on more than one tile, so i can't just render from top to bottom. Here is an image that explains my problem: http://img340.imageshack.us/img340/2953/isopw7.jpg if i would render from top to bottom green would be rendered above blue, that is not how it should be... Does anybody know of a good formula to calculate the depth of an object so that i can make a list of wich objects to render first? Thank you :d

Share this post


Link to post
Share on other sites
I would introduce all objects into a graph structure (using the fact that a tile is in front of some tiles and behind some others), then topological-sort that structure, and deduce the depth from there. This only needs to be done once per modification, and it's pretty quick (because single-tile objects are trivial to sort).

Share this post


Link to post
Share on other sites
You can calculate the depth for each object. For instance, if the origin of your isometric grid is on the top and x and y axis are pointing to the bottom left and right respectively, you can simply calculate your depth as x+y. Then draw the tiles with an order of increasing depth.

Since your buildings can stand on more than one tile, just split them into parts that can be rendered individually. For instance for a tile you can store what building and which part of it belongs to that tile, then only draw that part when drawing the tile.

Share this post


Link to post
Share on other sites
In that example you gave, can't you solve it by iterating through the loop from the top corner down the bottom corner? That should draw the buildings in the way you want. If the top corner is (0, 0), and we assume a square area, then shouldn't the following code worrk?

i=0; j=0;
for(n=0; n<2*max; n++) {
drawTile(i,j)
if(j==0) {
i = 0;
j++;
}
else if(i == max) {
i = j;
j = max;
}
else {
i++;
j--;
}
}

Share this post


Link to post
Share on other sites
a possible way of doing it could be:

assign each tile an id based on the grid-cell it's on, e.g.:

tile-abc is on 10x and 10y, tile-id will be 100.

then sort your list of tiles based on the tile-id (rendering starts with the highest-id), e.g.:

render tile with id 100, 99, 98, 97, ...

i think that's what the poster above me wanted to say but his post looked a bit confusing to me :)

Share this post


Link to post
Share on other sites
Check my post here:

http://www.gamedev.net/community/forums/topic.asp?topic_id=437968

I think that loop should work, but I am too lazy to check it. And it only works for a square region. Basicaly what I am saying is that if you are doing an isometric view with square grid of cells, and you can't do a simple for (i=0; i<max; i++) for(j=0; j<max; j++) loop. Because it will draw some tiles that are closer to the user *before* it draws tiles that are further away. And if that tile contains something which would overlap with another tile, it may draw over a tile that is closer to the user. You want tiles that are the furthest away from the user to be draw first. The tiles lying along a 'diagonol' of the square grid pretty much are all equidistance from the users perspective. So you want to draw all thsoe tiles are the same time, then the tiles in front of them, then the tiles in front of those, etc. You want to start at the top, then work your way toward the viewer. If the upper-left hand corner is the furthest away from the viewer, you would want to draw the tiles like this (the number indicates the order which you should draw them - equal numbers means the orders are equal and they should all be drawn at roughly the same time)


1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6 etc.
6 (camera)


As opposed to

1 2 3 4 5 6
7 8 9 10 11 12
13 14 ...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this