Recommended architecture to allow entities to access each other's data.

Hi guys,

I’m looking for some advice or resources on game programming patterns / architectures that are most appropriate to handle interactions between entities in games. To give some background, I am currently designing a game mechanic where projectiles are launched in space and affected by the orbital gravity of planets (similar to Angry Birds in Space). In this case, the projectile entities need to be aware of the positions of the planet entities in order to calculate their velocities due to gravity. They also need to be aware of other projectile entities for collision detection.

After doing some research I have come up with the following potential solution:

  • Create an entity management class to store reference to all entities.
  • Pass entity manager to entities during construction.
  • During game loop update, the projectile objects can access the planet objects via the entity manager class to calculate velocity and collision detection.

However, it seems like the point of having an entity manager is so that the entity manager can perform the collision detection so that the entity classes do not need to know of one another. However, the entity manager would then also need to calculate the velocities for each projectile, per update, and this is where i get confused.

I get that I can simply pass a planet entity list to the projectile class update function, but what about in big games where many entities require information on each other to function correctly?

I would really appreciate if anyone can share any resources that could help me build a pretty architecture for this, or how they handle these types of interactions themselves.

Getting my head around programming architectures is the reason I enjoy game programming.

When you need to look at the whole picture or need all objects of a kind for a task, it is a good approach to create new separate classes that perform such tasks. But don’t call any class “Manager”. It the most abused and meaningless name you can think of. What you want is a physics simulation and collision detection. It might make sense to integrate both into one PhysicsSimluator or have two classes. Depends on your approach on physics simulation.

This does sound like a cleaner approach, do you happen to know of any resources or examples that I could have a look through? I’m having difficulty finding the right words to Google.

I recommend:

  • Create a Scene class that stores and updates everything related to your game
  • Create a CollisionManager class that checks for collisions. The Scene class should store the CollisionManager and update it.
  • Create a data structure that group entities by their location for faster collision detection and faster access of entities in certain areas
  • Give a every entity a reference to the scene so that they can access any entity
  • Have every relevant entity store a list of vectors for each force/accel that they are feeling
  • Have every relevant entity update its position and vel based on these vectors
  • Create a Planet class that gives and updates force vector to every relevant entity

The basic architecture is (and should be) very simple, for example:


public class EntityModel {
	List<Entity> getEntities();
	List<Entity> getPlanets();
	List<Entity> getProjectiles();
}

public class PhysicsSimulator {
	public void update(long time) {
		// place to tear your hair by simulating gravity effects...
		for (Entity entity : entities) {
		   ...
		   entity.getMass()
		   ...
		   entity.setVelocity(...)
		   ...
		}
	}
	
	private EntityModel entityModel;
}

Think of having controllers and models. Controllers do the action and know the model. Not vice versa.
This will massively help in keeping code dependencies and data flow under control.

Wow, thank you so much for the advice guys. I wouldn’t have thought to maintain my projects in this way, there is still much to learn about effective software development!