#### Archived

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.

## 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 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}

ah, thanks

1. 1
2. 2
3. 3
Rutin
19
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633659
• Total Posts
3013207
×