Skip to main content
gamedev.net
🔒 Locked

sprite ordering

Started by Brad8383 Dec 7, 2001 at 12:32 PM 3 replies 350+ views
Original Post
Brad8383
Brad8383
I have my sprites in an stl list and i thought sorting them by x*y would order them right but it doesnt. How would you order the list so it will render them right?(like render the ones with the less y value first and if they have the same y value render the ones with the less x value first and so on)
krez
krez
do you know how many columns you have (i.e. the highest possible x value)? if so, sort them by ((y * max_x_value) + x).

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
TANSTAAFL
TANSTAAFL

you might use y * w + x for your sort, and you''ll likely get better results, where w is the width of worldspace in pixels or whathaveyou.

also, if the x and y come from the upper left corner of the image, then it still won''t work for all cases, unless all of your sprites are of uniform size. you need to set an anchoring point for your sprites, and use that position instead. typically, the anchoring point will be near the middle of the bottom, where the "feet" are.
Get off my lawn!
Brad8383
Brad8383
Now its working inversly(a sprite with a y*w+x value comes before a sprite with a higher y*w+x) Im using an stl list sort. The < operator looks like this.
bool DxObj::operator<(const DxObj &a) const
{
return ((Objinfo.AbsPixPos.y+Objinfo.SortPoint.y)*(Objinfo.Map->size()*Objinfo.Mapinfo->TileSize.x)+Objinfo.AbsPixPos.x+Objinfo.SortPoint.x)
< ((a.Objinfo.AbsPixPos.y+a.Objinfo.SortPoint.y)*(a.Objinfo.Map->size()*a.Objinfo.Mapinfo->TileSize.x)+a.Objinfo.AbsPixPos.x+a.Objinfo.SortPoint.x);
}
and everytime i add a sprite in or move a sprite i sort the list.
Brad8383
Brad8383
If i change the return value to use greater than instead of less than then it works fine.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.