I'm working on adding physics to an Entity System and am confused about the systems part of ECS.
For example, in a non-ECS project I might have something like this:
function updatePhysics()
foreach(thisRobot in robots)
thisRobot.move()
foreach(otherRobot in robots)
if(response = thisRobot.isColliding(otherRobot))
thisRobot.resolveCollision(response)
However in the ECS project, I would have a MovementSystem that operates on a PositionComponent and a VelocityComponent as well as a CollisionSystem that operates on a PositionComponent and a ShapeComponent. The result being something like:
MovementSystem
function update()
foreach(entity in entities)
this.move(entity)
CollisionSystem
function update()
foreach(thisEntity in entities)
foreach(otherEntity in entities)
if(response = this.isColliding(thisEntity, otherEntity)
this.resolve(thisEntity, response)
The difference being that in the non-ECS version the movement and collision are interleaved where as in the ECS version they are seperated. Is there a normal pattern to model this behavior in entity systems? I know the whole point of ECS is to get away from inheritance, but perhaps having both MovementSystem and CollisionSystem be part of a more general PhysicsSystem which calls the other systems update functions on a single entitiy rather than each system maintaining their own loops?
A straightforward approach could be to add to the movement component a desired destination along with the actual one. Then update only the former within the movement system.
On the other side, the collision system will try to apply the new position by switching it with the desired one and check for collisions one entity at a time.
Another approach could be to update only velocities within the movement system. Then add a more general physics system that updates position for an entity at a time and check for collisions, adjusting velocity and position if needed.
You can even define a single system that does exactly what you did previously: iterate over all the entities, update position for an entity at a time and check for collisions, then proceed with the next entity.
You don't have necessarily to separate movement system and collision system, do what sounds good for you and your game.