Sign in to follow this  
chris2307

Changing the Render Order

Recommended Posts

chris2307    163
Hi folks,

I am making an isometric game in 2D and am having some difficulty in coming up with a solution to my current problem.

If I have lots of little characters (just an example) running around the screen, I want to know what order to render those sprites in. I already know that I can do this simply by knowing each of the characters Y coordinate (If the Y coordinate is lower than one sprite, then it should be rendered in front).

I have already written a sample program that gives the illusion of a ball orbiting around a pole. Trouble is, if I have lots more than just two sprites - for example 20 sprites... it becomes a little more complicated.

I am struggling because obviously, a web of IF statements will not suffice. I would like to know if there is a common method that people may use in this situation? And, lets say for example I was able to get all the objects Y pos and order some kind of virtual Z buffer array that will help me order the rendering, how would I go about this for those objects already contained in a vector (A vector containing 10 generic automated characters for example).

Any thoughts on this? (That is, if I have explained myself correctly). I am using Direct3D 9.0 and C++ although I am sure languages and library's do not really come in to this much.

Thanks

Chris

Share this post


Link to post
Share on other sites
CryoGenesis    528
[quote name='chris2307' timestamp='1352927413' post='5001013']
Hi folks,

I am making an isometric game in 2D and am having some difficulty in coming up with a solution to my current problem.

If I have lots of little characters (just an example) running around the screen, I want to know what order to render those sprites in. I already know that I can do this simply by knowing each of the characters Y coordinate (If the Y coordinate is lower than one sprite, then it should be rendered in front).

I have already written a sample program that gives the illusion of a ball orbiting around a pole. Trouble is, if I have lots more than just two sprites - for example 20 sprites... it becomes a little more complicated.

I am struggling because obviously, a web of IF statements will not suffice. I would like to know if there is a common method that people may use in this situation? And, lets say for example I was able to get all the objects Y pos and order some kind of virtual Z buffer array that will help me order the rendering, how would I go about this for those objects already contained in a vector (A vector containing 10 generic automated characters for example).

Any thoughts on this? (That is, if I have explained myself correctly). I am using Direct3D 9.0 and C++ although I am sure languages and library's do not really come in to this much.

Thanks

Chris
[/quote]

There are two ways I do isometric rendering.

One is kind of slow and relatively complicated for a new dev. One way I do isometric rendering is to give each pixel a number representing it's distance from the virtual camera. So, you start by drawing a tree which has a closeness of it's y value. if the player is in front of the tree then it's y value must be bigger.
Instead of using if statements I used an 2D array of integers which each represent a pixel on the screen. When you draw an image on the screen it writes that corresponding integer in the array to the y value of the image. If another image is trying to be drawn over another image then the image's y value has to be bigger than the one on the screen. I'm finding it hard to explain.
Another method would be to have tiles on the screen then add the objects onto the tiles then when the tile is rendered isometrically then the object is rendered too.

Share this post


Link to post
Share on other sites
KnolanCross    1974
If I got the problem right, you have several objects and you want to render the ones with bigger Y first, correct? If that is the case, this should do:

1) Place all the objects you want to render in a list.
2) Sort this list by the Y axis (biggest Y will be the first of the list, lowest Y will be the last one).
3) For each object in the list, render it.

Hope it helps.

Share this post


Link to post
Share on other sites
chris2307    163
I did think about having a tile based game but as I have never used a tile based method before, I kind of cast the idea away. At the moment, I am trying to stick to what I know but if a tile map would be easier than checking against the values for each sprite, then maybe I should look in to this.

And unfortunately, I do not [i]fully [/i]understand what you were trying to explain for your other method but thanks anyway!

I think I will look at adding tiles to the screen unless anyone else has another method that may fit better?

Share this post


Link to post
Share on other sites
chris2307    163
Am I able to do this with a vector? For example, if I have a vector of 6 objects, can I re-order that vector based on the Y position (which is a value returned from an object contained in the vector anyway)? If I can do this, then I just need to re-order the vector before I render don't I?

Share this post


Link to post
Share on other sites
KnolanCross    1974
"Yes you can!"

If it is a STL vector (the one that comes by default), you should use the sort function from algorithm. Here is some documentation:
http://www.cplusplus.com/reference/algorithm/sort/

On the rendering, I don't know how Direct3D works, so I can't really help. I
If you render every object every frame, just render every element of your objects vector and you should be fine.

EDIT: Didn't see that you said you are using CPP, corrected some info. Edited by KnolanCross

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