Draw order depending on Y position

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Draw order depending on Y position

Post 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?
User avatar
SimonLarsen
Party member
Posts: 100
Joined: Thu Mar 31, 2011 4:47 pm
Location: Denmark
Contact:

Re: Draw order depending on Y position

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Draw order depending on Y position

Post by kikito »

Lua has a sorting function already built-in: table.sort. No need to implement your own.
When I write def I mean function.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Draw order depending on Y position

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Ragzouken
Citizen
Posts: 84
Joined: Fri Aug 10, 2012 7:59 am
Contact:

Re: Draw order depending on Y position

Post 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
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: Draw order depending on Y position

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests