Jump to content
  • Advertisement


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


Sorting objects for drawing order...

This topic is 5733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m having some major trouble sorting objects in a scene for rendering... *(my engine is rectangular, not isometric) each object (character, item, etc... with a sprite attached) in the scene has an x,y,z map coordinate (1 coordinate = 1 pixel) and an axis aligned bounding box. Assuming that the bounding boxes can never consume each other''s space, how can you sort all of the objects into an array in the correct order to be drawn on to the screen? I''ve tried many different algorithms for this and I can''t seem to get it working right thanks

Share this post

Link to post
Share on other sites
Is this a 2d or 3d engine? You have a z-coordinate, but I have the feeling that you''re talking about a 2d engine, so I''ll go from there.

You should just need to sort them by the top-to-bottom. The coordinate you want to sort by is the bottom-right corner of your bounding box.

Since you also have a z-coordinate, you will want to sort by that first. I''ll assume that a lower z-coordinate refers to the "bottom" layer.

Given objects A and B with coordinates (Ax, Ay, Az) and (Bx, By, Bz), where the coordinates coorespond to the bottom-right (greatest X, Y and Z position) of the object.

This funciton returns -1 if A < B, 0 if A = B and 1 if A > B.
(A < B means that A is drawn before B)

Compare(A, B)
if ( Az < Bz ) return -1
else if ( Bz < Az ) return 1

if ( Ay < By ) return -1
else if ( By < Ay ) return 1

if ( Ax < Bx ) return -1
else if ( Bx < Ax ) return 1

// same position!
return 0

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!