Trying to wrap my head around the ways to properly handle collision in a game designed around components.
I see many examples have some sort of PhysicsComponent
that gets added to the entity's list of components but the actual implementation is confusing me.
For this to work, the PhysicsComponent
would need access to the world around it. This doesn't make intuitive sense to me. Shouldn't a component be unaware of not only its container (the entity), but its container's container (the world)?
To me, it sounds like the level or scene should maintain a list of these entities and every game update, loop through the entities to determine which collide.
My question is firstly, whether or not this is good design, and secondly, how to determine which entities can collide. I suppose solid entities could implement an empty IRigidBody interface so the level could determine which entities in the list support collision. But is this breaking the component design?
Instead, should they contain an empty RigidBody component? This may actually be better because it may not always be empty and this approach is more future-proof. The only problem with this is the complexity. The scene would have to loop through not only every entity, but also every entity's components to determine if it had this RigidBody component.
Thirdly, when they do collide both entities should be informed somehow and I am unsure on how to accomplish this either.
Let's say that both entities contained a HealthComponent and when they collided both their healths would be decreased by some arbitrary value, 5. I suppose it would be the scene's responsibility to handle this when it detects a collision between two entities?
But then is the scene responsible for too much? I could see this possibly getting out of hand and becoming unwieldy when the scene is responsible for many things entities should not (?) have access to.
Edit: Question updated with more details.