love.scene (yet another scene graph library)

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

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

Post by yetneverdone »

Oh another thing, can we have type id for the nodes? so we can have `view:type() == "View"` assertions and whatnot.

Also, what about the fill mode when using this?

Code: Select all

local rect = { {0,0}, {100,0}, {100,100}, {0,100} }
local mesh = love.graphics.newMesh(rect, "fan", "static")
local view = love.scene.newView()
local sprite = view:newSprite(0, 0)
sprite:setGraphic(mesh)
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post by ivan »

yetneverdone wrote: Thu Nov 17, 2022 1:13 am Awesome. Now im convinced to go with it. Just got to think how i will incorporate this with ecs 🤔
Ultimately, love.scene is just one way of organizing your drawing code.
You could easily write your own custom scene graph once you understand the underlying principles.
yetneverdone wrote: Thu Nov 17, 2022 1:13 am Oh another thing, can we have type id for the nodes? so we can have `view:type() == "View"` assertions and whatnot
Ok, great. I have added your suggestion on GitHub and in the documentation.
https://2dengine.com/?p=scene#node:type
https://github.com/2dengine/love.scene
yetneverdone wrote: Thu Nov 17, 2022 1:13 am Also, what about the fill mode when using this?
You could use sprite:setColor(1, 0, 0) to change the color of the sprite.
Alternatively, you can attach a texture or color information to the mesh.
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

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

Post by yetneverdone »

Thanks! After more usage, i still have some questions.

Can the x and y be optional when creating the sprite? Perhaps default it to 0 if nil is passed

What's the difference between transforming the nodes vs using the rest of the parameters in :setGraphic (rotation, scale, offset, shear)? Whats the recommend way of lets say drawing a scaled and rotated sprite to the center of the screen/view?
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post by ivan »

yetneverdone wrote: Thu Nov 17, 2022 2:46 pm Can the x and y be optional when creating the sprite? Perhaps default it to 0 if nil is passed
Yes, but it leads to bugs in my experience.
If you want to make the position optional you can modify the "node.lua" file:

Code: Select all

function node.new(x, y, mt)
  x, y = x or 0, y or 0
  local t = { x = x, y = y, r = 0, sx = 1, sy = 1 }
yetneverdone wrote: Thu Nov 17, 2022 2:46 pm What's the difference between transforming the nodes vs using the rest of the parameters in :setGraphic (rotation, scale, offset, shear)?
setGraphic offsets the graphic relative to the position of the sprite.
This allows you to rotate your image around its center, rather than the top-left corner.
yetneverdone wrote: Thu Nov 17, 2022 2:46 pm Whats the recommend way of lets say drawing a scaled and rotated sprite to the center of the screen/view?
Sprites located at 0, 0 will be in the center of the screen/view.

Here is how to offset your image to the center of the sprite:

Code: Select all

local iw, ih = img:getDimensions()
local sprite = view:newSprite(0, 0)
sprite:setGraphic(img, -iw/2, -ih/2)
sprite:setRotation(math.pi/2)
I admit, this is not very elegant and similar scene graph libraries have better ways to center your graphic.
I considered adding something like sprite:setGraphic(drawable, "center") but some drawable objects like meshes do not have predefined dimensions.

Thank you for the questions!
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

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

Post by yetneverdone »

It seems that the sample in the repo's readme is wrong:

Code: Select all

local Scene = require("love_scene.scene") 
local view = Scene.newView()
print(1, view:newSprite(e.transform.pos:unpack())) --no error
print(2, Scene.newSprite(view, e.transform.pos:unpack())) --has error

--should be
local node = view:newSprite(e.transform.pos:unpack()) --no error
node:setParent(view)
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post by ivan »

You are right, this bug was introduced a while ago. I rarely use the scene.newSprite syntax so I missed it.
Here is the fixed version:
https://github.com/2dengine/love.scene/ ... b0338f3a9e
https://2dengine.com/?p=scene#node

Also, you do not need to call setParent explicitly in this case:

Code: Select all

local node = view:newSprite(0, 0)
-- node:setParent(view) <-- unnecessary
I do not know which version is more useful for you:

Code: Select all

love.scene(parent, x, y)
or

Code: Select all

love.scene(x, y)
I am leaning more towards the first version since it avoids one call to the "setParent" function.
Maybe we can support both by making the "parent" argument optional.
Last edited by ivan on Fri Nov 18, 2022 10:21 am, edited 2 times in total.
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

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

Post by yetneverdone »

ivan wrote: Fri Nov 18, 2022 10:11 am You are right, this bug was introduced a while ago. I rarely use the scene.newSprite syntax so I missed it.
Here is the fixed version:
https://github.com/2dengine/love.scene/ ... b0338f3a9e
https://2dengine.com/?p=scene#node

Also, you do not need to call setParent explicitly in this case:

Code: Select all

local node = view:newSprite(0, 0)
-- node:setParent(view) <-- unnecessary
Oh my bad. I meant to use the setParent with the other method Scene.newSprite.

I prefer the passing of parent as parameter.

Im trying drawing a sprite in my project but so far i see nothing. I'll test around more
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

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

Post by yetneverdone »

This fixed the issue i was having `self.view:setScene(w/2, -h/2)` (the negative height/2). Why though?
User avatar
ivan
Party member
Posts: 1911
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 10:21 am This fixed the issue i was having `self.view:setScene(w/2, -h/2)` (the negative height/2). Why though?
Difficult to say but setScene(w/2, -h/2) is a perfectly valid line.
Please post the rest of your code if you are having further trouble.
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

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

Post by yetneverdone »

ivan wrote: Mon Nov 21, 2022 12:53 pm
yetneverdone wrote: Mon Nov 21, 2022 10:21 am This fixed the issue i was having `self.view:setScene(w/2, -h/2)` (the negative height/2). Why though?
Difficult to say but setScene(w/2, -h/2) is a perfectly valid line.
Please post the rest of your code if you are having further trouble.
What i meant was, i was doing `h/2` first, then making it negative made it works. I guess i cant still visualize mentally that working.

Also, any tips for when to set node's positions/scale vs graphic's position/scale?
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 19 guests