#67570 - gauauu - Thu Jan 19, 2006 2:43 am
Hey all, I'm getting ready to start an algorithm to sort my sprite list, based on screen position (on a top-down, almost isometric view, similar to secret of mana).
I had some ideas about how to implement it, but wanted to run my thoughts by some of you smart folk to see if anyone has any suggestions.
Basically, I want to sort to keep my sprites on the bottom (more "front") part of the screen drawn first, so must sort them to the front of the sprite data.
Here are the things to consider:
1. There won't be that many sprites to sort. (gba doesn't have so many sprites, and probably more likely I'll only use 30-40 at a time)
2. Because object don't move extremely fast, the array will almost always be "almost sorted"
So, my thought, which runs contrary to everything that I feel like is good and reasonable, is to do a type of bubble sort.
Each frame, I would do ONE pass of the bubble sort, swapping entries as needed. Then, I wouldn't worry about the remaining out-of-order sprites until the next frame of gameplay.
Doing this way, each frame, I am guaranteed only one pass through the list. And as most of the time, only 1 or 2 sprites will have swapped places, a simple bubble sort swap will fix them.
The nice things about using a bubble sort in this situation are:
-usually if a sprite will be out of order, it will only need to be moved to an adjacent slot, not to be inserted at some completely other position
-with other sorting methods, it's harder to isolate it down to just 1 pass through the list. With insertion, for example, it can take 2 passes just to fix one item. Or with something like quicksort, it mangles the list to pieces while the sorting is in process, so you have to do the sort all at once, instead of doing it a piece at a time. The bubble sort can sort of be in a state of perpetual "sorting", where after you do one pass, you can keep using the list, even if it is slightly out of order
For more complicated situations, this method might take multiple frames before it finally has the list properly sorted. Which means that for the worst cases, there might be a few frames where sprite priorities are wrong and the screen sprites look funny. But in all but the very nasty cases, (all sprites but one lined up on the same scanline, the one sprite passing by them all) it shouldn't be more than a small handful of frames, and be barely noticable (especially because the sprite priorities only matter when the un-sorted sprites overlap). I figure this would probably be a fair tradeoff to guarantee that my sorting doesn't end up taking a huge amount of time.
Anyway, am I missing something big? Any thoughts?
I had some ideas about how to implement it, but wanted to run my thoughts by some of you smart folk to see if anyone has any suggestions.
Basically, I want to sort to keep my sprites on the bottom (more "front") part of the screen drawn first, so must sort them to the front of the sprite data.
Here are the things to consider:
1. There won't be that many sprites to sort. (gba doesn't have so many sprites, and probably more likely I'll only use 30-40 at a time)
2. Because object don't move extremely fast, the array will almost always be "almost sorted"
So, my thought, which runs contrary to everything that I feel like is good and reasonable, is to do a type of bubble sort.
Each frame, I would do ONE pass of the bubble sort, swapping entries as needed. Then, I wouldn't worry about the remaining out-of-order sprites until the next frame of gameplay.
Doing this way, each frame, I am guaranteed only one pass through the list. And as most of the time, only 1 or 2 sprites will have swapped places, a simple bubble sort swap will fix them.
The nice things about using a bubble sort in this situation are:
-usually if a sprite will be out of order, it will only need to be moved to an adjacent slot, not to be inserted at some completely other position
-with other sorting methods, it's harder to isolate it down to just 1 pass through the list. With insertion, for example, it can take 2 passes just to fix one item. Or with something like quicksort, it mangles the list to pieces while the sorting is in process, so you have to do the sort all at once, instead of doing it a piece at a time. The bubble sort can sort of be in a state of perpetual "sorting", where after you do one pass, you can keep using the list, even if it is slightly out of order
For more complicated situations, this method might take multiple frames before it finally has the list properly sorted. Which means that for the worst cases, there might be a few frames where sprite priorities are wrong and the screen sprites look funny. But in all but the very nasty cases, (all sprites but one lined up on the same scanline, the one sprite passing by them all) it shouldn't be more than a small handful of frames, and be barely noticable (especially because the sprite priorities only matter when the un-sorted sprites overlap). I figure this would probably be a fair tradeoff to guarantee that my sorting doesn't end up taking a huge amount of time.
Anyway, am I missing something big? Any thoughts?