Page 1 of 1

Draw order depending on Y position

Posted: Thu Apr 17, 2014 2:20 pm
by Jeeper
Hello there! I am working on a game that has a "2.5d" camera angle, just like Final Fight (http://img.gamefaqs.net/screens/7/0/7/gfs_62773_2_8.jpg)

I just wonder if anyone knows a good way to make sure that all objects/players/entities are drawn in a order that changes depending on their Y axis. So the further up an object is, the earlier it gets draw. And the further down an object is, the later it gets drawn.

Anyone done something similar or have any ideas?

Re: Draw order depending on Y position

Posted: Thu Apr 17, 2014 2:28 pm
by SimonLarsen
Jeeper wrote:Hello there! I am working on a game that has a "2.5d" camera angle, just like Final Fight (http://img.gamefaqs.net/screens/7/0/7/gfs_62773_2_8.jpg)

I just wonder if anyone knows a good way to make sure that all objects/players/entities are drawn in a order that changes depending on their Y axis. So the further up an object is, the earlier it gets draw. And the further down an object is, the later it gets drawn.

Anyone done something similar or have any ideas?
You will need to sort the entities on their Y-coordinate so you can drawn them back to front.
Make sure to use a sorting algorithm that works well on almost sorted input such as Smoothsort or Cocktail sort or even something like Insertion sort or Bubble sort provided you won't have too many entities on screen at once.

Re: Draw order depending on Y position

Posted: Thu Apr 17, 2014 6:53 pm
by kikito
Lua has a sorting function already built-in: table.sort. No need to implement your own.

Re: Draw order depending on Y position

Posted: Thu Apr 17, 2014 9:50 pm
by davisdude
table.sort would return the lowest ones first, by default. This would work, but just for kicks, you could also get the highest y-value first with this:

Code: Select all

table.sort( Table, function( First, Second ) return First > Second end )
This is, of course, not what you wanted. Figured it could be helpful, though.

Re: Draw order depending on Y position

Posted: Thu Apr 17, 2014 11:42 pm
by Ragzouken
davisdude wrote:table.sort would return the lowest ones first, by default. This would work, but just for kicks, you could also get the highest y-value first with this:

Code: Select all

table.sort( Table, function( First, Second ) return First > Second end )
This is, of course, not what you wanted. Figured it could be helpful, though.
to elaborate on that; you'd want:

Code: Select all

table.sort(drawables, function(a, b) return a.y > b.y end)
where drawables is an array-style table with all the drawable objects in it. it's unlikely performance will matter enough that you need to consider using specific sorting algorithms

Re: Draw order depending on Y position

Posted: Fri Apr 18, 2014 1:09 am
by Jeeper
Thanks for the help! I was going to do the whole "1 table and sort it" thing but was unsure of if it would cause performance issues or not, specially when you need to do it with a lot of entities. But it doesn't seem to cause any noticeable effect.