Friday, September 28, 2018

game loop - Updating entities in response to collisions - should this be in the collision-detection class or in the entity-updater class?


In a game I'm working on, there's a class responsible for collision detection. It's method detectCollisions(List entities) is called from the main gameloop.


The code to update the entities (i.e. where the entities 'act': update their positions, invoke AI, etc) is in a different class, in the method updateEntities(List entities). Also called from the gameloop, after the collision detection.


When there's a collision between two entities, usually something needs to be done. For example, zero the velocity of both entities in the collision, or kill one of the entities.


It would be easy to have this code in the CollisionDetector class. E.g. in psuedocode:



for(Entity entityA in entities){
for(Entity entityB in entities){
if(collision(entityA, entityB)){
if(entityA instanceof Robot && entityB instanceof Robot){
entityA.setVelocity(0,0);
entityB.setVelocity(0,0);
}
if(entityA instanceof Missile || entityB instanceof Missile){
entityA.die();
entityB.die();

}
}
}
}

However, I'm not sure if updating the state of entities in response to collision should be the job of CollisionDetector. Maybe it should be the job of EntityUpdater, which runs after the collision detection in the gameloop.


Is it okay to have the code responding to collisions in the collision detection system? Or should the collision detection class only detect collisions, report them to some other class and have that class affect the state of the entities?




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...