1

I'm implementing an entity system for educational purposes. I wanted to focus on the system itself and don't know much about rendering and physics, so I'm using external libraries for those tasks.

These libraries come with pre-defined classes for their objects (sprites, bodies, etc.) These do not fit into my entity system easily.

How can I utilize existing libraries and classes within an entity system?

K..
  • 205
  • 2
  • 10

3 Answers3

3

This is one of my issues with entity systems as opposed to other component architectures. Most games do not use the components themselves as data for drawing or physics. Not every component needs all the complexity and problems of strict data-oriented design.

Sticking to the entity system approach, though, you can have these external libraries attached as data like anything else or in a separate array.

Example when treating pointers as any other data for a component:

class PhysicsComponent {
  LibraryBody* _body; // points into the physics library you're using
  float3 _centerOfMass;
  float _inverseMass;
};

class PhysicsSystem {
  vector<PhysicsComponent> _components;
};

And as separate arrays:

class GraphicsConfigurationComponent {
  string _nameOfSprite;
  string _nameOfShader;
};

class GraphicsSystem {
  vector<GraphicsConfigurationComponent> _configs;
  vector<LibrarySprite*> _sprites; // points into the graphics library you're using

  void RegisterConfiguration(GraphicsConfigurationComponent* config) {
    _configs.push_back(*config);
    _sprites.push_back(MakeSpriteFromConfig(config));
  }
};

There's not a huge advantage to either approach on their own.

Sean Middleditch
  • 41,807
  • 4
  • 89
  • 132
3

I wanted to focus on the system itself and don't know much about rendering and physics, so I'm using external libraries for this.

An "entity system" should not directly handle rendering and physics anyway; at best it should just have references to objects that are part of the rendering or physics systems, whether or not you wrote those systems yourself.

These libraries come with pre-defined classes for their objects. (Sprites, Bodies etc.) These do not fit into my entity system easily.

How can I utilize existing libraries and classes within an entity system?

Wrap them in structures that your system can consume.

An entity system really should not be a low-level API, so systems like physics and rendering should not depend or even know about it. Entity systems are about representing game objects and very high level behavior.

If you're building something akin to the current fad of "entity / component" systems, you'd create a visual component and a physics component that are at the abstraction level of your entity system and which respectively wrap the actual rendering or physics objects necessary. Something like:

struct RenderComponent {
  Render::RenderableObject m_renderable;
  Matrix m_transform;
}

struct PhysicsComponent {
  Physics::RigidBody m_body;
}

And so on. These act as adapters to the rendering and physics systems, and can be written to expose via their public interface the various details of the underlying external library objects that might be relevant to other things within the entity system.

Even if you aren't taking the "component-based" approach, you can still build adapters as above (although exactly what you call them should be informed by the structure of the rest of your API, which you have not specified).

  • the problem I see is, I will end up with, at least, one indirection. Like a component which hast the size and position of a entity and a renderobjet, which also has a size and position, so the system would have to copy those values on every iteration :\ – K.. Feb 25 '14 at 15:46
  • 1
    This is true regardless, and isn't always bad. Position data is trivial in size, plus it can be useful for interesting effects to separate render and physics position. You can also implement a data-sharing system in your entity system if you really want, et cetera. There are a lot of ways to handle it. –  Feb 25 '14 at 15:58
0

OP's options:

Using these libs directly and using (for example) objects of the engine-specific Sprite class as an component and rendering it in the system which wraps the engine.

Or

Using a generic Sprite component, which defines dimensions/positions and map those, when changed, inside of the system, which wraps the engine, to the engine-specific Sprite-class.

Seth Battin
  • 5,509
  • 3
  • 27
  • 43