Problem with drawing only the cubes on screen.
Posted: Thu Aug 09, 2018 1:06 pm
So I decided to optimize my game by only drawing cubes that are visible ( already been doing that, and by that I will explain in a bit ) and those that are on screen. Problem is, there are only so many cubes on the screen at a time, yet it tells me it draws around 900+ of them.
Now by visible I mean cubes that should theoretically be visible from the isometric point of view. Cubes that have a neighbor to the top, left and right are "not visible". If any of those three conditions is not met, then it "is visible".
After that I use two functions I made, normalToIsoX and normalToIsoY to find out the x,y coordinates of the cube, based on its x,y,z coordinate on the grid. After that I use cam:cameraCoords() on isoX and isoY to find the cubes x,y on the screen ( I use hump.camera )
Now here is the code :
I have no idea what is causing this problem. Could anyone help me please? Thank you.
When I'm zoomed in at 100% ( default zoom and above ) it's around 900+. At 200% or when I'm at the edge, around 700+. At some point, the number of drawn cubes caps at 1200+ when I zoom out enough.Now by visible I mean cubes that should theoretically be visible from the isometric point of view. Cubes that have a neighbor to the top, left and right are "not visible". If any of those three conditions is not met, then it "is visible".
After that I use two functions I made, normalToIsoX and normalToIsoY to find out the x,y coordinates of the cube, based on its x,y,z coordinate on the grid. After that I use cam:cameraCoords() on isoX and isoY to find the cubes x,y on the screen ( I use hump.camera )
Now here is the code :
Code: Select all
t.cubeIsVisible = function(x,y,z)
local xE = false
local yE = false
local zE = false
if (x + 1) > #grid then xE = true end
if (y + 1) > #grid[x] then yE = true end
if (z + 1) > #grid[x][y] then zE = true end
if xE or yE or zE then return true end
local n = 0
if grid[x + 1][y][z] ~= 0 then n = n + 1 end
if grid[x][(y + 1)][z] ~= 0 then n = n + 1 end
if grid[x][y][(z + 1)] ~= 0 then n = n + 1 end
local screenX, screenY = cam:cameraCoords(gridFuncs.normalToIsoX(x,y),gridFuncs.normalToIsoY(x,y,z,3))
if n == 3 then
return false
elseif not (((screenX < -100*zoom) or (screenX > wW + 100*zoom)) and ((screenY < -100*zoom) or (screenY > wH + 100*zoom))) then
return true
end
end