Why you should obsess about your game engine architecture?

Many many many times it seems as if I'm not progressing in the game engine. I tend to obsess about the architecture of the engine. I move things around, put them back, completely modify the engine until I have something that I think is optimal.

I tend to look for things that allows the engine to be flexible and modular. I also strive my best to avoid any code duplication. I love minimalism and I tend to code the same way.

However, in the last couple stages of development, I forgot to keep the above rules in mind.

Let me explain. Currently I'm working on implementing collision detection in the engine. I'm working with two algorithms. One is called GJK algorithm and its purpose is to detect a collision between two objects. This algorithm simply returns a True or False.

The second algorithm is called EPA algorithm and it generates the contact manifold, i.e. Penetration depth and contact normal vector.

What I did was the following: I created a class called GJKAlgorithm and in this class I implemented two methods:

  • A method which determines if a collision occurred
  • A second method that determines the contact manifold, i.e. EPA algorithm

You may say, so what is the problem?

The problem is that by making one class responsible of a) detecting if collision occurred and b) getting the contact manifold, I'm making the engine static.

Imagine if a year from now, I read about a nice algorithm which is twice as fast as EPA and provides better collision information? How would I implement this new algorithm? I will have to modify/remove code that I currently have in order to give room to the new algorithm.

See, this is bad design. The engine needs to be modular. It needs to be flexible. I always try to follow these rules of Object-Oriented Programming

  1. Encapsulate what varies
  2. Code to the interface, not to the implementation.
  3. Favor composition over inheritance.
  4. Strive for loosely coupled designs between objects that interact.
  5. Classes should be open for extension but closed for modifications.
  6. Depend on abstractions. Do not depend on concrete classes.
  7. A class should have only one reason to change

In this instance I was breaking rule 4,5 and 7.

I was not making my engine loosely coupled between objects. I was not making them closed for modifications and I was making a class responsible for two operations.

So, I decided to make each algorithm a class and make them both sub-classes of a super-class. If in the future, I find better algorithms, I will be able to simply plug them into the engine without modifying my current algorithms. This keeps the engine modular, flexible and clean.

PS. Sign up to my newsletter and get OpenGL development tips.

Harold Serrano

Computer Graphics Enthusiast. Currently developing a 3D Game Engine.