local comparisons = {} function comparisons.zsort(itemA, itemB) local _, aY, aZ, _, aH, aD = itemA.mainHitbox:getCube() local aDepth, aID, aType aDepth = itemA.depth aID = itemA.creationID aType = itemA.type aZ = math.ceil(aZ) aY = math.ceil(aY) local _, bY, bZ, _, bH, bD = itemB.mainHitbox:getCube() local bDepth, bID, bType bDepth = itemB.depth bID = itemB.creationID bType = itemB.type bZ = math.ceil(bZ) bY = math.ceil(bY) --print("comparing " .. aID .. " to " .. bID) --print("a", aY, aZ, aH, aD, aDepth, aID, itemA.type) --print("b", bY, bZ, bH, bD, bDepth, bID, itemB.type) local comparison = 0 if aZ >= bZ + bD then -- item A is completely above item B --graph:add(itemB, itemA) comparison = 1 elseif bZ >= aZ + aD then -- item B is completely above item A --graph:add(itemA, itemB) comparison = -1 elseif aY + aH <= bY then -- item A is completely behind item B --graph:add(itemA, itemB) comparison = -1 elseif bY + bH <= aY then -- item B is completely behind item A --graph:add(itemB, itemA) comparison = 1 elseif aY + aH > bY + bH then --(aY - aZ) + aH > (bY - bZ) + bH then -- item A's forward-most point is in front of item B's forward-most point --graph:add(itemB, itemA) comparison = 1 elseif aY + aH < bY + bH then --aY < (bY - bZ) + bH then -- item B's forward-most point is in front of item A's forward-most point --graph:add(itemA, itemB) comparison = -1 else -- item A's forward-most point is the same than item B's forward-most point if aDepth > bDepth then --graph:add(itemB, itemA) comparison = 1 elseif aDepth < bDepth then --graph:add(itemA, itemB) comparison = -1 else if aID > bID then --graph:add(itemA, itemB) comparison = 1 elseif aID < bID then --graph:add(itemB, itemA) comparison = -1 end end end return comparison == -1 end return comparisons