Page 1 of 1

Sprite sorting?

Posted: Mon Jul 14, 2008 4:34 am
by cdoty
I'm writing a dynamic sprite list routine, and need to sort the list.

I'm curious to find out what value everyone would sort on?

My thought would be the bottom on the sprite. This assumes that the sprite data is justified to the bottom of the sprite, and sprites always appear on the ground (no sprite is floating). There would be a problem with layering of sprites at the same location.

A 'z' position might be better. The only issue I can see is finding a way to ensure a sprite can draw over the top of another sprite, but not draw over a higher priority sprite. For example, the explosion of a sprite should show over the sprite, but not draw over any other sprite at the same level, that is a higher priority. I guess I could have a sprite attachment field, and not sort the attachments. This would also have the advantage of allowing multiple attachments to a sprite, and the order of the attachments could be controlled, similar to the sprite link field. And, the position of the attachment would be automatically controlled.

Posted: Mon Jul 14, 2008 6:54 am
by tomaitheous
If I understand you correctly, you what to simulate 'planes' of sprites. I mean, lower priority sprites have lower priority explosions/etc that don't overlay on the upper pseudo sprite plane, correct?

If that's the case, then I'd just section off the sprite table into 2 or more pseudo planes. Unless it's something pretty complicated, that would be easier in my opinion than writing a dynamic sprite display list.

Posted: Mon Jul 14, 2008 12:06 pm
by TmEE co.(TM)
For priority stuff, I arrange the code in my stuff so that high priority sprites (like explosions that have to cover everything) are entered into the list first, then not so high ones and so on... works nicely.

Posted: Tue Jul 15, 2008 5:38 am
by cdoty
What if the objects change 'z' positions on the screen? Then you have to insert and remove them into the list.

I've started the routine; it isn't looking too complicated, at this point. I've coded everything, except for the attachment part. I still have to test it.

I plan to put up an example of the routine, with source code.

Posted: Tue Jul 15, 2008 4:05 pm
by TmEE co.(TM)
For changing Z pos, I don't know really... I'm more of a half dynamic stuff user... I have plenty of hardcoding done in my stuff (as part of optimizations, I need as much performance as possible... to do so, keeping things simple, at least for me).

Posted: Tue Jul 15, 2008 7:12 pm
by Chilly Willy
Make a fake Z value for each sprite with certain levels predefined for things like explosions. Binary sort the list to order everything before generating the sprite list. If you keep the Z value range small, the binary sort will be fast. A binary sort is b*N, where b is the number of bits in the field to sort by, and N is the number of elements in the list.