Page 1 of 1

[SOLVED] Z order drawing

Posted: Wed Jul 11, 2018 10:31 am
by var77
Hi everyone i came back here to ask a little question about drawing order.
I wanted to know if actually there are any way to control drawing order.
To be more precise i know there is this tricky tutorial https://love2d.org/wiki/Tutorial:Drawing_Order
but in my issue i got random object that not appear at the same time.

For example imagine that you want a simple Space Invader (galaga) and
want to control the drawing Z order of random incoming enemy, is that possible ?

Thanks in advance.

Re: Z order drawing

Posted: Wed Jul 11, 2018 11:17 am
by grump
You can use the sorted table approach as shown in the tutorial. Keep your enemy sprites in a table and keep that table sorted according to a z value that you assign each enemy. Draw sprites in the order they have in the table.

table.sort is not a stable sort though. Enemies with the same z value will swap order. A better approach would be to use binary search to find the correct insertion location, this keeps the list always sorted and stable.

Re: Z order drawing

Posted: Wed Jul 11, 2018 2:37 pm
by var77
Thanks grump for your answer.

After a noon of test i try to handle it i found what i was searching on the forum there viewtopic.php?t=10628.
That was what you wrote draw them by adding a Z value and "sort" them.

In my case the swaping order is not a real problem i guess i could deal
with it for the moment. ^^