Here's an easy one for you...

Started by
5 comments, last by Jonoxon 21 years, 10 months ago
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
Advertisement
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.
Ghostly yours,Red.
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.
It's not what you're taught, it's what you learn.
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]
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
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.

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.pt.y >= m_actors[i+1].pt.y) {<br> int temp;<br> temp=m_iZOrder;<br> m_iZOrder=m_iZOrder[i+1];<br> m_iZOrder[i+1]=temp;<br> }<br> }<br>}<br>while(!bOK);<br><br>for(int h=0;h<2-NUM_SPRITES;h++) {<br> i=m_iZOrder[h];<br> Blt(actor);<br>}<br><br><br><br>Jonoxon </i>

This topic is closed to new replies.

Advertisement