Harold Serrano

View Original

The purpose of a scenegraph in a game engine

Scenegraphs are generic trees which provide an efficient way to traverse game entities for rendering.

As the image below shows, a scenegraph node can have as many children as it desires. This property is in complete contrast with a binary tree; which allows at most two children per node.

On each game tick, the scenegraph traverses its lists and sends each game object to an Engine Loop. Where it gets rendered, and its coordinate space gets updated.

Aside from its fast traversal property, a scenegraph provides a clean method to transform a child node space with respect to its parent node space.

For example, in the image below, both the soccer player and the soccer ball are children of the World Entity.

Thus, if the world entity (grid) rotates, both children nodes will be affected by their parent space-transformation, as shown below.

However, a child node does not affect the space of a sibling node, nor its parent. Thus, rotating the player or the ball about their y-axis does not affect either or, nor the World entity, as shown below:

Now, if the ball was a child of the soccer player, as shown below:

then rotating the player causes the ball to rotate as well.

A scenegraph provides a clean method to establish a parent-child relationship and fast traversal of its entities. Such feature is useful when the number of game objects grows, and efficiency is of utmost importance.

Hope this helps.