PDA

View Full Version : Ideas needed to 2d sprite z-ordering sort



ianatwork
07-10-2008, 07:57 AM
I'm developing a 2d game that requires the sprites to be z-order sorted but am not sure how to go about doing this. Basically the sprites move around a rectangular space at angles of 45 degrees. The sprite that the user controls can move in the normal four directions and also diagonally. As they overlap each other the sprites higher up the screen need to be drawn first. Should I base the sort on the Y-axis or have a separate variable ? I also don't want to sort the sprites every frame so how should I be checking to know when to resort the list ?

I have put together a short video that shows the game running so you can see the sprite movement. I don't expect there to be more than 6 moving sprites on screen at anyone time.

http://uk.youtube.com/watch?v=tl9ls02onj0

Ñuño Martínez
07-10-2008, 08:54 AM
I think the best way is to develop a 3D axis system and then map the coordinates. May be something like this:(* iX - "Width" input.
* iY - "Height" input.
* iZ - "Depth" input.
* oX - "Width" output.
* oY - "Height" output. *)
PROCEDURE Map3D_2D (iX, iY, iZ: INTEGER; VAR oX, oY: INTEGER);
BEGIN
oY := iZ + iY;
oX := iX + iZ;
END;
Didn't test this but I think you get the idea, didn't you? This way you can order the objects by its Z axis easy.

Traveler
07-10-2008, 09:49 AM
I assume you have a x and y value for the sprites. If that's the case then just use a quicksort algorithm to sort on the y value of the sprite.
I use this method as well for my game and so far its working pretty well.

cairnswm
07-10-2008, 02:27 PM
I keep a list of all my sprites ordered by z-Order (usually based on Y position of feet). Before a sprite moves I take it out of the list, and after it moves I put it back in the right drawing order position.

This is the same system as DelphiX uses.

ianatwork
09-10-2008, 07:41 AM
Thanks for all your input. I'm now sorting my sprites based on the y value plus the height of the sprite to get the feet position. I now need to implement some collision detection and that should complete the sprite movement code.