ECS & DOD

There is some debate about ECS among programmers - for b3D, the architecture is Entities, Components, and Systems (rather than an Entity-Component System):

  • Entities represent things that exist.

  • Components are stores of data relating to a particular behaviour.

  • Systems operate on that data, producing the particular behaviour for each entity with that particular component.

This follows the principle of Data Oriented Design (DOD).

In b3D, entities are literal integers:

int camera
int spotlight
int player

To keep everything organised, the ECS manager takes care of keeping track of what numbers have been given out, and gives you the next one when you create an entity:

int camera = ecs.CreateEntity();
struct TransformComponent {
	glm::vec3 localPosition = glm::vec3(0.0f);
	glm::quat localRotation = glm::quat(glm::vec3(0.0f));
	glm::vec3 localScale = glm::vec3(1.0f);

	glm::vec3 worldPosition = glm::vec3(0.0f);
	glm::quat worldRotation = glm::quat(glm::vec3(0.0f));
	glm::vec3 worldScale = glm::vec3(1.0f);
};

Components are just structs:

And like before, the ECS manager keeps things organised by storing the components in a dense array:

int camera = ecs.CreateEntity();
auto& cameraTransform = ecs.AddComponent<Transform::TransformComponent>(camera);
cameraTransform.localPosition = glm::vec3(12, 4, -2);

Then each frame, systems can get one or more dense array of components and quickly operate over the data:

...
Debug::Update();
Time::Update();
Input::Update();
Velocity::Update();
Transform::Update();
Sequence::Update();
Audio::Update();
UI::Update();
Animation::Update();
Renderer::Render(window);
...