I'm recently learning entity component systems, and started implementing it into my test engine, however I'm having an issue integrating this with scene-graph, which I'm trying for the first time.
All my entities have a TransformComponent
which transforms the entity in the scene, with it's position, rotation and scale. This is how I'm doing this:
transform.reset();
Entity entity = this.entity;
while (entity != null)
{
if (!lockScale) transform.scale(entity.scale);
if (!lockRotation) transform.rotate(Vector3.AXIS_Z, entity.rotation);
if (!lockPosition) transform.translate(entity.position);
entity = entity.parent;
}
The TransformComponent
is present for all the entities, and hence it cannot be removed from the entity, I restrict it.
Now I have another component, called as CollisionComponent
which contains the polygon, and a callback when a collision occurs, along with a collision tag. It updates the polygon position upon update:
polygon.setPosition(entity.position);
polygon.setRotation(entity.rotation);
polygon.setScale(entity.scale);
This is working for all the entities that are in the scene root, they are displayed at their correct positions, and also the collisions are working. The issue starts with the entity's children if they are present.
I have attached a child entity with position set to (0, 50) whose coordinate system is local to the parent entity system. But here, the graphics work correctly, on rotating the parent, the children rotate too due to the transform matrix, but the polygon stays at the local position.
The solution that I'm thinking now is to make the entity positions in global coordinate system, even if they are in the children of an entity, and have the transform change correctly even when the parent moves.
How can I do that?