The way to transform coordinates is different based on your scene setup. You have two ways of drawing the scene: with cameras or without cameras.
1. If you do NOT use cameras the transformations are much simpler. In this case the root layer is parented by the view.
Code: Select all
function love.load()
Scene = require('scene')
view = Scene.newView()
local gw, gh = love.graphics.getDimensions()
view:setDimensions(300, 300)
view:setPosition(gw/2 - 300/2, gh/2 - 300/2)
view:setBackground(0, 1, 1)
root = view:newLayer(0, 0)
sprite = root:newSprite(0, 0)
sprite:setGraphic(love.graphics.newImage('icon.png'))
end
function love.update(dt)
local mx, my = love.mouse.getPosition()
local lx, ly = sprite:windowToLocal(mx, my)
local px, py = sprite:localToParent(lx, ly)
sprite:setPosition(px, py)
end
function love.draw()
view:draw()
end
Code: Select all
function love.load()
Scene = require('scene')
view = Scene.newView()
local gw, gh = love.graphics.getDimensions()
view:setDimensions(300, 300)
view:setPosition(gw/2 - 300/2, gh/2 - 300/2)
view:setBackground(0, 1, 1)
local w, h = view:getDimensions()
root = Scene.newLayer(0, 0) -- no parent
camera = root:newCamera(w/2, h/2)
camera:setRotation(math.pi/8)
sprite = root:newSprite(0, 0)
sprite:setGraphic(love.graphics.newImage('icon.png'))
end
function love.update(dt)
local mx, my = love.mouse.getPosition()
-- convert the mouse to local view coordinates
-- the camera coords are the same as the local view coords!
local x, y = view:windowToLocal(mx, my)
local px, py = camera:localToRoot(x, y)
sprite:setPosition(px, py)
end
function love.draw()
view:draw(camera)
end