2D scene graph

Hello forum,
how could a scene graph for a 2D game be implemented? I would choose an array list and store some sprites in there, but maybe it’s not that simple.

Hi

Do you really want to write your own scenegraph? I would use a tree as the main abstract data type.

Yes, if it’s not too complicated. In 3D a scene graph is used to implement an object hierarchy, but I’m not sure if it’s useful in a simple Tetris or Super Mario style 2D game.

Tetris is a very good example.

Imagine a scene graph tree:
You have your tetris objects in your tree. They are composed of (4) Blocks. If you move the top node, your blocks move.

Here is an example of such a tree:
root / \ / \ / \ / \ / \ / \ Tetris "L" Tetris Pipe / | | \ / | | \ offsets: (0,0)(0,1)(0,2)(1,2) (0,0)(0,1)(0,2)(0,3)

So if you rotate or move a tetris object, it’s children rotate, if you move the tetris objects, the children move.

Thanks. You are right, Tetris blocks are composed of multiple graphical objects, but would a scene graph still make sense if you just had to catch some graphical objects that fall from above? In this case, I don’t see any hierarchy.

Yeah. Of course there are examples, where there isn’t any hierarchy… this is one of those examples.

But generally I think they are cool :slight_smile:

If there’s not hierarchy, what sense does a scene graph make in this case? I haven’t understood this yet.

The overview for scene2d, libgdx’s 2D scene graph, lists what it provides:
https://code.google.com/p/libgdx/wiki/scene2d
If you have no hierarchy at all, it still helps with drawing and hit detection for rotated/scaled actors. The action system might also be useful. Also note the second to last paragraph that talks about model-view coupling.

In that sense there is a hierarchy.

Yes, a hierarchy consisting of one level (the root group with actor (non-group) children). This is also known as a list. :stuck_out_tongue:

It is convenient for actors to think in their own unrotated and unscaled coordinate system, where 0,0 is the bottom left corner and width,height is the upper right corner (you can use y-down with scene2d if you want, but then some scene2d.ui widgets won’t work (scene2d.ui is a UI library built on top of scene2d)). It makes drawing and hit detection easier.