I’m making a map editor for my game but Im having a bit of a brain freeze when it comes to thinking up what ratios to apply to the touch position for translating the map and zooming in and out.
I want the centre of the map to be 0,0 and then the edge should be 750 away from the centre either way. Any help is well appreciated.
-- mapeditor
-- Use this function to perform your initial setup
function setup()
mapimg = readImage("Documents:level1")
mapmesh = mesh()
maprect = mapmesh:addRect(WIDTH/2,HEIGHT/2,1500,1500)
mapmesh.texture = mapimg
tch = {}
odist,dist = 0,0
scl = 1
end
function touched(t)
if t.state == BEGAN then
for k,v in pairs(tch) do
if v.z == t.id then
return
end
end
table.insert(tch,vec3(t.x,t.y,t.id))
end
if t.state == MOVING then
for k,v in pairs(tch) do
if v.z == t.id then
v.x,v.y = t.x,t.y
return
end
end
end
if t.state == ENDED then
for k,v in pairs(tch) do
if v.z == t.id then
table.remove(tch,k)
end
end
end
end
function zooming()
local n = 0
if tch[1] and tch[2] then
local v1,v2 = vec2(tch[1].x,tch[1].y),vec2(tch[2].x,tch[2].y)
dist = v1:dist(v2)
if dist > odist then
n = 1
elseif dist < odist then
n = -1
elseif dist == odist then
n = 0
end
odist = dist
end
return n
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
local zm = zooming()
scl = scl + zm/50
scl = math.min(math.max(scl,0.1),1)
local imgsize = 1500*scl
local tm = vec2(WIDTH-WIDTH*0.5*(1-scl)-imgsize/2,HEIGHT-HEIGHT*0.5*(1-scl)-imgsize/2)
translate(tm.x,tm.y)
scale(scl)
if tch[1] and tch[2] == nil then
local t = tch[1]
local d = (t.x/imgsize)*scl
local b = 1500/WIDTH
d = d-0.25
d = d*2
d = d*1500
print(d,scl)
end
-- Do your drawing here
mapmesh:draw()
end
Level1 image: https://www.dropbox.com/s/j1xvoteb9wgycpw/Untitled%2034.png