Skip to main content
2 of 3
edited title
user avatar
user avatar

How can I handle position in an entity system where position can be 2D or 3D?

I'm working on a game engine to learn how it's done (so I might better take advantage of the techniques involved and other, similar engines in the future), and I've run into a problem when handling position when it should be able to be either 2D or 3D.

Since the physics updating the position must also affect the rendering of the graphical components' positions, I need to hold the position of the entity itself separately. Since it is used by most systems anyway, it is fine to break the purity of the design for this bit.

However, I can't decide a default position component type as the engine is meant to have both 2D and 3D capabilities. I know Unity has both 2D and 3D systems and uses a similar Entity-Component system, and there may be other engines that have a similar setup. How is it determined what type of position component to use in those systems? Or rather, what would be the best way to handle this sort of conflict (in terms of simplicity and memory)?

I'm using C++ with a little bit of inheritance with the components, where every component has an update method that is defined per component that does any sort of batching or default updating behavior (e.g. the SpriteComponent class's update will send the necessary data to the SpriteBatcher). However, the Entity class simply stores them all in a std::unordered_map<std::string, Component*> and iterates through them, calling their update methods.