Sunday, November 1, 2015

xna - How to properly handle collision in a component-based game?


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.



Answer



Honestly, from a component design side of things, my components do not know about each other unless they have to (And that is very rare). Even then I usually prefer the components talk to some managing system of said components instead of the components directly. (The scripting interface looks like its object to object, but its not in the actual engine, hehe).


To that end I would side with what you first said and go with physics components that exist wherever objects need to be tested for their collision. Now clearly these objects may have to inform other components of themselves upon collision resolution but, as mentioned, this is where I prefer the event itself just to go out to the objects through another interface (either to a manager or through an event messaging system if you have one of those for instance).


I think you are on the right track and just need more of a 'Yes, that sounds right' So.. Yes, that sounds right.


Hope this helps!



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...