Archived

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

Jonoxon

Here's an easy one for you...

Recommended Posts

I am trying to get Z-Ordering working. Objects are(supposed to be) drawn in order of their Y position, from least to greatest. So that objects which are higher(lower Y value) on the screen appear behind those which are lower(higher Y value). What is the best way to do this? (I have to do it for every frame) Jonoxon

Share this post


Link to post
Share on other sites
The articles section of GameDev gives some answers while a quick search on the web may point you to the tutorials for programming Pocket PC by Jacco Bikker aka The Phantom.

Ghostly yours,
Red.

Share this post


Link to post
Share on other sites
You''ll find details on a quicksort algorithm. The first sort will take the longest. Hopefully afterwards your objects will stay in y order (in your linked list or however you represent them) from frame to frame. Some suggest a quicker algorithm for a mostly sorted list (bubblesort or radix?) for subsequent sorts.

You may need to experiment to find the one that''s right for you.

Share this post


Link to post
Share on other sites
Quicksort's worst-case scenario is on a nearly-ordered list. Randomize the list if the implementation you're using doesn't already. Or, like Waverider said, choose another algorithm for subsequent sorts.

(NOTE: I do not know anything about z-sorting, just talking about sorting in general)



[edited by - Thrump on June 6, 2002 12:11:40 PM]

Share this post


Link to post
Share on other sites
Yes, I did read all of the articles on sorting algorithms and search the internet etc. I am currently using a system where I scan all the objects, and put the order into a separate array that tells the engine what order to blt the sprites.

I guess what I''m looking for is the best algorithm for an array that changes every frame. It''s FF1(Final Fantasy on SNES) style sprites that move around alot. I could just block the sprites from going behind each other, but I want to allow that.

Jonoxon

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Insertion sort''s a really nice sort if you know which of your sprites are moving...

Bubble sort''s also good if only a few elements are out of place.

Share this post


Link to post
Share on other sites
Thanks for the suggtestions, they were a great help.

Here is the code I''m currently using, it''s basically a bubble sort. Sorry for the lack of commentataion, but it should be pretty easy to figure out. I do this once per frame.


bool bOK;

for(int i=0;i=i; //Make the list


do {
bOK=true;
for(i = 0; i if(m_actors[i].pt.y >= m_actors[i+1].pt.y) {
int temp;
temp=m_iZOrder[i];
m_iZOrder[i]=m_iZOrder[i+1];
m_iZOrder[i+1]=temp;
}
}
}
while(!bOK);

for(int h=0;h<2-NUM_SPRITES;h++) {
i=m_iZOrder[h];
Blt(actor[i]);
}



Jonoxon

Share this post


Link to post
Share on other sites