[ad_1]
Meh. I’m presupposed to be fascinating.
So!
I’ll write a tad about my downside with setting the drawing order of photos.
My sport is meant to have a whole lot of photos on prime of one another, and at first they have been simply drawn so as of sort (gadgets could be drawn first, then individuals) after which by creation order (so individuals created final would get drawn on prime of each different individual). This was clearly lower than very best.
This is a picture of the issue (left), subsequent to the answer (proper)
So I regarded for sorting algorithms, and with me being a n00b, a easy algorithm could be appropriate. So I checked Wikipedia’s Sorting algorithm web page. A typically quick algorithm is sweet however:
- Algorithms will be easier or extra difficult, with difficult algorithms taking extra time to implement
- Different algorithms are sooner in numerous situations
- Some algorithms are steady which means that issues with the identical place will not be moved; I’m utilizing floating level for X and Y positions in order that’s unlikely, however I’d wish to keep away from flickering because of two photos being moved in direction of the entrance after which in direction of the again by the frames
- Some algorithms are adaptive which means that if they’re near be so as, it’s sooner. Important as a result of between frames more often than not the sorting order will stay the identical
So I simply determined to choose the insertion kind algorithm.
Thanks to Wikipedia’s web page including insertion kind was solely translating pseudocode to Lua code, and switching from zero-based arrays (in Lua, arrays begin at 1).
I did hit a small snag after implementing it… wanting on the output numbers all the things regarded positive, however now a few of the drawables weren’t being drawn. Turns out that I’m sorting by place within the X axis (Y-axis comes later), however I’m sorting by properties of objects, however what I’m sorting are the objects themselves. So I simply made a small modification to the algorithm to separate the sorting standards from what’s being sorted:
This is Wikipedia’s pseudocode (for zero-based arrays)
i ← 1
whereas i < size(A)
x ← A[i]
j ← i – 1
whereas j >= 0 and A[j] > x
A[j+1] ← A[j]
j ← j – 1
finish whereas
A[j+1] ← x
i ← i + 1
finish whereas
So at this level mine, as a result of my arrays begin at 1 and since I’m sorting objects by their property, is that this:
i ← 2
whereas i < size(A)
obj_ref ← A[i]
j ← i – 1
whereas j > 0 and A[j].property > obj_ref.property
A[j+1] ← A[j]
j ← j – 1
finish whereas
A[j+1] ← obj_ref
i ← i + 1
finish whereas
After that I added the flexibility to cross an offset operate for the sorting standards, provided that I do not really wish to kind by the X and Y axis of the pictures, as a result of these are on the prime left nook; the offset operate simply provides half width and half top to allow them to be sorted by their middle, not their origin.
That labored properly, and after sorting by the X axis, I simply repeated the sorting for the Y axis. So sure! Finally nobody’s toes are on nobody’s face!
I do not anticipate a whole lot of (any?) individuals to learn this, however for those who do, know I’ll proceed posting however it is going to be erratically at first till I get into an schedule.
Finally, as a result of I copy-pasted the pseudocode from Wikipedia I’m required to speak legalese and say this devlog entry is licensed to the general public underneath the CC-BY-SA 4.0 License.
[ad_2]