Re: love.scene (yet another scene graph library)
Posted: Tue Nov 29, 2022 9:58 pm
Hello and thank you for the feedback. Upon more testing I found a few minor issues with the transformation functions so please update your code with the latest version from GitHub. Thanks for your patience.
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.
2. If you do use cameras, then make sure that your root layer does NOT have a parent. This will save you a lot of confusion later.
I will try to do additional testing and promise to provide more updates if I found any other issues.
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