Scene graph
A scene graph is often used in game development to link objects together. This link can be logical, for instance a sword can be seen as a part of the hero; but it can also be spatial: the sword moves along with the hero (assuming he doesn't drop it of course). A scene graph can be visualized as a system of nodes; each node can have a parent, siblings, and children. The position, rotation, and scale of a child will be relative to its parent. For instance, if our hero moves, the sword would also move, even though you don't change the position of the sword itself.
Implementation
We will update GameObject2D
and GameObject3D
so they implement this concept. Each object will hold a list of children and a reference to its parent (if a parent exists). Note that the implementation is very similar (if not almost identical) for GameObject2D
and GameObject3D
; therefore the code in this section will be the implementation of GameObject2D
. We can find the implementation of GameObject3D
in the...