Draw order depth

Started by
1 comment, last by Vrokulaf 11 years, 1 month ago

Hi dudes.

So I don't really know what this particular thing is called, so I decided to call it "draw order depth" because it kindof describes what it is.

SO, I've been having some trouble figuring out this relatively simple thing, depth in 2D.

So imagine you're a dude running around in a semi-tilted camera view (or isometric), and you want to run behind a tree or a house. What SHOULD happen is that the house should now be in front of you, obstructing the view for the camera. Very basic concept, things closer to the camera should be drawn last, so the things behind it gets drawn over (and that way, creating depth). I included an image illustrating what I'm talking about.

draworderdepth.jpg

So it seems simple enough, but what is a good and efficient way of coding this? Is it putting everything in an array, and using an algorithm to sort them in order how far away from the camera they are, then just run through the list? If thats the case, how do you do it efficiently?

One thing I tried, but which took up way too much CPU, was to cut the screen up to slices horizontally, and go through every slice, check every object to see if they are positioned on that particular slice, and if they were, draw them. Were I thinking in the right direction?

Any answer is appreciated!

Have a good one

~Vrokulaf

Freelancing musical composer!
For more info check out my website!

Advertisement

give each object a 'depth' value and during rendering store each object in an array (but do not draw it yet).

When all objects are recorded, sort the array and do the actual drawing.

This way, objects with a lower depth will be drawn first. For dynamic objects (like the player), you can update the depth value as it moves.

For the sorting, you could probably use any sort algorithm but if you use an integer as depth value, radix sort might be the fastest.

Yes, was just gonna write, I figured it out myself. I actually wrote it in the post even, and thought something like "Hey, I should actually try that out...".

If anyone is interested I can post the source code here, I wrote a very simple engine to handle the drawing order.

Freelancing musical composer!
For more info check out my website!

This topic is closed to new replies.

Advertisement