How to handle Z-coord in 2d?

Started by
3 comments, last by skrwX 22 years, 9 months ago
Well, hello I''m programming a 2d engine and got a problem: I''ve got a linked list to hold all game objects(monsters, hero, etc.) and an array to hold the tile data. I first draw the tiles and then the objects(should be normal I think) but my engine draws every object in the order they have in the linked list so that even when some object has a lower ypos-value(means that it should be drawn before the other object) it is draw above it. I thought I simply have to do some sorting(y-value) but then discovered that I have to sort a whole linked list everytime the gameloop is executed! How do you guy handles such things?
Advertisement
Maybe you are over complicating?
how many y-values do intend on having?
if (y==1)
{
blit() all y=1''s
}
if (y==2)
{
blit() all y==2''s
}
Yes that''s the way to go! Just look back at the technique of the SNES, it used 6 layers, 5 for the background and 1 for the sprites (at least something like that).
So, if you are codeing a sidescroller the following could work well for you:

1 or 2 (or more) layers for the backgrounds (maybe scrolling at different speeds, to fake depth)

1 or 2 sprite layers (one for animations like neon signs, etc. that are in the background of the actual characters, bullets,...)

1 or 2 layers for the foreground (fog, even more buildings,...)

Then assign every object to a layer an then draw every layer, by checking which layer the object belongs to.
This should also work with isometric engines, but I think it is far more complicated here.

Perhaps it helps, perhaps it was absolutely nonsens, you decide!
Yesterday we still stood at the verge of the abyss,today we're a step onward! Don't klick me!!!

The key is to sort your objects only when absolutely necessary.

Sort it once at the beginning of an area/level/room/whatever.

If an object is moving forward, compare his Z value with the object in front of him, and swap if/when necessary. If an object is moving backward, compare his Z value with the object in back of him, and swap if/when necessary. When you create an object, insert him into the list at the correct location. If you follow these rules, you won''t have to sort your list every frame.

If it doesn''t take too long to sort, you could sort once every 5 frames or something like that, if you don''t want to deal with above method.



Thanks, I''ll try it this way!

This topic is closed to new replies.

Advertisement