Page 5 of 5

Re: love.scene (yet another scene graph library)

Posted: Tue Nov 29, 2022 9:58 pm
by ivan
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.

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
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.

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
I will try to do additional testing and promise to provide more updates if I found any other issues.

Re: love.scene (yet another scene graph library)

Posted: Wed Nov 30, 2022 3:54 am
by yetneverdone
Hi, thanks for the explanation! I used the camera example, but it seems that it's inverted. The sprite follows the mouse in an inverted way.
lovec_UwZdCP9Xya.gif
lovec_UwZdCP9Xya.gif (1.56 MiB) Viewed 7850 times

Re: love.scene (yet another scene graph library)

Posted: Wed Nov 30, 2022 1:00 pm
by ivan
With the introduction of the camera objects you have the option to render the same camera across multiple views. Therefore, when using cameras the functions sprite:localToWindow(x,y) and sprite:windowToLocal(x, y) will return nil. The root object must be a "View" if you want to use sprite:localToWindow or sprite:windowToLocal.

I made some adjustments to the library which should make it somewhat easier to use cameras. The basic idea is to use view:setCamera(cam) and then view:windowToRoot(mx, my) to convert your mouse coords to scene coords.
Here is the gist of it:

Code: Select all

function love.load()
  -- setup the view
  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)
  -- scene and camera
  local w, h = view:getDimensions()
  root = Scene.newLayer(0, 0) -- no parent
  camera = root:newCamera(0, 0)
  view:setCamera(camera)
  camera:setRotation(math.pi/8)
  -- sprite and graphic
  sprite = root:newSprite(0, 0)
  local img = love.graphics.newImage('icon.png')
  local iw, ih = img:getDimensions()
  sprite:setGraphic(img, -iw/2, -ih/2)
end

function love.update(dt)
  -- convert the mouse to scene and move the sprite under it
  local mx, my = love.mouse.getPosition()
  local rx, ry = view:windowToRoot(mx, my)
  sprite:setPosition(rx, ry)
end

function love.draw()
  view:draw()
end
Thank you again for the feedback. I think the library has become much better thanks to your suggestions.

Re: love.scene (yet another scene graph library)

Posted: Wed Nov 30, 2022 1:37 pm
by yetneverdone
Awesome! It works correctly now! Thanks a lot. I'll post here again if I have a question or suggestion :)

Re: love.scene (yet another scene graph library)

Posted: Mon Dec 26, 2022 9:55 am
by ivan
4-7% increase in speed when drawing and transforming sprites through the use of local variables in conjunction with the debug registry
https://github.com/2dengine/love.scene/ ... r=2dengine

Re: love.scene (yet another scene graph library)

Posted: Mon Jan 23, 2023 1:00 pm
by yetneverdone
Question, given this node structure/hierarchy.

Code: Select all

- root layer
- layer 1
  - sprite1 node
  - sprite2 node
  
shouldn't the sprite nodes follow the layer 1 position when the layer 1 moves?

Also, shouldn't the `sprite1:getRoot()` return the `layer 1` instead of the `root layer`? Right now it returns the `root layer`. I think getting the nearest parent is useful

Re: love.scene (yet another scene graph library)

Posted: Mon Jan 23, 2023 2:36 pm
by ivan
yetneverdone wrote: Mon Jan 23, 2023 1:00 pm shouldn't the sprite nodes follow the layer 1 position when the layer 1 moves?
Yes they should! Please provide code examples if you have come across a bug.
yetneverdone wrote: Mon Jan 23, 2023 1:00 pm Also, shouldn't the `sprite1:getRoot()` return the `layer 1` instead of the `root layer`? Right now it returns the `root layer`. I think getting the nearest parent is useful
Just a small clarification: is "layer 1" a child of "root layer" or its sibling?
I have just added "node:getParent()" which was missing from the library.

Re: love.scene (yet another scene graph library)

Posted: Mon Jan 23, 2023 2:54 pm
by yetneverdone
Just a small clarification: is "layer 1" a child of "root layer" or its sibling?
oh I missed that, layer 1 is child of root layer.
I have just added "node:getParent()" which was missing from the library.
thank you! will check again and see if i just missed something on my end

Re: love.scene (yet another scene graph library)

Posted: Mon Feb 06, 2023 9:25 am
by ivan
By avoiding metatables we achieved 5-10% increase in performance for most games depending on the total number of sprites. Please use scene.cache to adjust how this works.
https://github.com/2dengine/love.scene/ ... ce46297ebb