love.scene (yet another scene graph library)

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post by ivan »

The origin of the scene is drawn in the center, so sprites at [0,0] are in the center of the view/window. The Y-axis decreases as you move nodes higher on the screen. The node position determines where your sprite is located inside the scene (so called "scene coordinates"). If you have a car sprite located at 100,300 you can use node:setRotation(x,y) or node:setScale(sx,sy) to turn and scale the car without affecting its position. We use car:setPosition(x, y) to move the car.
On the other hand node:setGraphic(img, ox, oy) offsets your graphic and determines the center of rotation for your car texture. We use car:setGraphic(car_img, -car_img_width/2, -car_img_height/2) to center the car texture over the position of the node.
User avatar
yetneverdone
Party member
Posts: 448
Joined: Sat Sep 24, 2016 11:20 am
Contact:

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

Post by yetneverdone »

On the other hand node:setGraphic(img, ox, oy)
Wait, is the 2nd and 3rd parameters of setGraphic the offset? I thought thats the position x and y?

Another question, can't a sprite be set as another sprite's parent? Something like this errors:

Code: Select all

local s1 = view:newSprite(0, 0)
local s2 = view:newSprite(32, 32)
s2:setParent(s1) -- errors  scene/node.lua:59: attempt to call method 'insertChild' (a nil value)
I assumed that sprites can be nested
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post by ivan »

yetneverdone wrote: Mon Nov 21, 2022 4:27 pm Wait, is the 2nd and 3rd parameters of setGraphic the offset? I thought thats the position x and y?
Yes, you are right of course. I was not 100% accurate in my definitions. The coordinates we pass to setGraphic (both x,y and ox,oy) are relative to the sprite transform. On the other hand, sprite setPosition(x, y) usually works with a position in "scene coordinates" (unless you transform its parent layer).
yetneverdone wrote: Mon Nov 21, 2022 4:27 pm Another question, can't a sprite be set as another sprite's parent?
Only layers can contain other nodes or could be nested. Sprites are the "leaves" of the scene graph and layers are the "branches".
Also, please note that you can share one drawable object (texture, text or mesh) between multiple sprites.

PS. I will try to provide an update in the next week or so. Some of the function names of the lib are not super clear.
User avatar
yetneverdone
Party member
Posts: 448
Joined: Sat Sep 24, 2016 11:20 am
Contact:

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

Post by yetneverdone »

Why can't sprite nodes be nested though? Perhaps theres a better way to do what I was trying to do which is to have another sprite node (flashlight) attached/nested (so it can easily be translated/offset by the parent node) to the player sprite node.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post by ivan »

Thank you so much for the feedback. You have pointed out several issues with the library so I released an update trying to address your criticisms. I also added a camera object and a memory pool which will make the memory usage of the scene graph more stable:
https://github.com/2dengine/love.scene
https://2dengine.com/?p=scene
Regarding your question as to why sprites cannot be nested: if we had nested sprites then it would be unclear in what order to draw the graph. Do we draw the sprite's own graphic before its child nodes, after its child nodes or should the sprite's own graphic be sorted alongside with the other nodes? I am not saying it cannot be done, but I think having the layers separate from sprite nodes makes the library a lot more clear and straightforward.
Thanks again,
Ivan
User avatar
yetneverdone
Party member
Posts: 448
Joined: Sat Sep 24, 2016 11:20 am
Contact:

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

Post by yetneverdone »

I see that `camera` node was added. What would be the example usage of that compared to just using view
As per the sprite nodes nesting, i think it's expected to draw the parent behind the children. Anyways i have implemented a way to get around getting the transformation of the parent node in my project.

Thanks a lot for the updates
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post by ivan »

yetneverdone wrote: Fri Nov 25, 2022 2:12 am I see that `camera` node was added. What would be the example usage of that compared to just using view
It works in a similar way, except that it allows the distinct separation between the graph and view:

Code: Select all

local view = love.scene.newView()
local root = love.scene.newLayer(0, 0) -- note that the root is not parented by the "view" object
local cam = root:newCamera(100, 0)
cam:setRange(800, 400)
view:draw(cam)
One of the benefits of cameras is that you can render the same camera across multiple views.
yetneverdone wrote: Fri Nov 25, 2022 2:12 am As per the sprite nodes nesting, i think it's expected to draw the parent behind the children. Anyways i have implemented a way to get around getting the transformation of the parent node in my project.
What you are saying is that in the case of nested sprites, the container sprite's graphic should be drawn BEFORE any of the child nodes. This is fine in specific cases, but for a general purpose library you need a mechanism that controls the order in which the nodes are drawn.

We don't want to hard-code the order in which the graphics are drawn and that is why we have the layer object. The layer object allows you to control the depth of the child nodes. You use sprite:setDepth(z) to control the depth.
User avatar
yetneverdone
Party member
Posts: 448
Joined: Sat Sep 24, 2016 11:20 am
Contact:

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

Post by yetneverdone »

Question, if i have the main game size (1024, 640), and i want to draw that in the center of the love window (lets say 1408, 800), how would i do that? Should i adjust the position and size of the view or the layer or the camera?
Milton_5s10uhEPgU.png
Milton_5s10uhEPgU.png (30 KiB) Viewed 5039 times
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post by ivan »

If you want to draw the scene with a black border around it you can adjust the size and position of the view (view:setPosition or setDimensions). If you want to draw the black border yourself (or maybe draw a HUD over the border) set the view as large as the window and use camera:setRange or setScale to stretch the scene. Either way is fine especially if you use a resizable window.
Changing the scale of the root layer is possible too if your scene does not use cameras.
Hope this helps:

Code: Select all

function love.resize(w, h)
  view:setDimensions(w, h) -- make sure the view covers the entire window
  -- option 1: stretch the camera along with the window
  camera:setRange(800, 600)
  -- option 2: zoom the camera preserving the aspect ratio
  -- camera:setScale(h/600, h/600)
end
User avatar
yetneverdone
Party member
Posts: 448
Joined: Sat Sep 24, 2016 11:20 am
Contact:

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

Post by yetneverdone »

Hi, I am currently doing this

Code: Select all

function SceneNodes:create_camera()
    self.view = Scene.newView()

    if DEV then
        self.view:setDimensions(WINDOW_WIDTH, WINDOW_HEIGHT)
        self.view:setPosition(love.graphics.getWidth()/2 - WINDOW_WIDTH/2, 64)
    end

    local w, h = self.view:getDimensions()
    self.root = self.view:newLayer(0, 0)
    self.camera = self.root:newCamera(w/2, h/2)
end
This draws it like this:
lovec_CKWkcV4xFj.png
lovec_CKWkcV4xFj.png (123.58 KiB) Viewed 4997 times
which is what I want (main game screen at center (border is the red box), other dev stuff outside)

Now, how would use `love.mouse.getPosition()` to make the nodes/sprites follow the mouse coordinate?

Before any of the separate game screen and dev stuff, it works perfectly (sprite's center follows the mouse). But now theres some kind of offset. I have tried localToRoot, sceneToLocal, sceneToWindow, windowToRoot functions to convert windows-space mouse coordinate to view coordinates but none worked as before the change
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests