Sign in to follow this  
Vrokulaf

Draw order depth

Recommended Posts

Vrokulaf    106

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

Share this post


Link to post
Share on other sites
Yourself    1961

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.

Share this post


Link to post
Share on other sites
Vrokulaf    106

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this