Friday, September 29, 2023
HomeSample Page

Sample Page Title

So, first submit of the primary sport.
Meh. I’m purported to be fascinating.


I’ll write a tad about my downside with setting the drawing order of photographs.
My sport is meant to have plenty of photographs on prime of one another, and at first they had been simply drawn so as of kind (objects can be drawn first, then folks) after which by creation order (so folks created final would get drawn on prime of each different particular person). This was clearly lower than very best.

This is a picture of the issue (left), subsequent to the answer (proper)

So I appeared for sorting algorithms, and with me being a n00b, a easy algorithm can be appropriate. So I checked Wikipedia’s Sorting algorithm web page. A usually quick algorithm is good however:

  • Algorithms will be less complicated or extra difficult, with difficult algorithms taking extra time to implement
  • Different algorithms are quicker in several situations
  • Some algorithms are secure that 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 might need to keep away from flickering attributable to two photographs being moved in direction of the entrance after which in direction of the again by the frames
  • Some algorithms are adaptive that means that if they’re near be so as, it’s quicker. Important as a result of between frames more often than not the sorting order will stay the identical

So I simply determined to select 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… trying on the output numbers every part appeared nice, 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 >
        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 go an offset operate for the sorting standards, on condition that I do not truly need 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 peak (edit: @ThemsAllTook observed half peak will not be very best both) to allow them to be sorted by their heart contact level with the ground, not their origin.

That labored nicely, and after sorting by the X axis, I simply repeated the sorting for the Y axis. So sure! Finally nobody’s ft are on nobody’s face!

I do not count on plenty of (any?) folks to learn this, however in the event you 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.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments