Page 1 of 1

Help: Isometric map drawing incorrectly

Posted: Mon Jan 08, 2018 12:54 am
by Ostego160
Greetings!

I am making an isometric grid composed of nodes(intersections) and tiles. I want the tiles to update when the nodes are adjusted. There are no errors however, one of the isometric tile coordinates is wrong (I believe its the 4th coordinate) and its causing the tiles to be drawn wrong.

Here is the main.lua in its entirety: The .love and pic are attached as well to demonstrate my issue.

Code: Select all

Spectra =   require('spectra')

local lg = love.graphics
local push = table.insert
local width, height = lg.getDimensions()
-------------------------------
-- Helpers
local function dist(x1,y1, x2,y2) return ((x2-x1)^2+(y2-y1)^2)^0.5 end

local function get2D(index, width)
   local i = index - 1
   x = i%width
   y = (i - x)/width
   return x + 1, y + 1
end

local function get1D(x,y, width)
   return x + (y - 1)*width
end

local function createGrid(tw,th, w,h, cx,cy)
   local nodes,tiles = {},{}
   for col=1,w+1 do
      for row=1,h+1 do
         local index = get1D(col,row,w)
         local _x,_y = (col-row)*tw/2+cx, (col+row)*th/2+cy
         nodes[index] = {x=_x, y=_y}
         if (col<=w and row<=h) then
            tiles[index] = lg.newMesh({   {_x,_y},
                                          {_x+tw/2,_y+th/2},
                                          {_x,_y+th},
                                          {_x-tw/2,_y+th/2}
                                       })
         end
      end
   end
   return nodes, tiles
end

function love.load()
   spectra = Spectra()

   range = 512

   gridWidth, gridHeight = 32,32
   nodes,tiles = createGrid(64,32, gridWidth, gridHeight, lg.getWidth()/2,0)
   --Create nodes and tiles 1D arrays for 2D grid
end

function love.update(dt)
   for k,mesh in ipairs(tiles) do
      local tx,ty = get2D(k, gridWidth)
      local x,y = {},{}
      x[1],y[1] = nodes[k].x, nodes[k].y
      x[2],y[2] = nodes[get1D(tx+1,ty, gridWidth)].x,    nodes[get1D(tx+1,ty, gridWidth)].y
      x[3],y[3] = nodes[get1D(tx+1,ty+1, gridWidth)].x,  nodes[get1D(tx+1,ty+1, gridWidth)].y
      x[4],y[4] = nodes[get1D(tx,ty+1, gridWidth)].x,    nodes[get1D(tx,ty+1, gridWidth)].y
      --Get the four node coordinates for the iso tile

      for i=1,mesh:getVertexCount() do
         mesh:setVertexAttribute(i, 1, x[i],y[i])
         --Update iso tile coordinates

         local mx,my = love.mouse.getPosition()
         local distance = dist(x[i],y[i],mx,my)
         mesh:setVertexAttribute(i, 3, spectra:gradient(range,distance, {'white','blue','royalblue','midnight'}))
         --Update colorAttributes for mesh
      end
   end
end

function love.draw()
   for _,mesh in ipairs(tiles) do
      lg.draw(mesh)
   end
end
Can anyone help? :cry: