I'm trying to figure out how to integrate entity component systems with scene graphs in a web game / application. The problem is of course that ECS and scene graphs are very different in nature. They are designed to solve different problems and in many ways contradict each other.
Actually, to be more precise, I’m looking for a way to introduce hierarchical relationships in an ECS architecture. If that involves a scene graph remains to be seen.
What would be a good, conventional way to handle hierarchical relationships in an ECS?
In my application entities on their own are nothing more than IDs. Components are containers of data that stores a reference to the corresponding entity ID. Components are stored outside of the entities. Systems loop through entities and check if they have the relevant components. At least this is how it stands right now.
So far I have come up with the following alternatives:
- Hierarchical entities, aka "scene graph with components": Store hierarchical relationships in entities. Turn entities into node objects that hold hierarchical data and possibly other types of data that is typical for nodes. Traverse the scene graph as usual and look up the relevant components for each node.
- Hierarchical components: Store hierarchical relationships in components, e.g. childComponent, parentComponent. One or more systems will use these components to traverse the hierarchy and operate on entities (or rather components belonging to entities) in the correct order.
- Clear separation: Keep the ECS and the scene graph entirely separate and focused on different tasks. The ECS is focused on gameplay logic while the scene graph handles transformations and rendering. I don’t know if this level of separation is possible.
- Separate but synchronized: Keep the ECS and the scene graph separate but synchronized. Have each entity with a certain set of components (position, transformations, etc) correspond to a node in the scene graph. Since both systems work on the same data they will need to be synchronized in some way.