-
Notifications
You must be signed in to change notification settings - Fork 0
Entity Component Model
Here is a description of the Entity-Component model used by the Shoggoth Engine.
There are countless ways to do an Entity-Component model and unfortunately there is not a standard way to do it yet. Implementations vary greatly and the one done here is thought for this specific project.
Typically an entity is only a container with no information, and it only stores a list of components. A Component
would store information and properties of how it should behave. So you could have an Entity
with a RenderableMesh
component and a RigidBody
component. However, there are managers which are responsible of processing those components.
In the Shoggoth Engine an Entity
is merely a point in 3D space. It has four basic properties: a 3D position vector, an orientation quaternion, a list of components, and a list of children. For simplification, an Entity
also is a node of the scene graph and therefore needs to store its children.
Every Entity
in the scene graph stores its position in a 3D vector of double
precision. It stores both the absolute position in the world and the relative position to their parent. Note that it gets converted to float
for processing in certain components like the RenderableMesh
component for OpenGL.
It stores the orientation in a quaternion, which is a 4D vector of double
precision. There is no easy way to work "by hand" with quaternions, but its easier and more efficient for processing. However there are functions to convert to/from Euler angles, axis-angle and Matrix3x3 for convenience. Note that it gets converted to float
for processing in certain components like the RenderableMesh
component for OpenGL.
There can only be one Component
of each type attached to a certain Entity
at a time. If multiple Component
's should be added, then multiple Entity
's should be created as children. The list of components is represented internally by a set with the key being a string with the name of the component.
The scene graph consists of one Entity
which is the root node. The root node generally will not have any components attached and it will have many children which would represent the whole scene graph. Whenever an Entity
changes its position or orientation, it recursively changes the position and/or orientation of all its children. The list of children is represented internally by a set.
The Component
's attached to an Entity
are the ones that define the way it behaves. They only store attributes and properties. Whenever a Component
is attached to an Entity
, it gets registered into a manager. The component manager is the one in charge of processing anything related to it.
There are some components with their respective managers that are already built in, but more can be added. The default managers are:
-
Device
: it creates an SDL window with an OpenGL context. -
Renderer
: it draws the scene each frame, rendering theRenderableMesh
components. -
Resources
: it stores a pool of resources, loading each resource only once. -
PhysicsWorld
: it computes a physics simulation with theRigidBody
components. -
Scene
: it stores the scene graph which consists of an n-ary tree ofEntity
s withComponent
s attached to them.
The default components are:
-
RenderableMesh
: it stores a pointer to a Model. -
RigidBody
: it gives access to its mass, linear and angular damping, friction, rolling-friction, restitution, linear and angular sleeping-thresholds, linear-factor, linear-velocity, angular-factor, angular-velocity and gravity. -
Light
: it stores the ambient, diffuse and specular colors. (Currently only point lights are supported) -
Camera
: it stores the perspective FOV, orthographic height, near distance and far distance.