Archived

This topic is now archived and is closed to further replies.

skrwX

How to handle Z-coord in 2d?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

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.



Share this post


Link to post
Share on other sites