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?
Draw order depending on Y position
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- SimonLarsen
- Party member
- Posts: 100
- Joined: Thu Mar 31, 2011 4:47 pm
- Location: Denmark
- Contact:
Re: Draw order depending on Y position
You will need to sort the entities on their Y-coordinate so you can drawn them back to front.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?
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.
- 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
Lua has a sorting function already built-in: table.sort. No need to implement your own.
When I write def I mean function.
Re: Draw order depending on Y position
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:
This is, of course, not what you wanted. Figured it could be helpful, though.
Code: Select all
table.sort( Table, function( First, Second ) return First > Second end )
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
Re: Draw order depending on Y position
to elaborate on that; you'd want: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:This is, of course, not what you wanted. Figured it could be helpful, though.Code: Select all
table.sort( Table, function( First, Second ) return First > Second end )
Code: Select all
table.sort(drawables, function(a, b) return a.y > b.y end)
Re: Draw order depending on Y position
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.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 7 guests