Page 1 of 1

Question about depth (Z-axis)

Posted: Sat Dec 05, 2015 10:10 am
by ElmuKelmuZ
I'm writing some sort of an engine/framework on top of Löve, and have a question.

I have a Vector2 pseudoclass( https://gist.github.com/Elmuti/b1d37f2b5784be76a683 )that I use for Position and Velocity.

My framework/engine will be very object oriented, meaning that I will have UI classes such as Frames, Textboxes, Textlabels etc.
However I know that I need a depth system to define how all the 2d objects on screen layer on top of eachother.
I was wondering if I should just have a ZIndex property in all drawn classes that defines the drawing order, or if I should use Vector3's instead, and having the Z-axis as depth. Or maybe even a completely different approach.

What do you guys think I should do?

Re: Question about depth (Z-axis)

Posted: Sat Dec 05, 2015 5:31 pm
by Relazy
ElmuKelmuZ wrote: However I know that I need a depth system to define how all the 2d objects on screen layer on top of eachother.
I was wondering if I should just have a ZIndex property in all drawn classes that defines the drawing order, or if I should use Vector3's instead, and having the Z-axis as depth.
I dont think that using either vector3's or zindex property will have make considerable difference so go with what you feel is right.
I have a UI system like yours that uses zindex, I draw my items as such:

Code: Select all

--@main.lua
function table.zsort(t)
  local a = {}
  for _,n in pairs(t) do table.insert(a, n) end
	table.sort(a,function(a,b) return a.zmap < b.zmap end)
  return a
end
--@ui:draw()
for i,v in ipairs(table.zsort(self.items)) do 
v:draw()
end
if you wanted to use vectors instead:

Code: Select all

--@ui
function self:sortOrder(t)
  local a = {}
  for _,n in pairs(t) do table.insert(a, n) end
	table.sort(a,function(a,b) return a.vector.z < b.vector.z end)
  return a
end