🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Entity-component-system and physics engine integration

Started by
1 comment, last by hyyou 4 years, 11 months ago

Hi. I'm trying to integrate physics engine (Box2D) into my custom game engine based on entity-component-system. When I add physics component to entity, I also need to create appropriate objects in Box2D world. Because Box2D has it's own internal world data structure. But components in ECS are just only plain data, they don't have any behavior like OnAttached(), OnDetached(). Instead all the behaviors are implemented in systems. So I need to somehow know if component is newly added to entity or not in physics system. If it is newly added then create object in Box2D world. For that reason I created XXXCreateInfo components. (For example RigidbodyCreateInfo) And if I need to add rigidbody to entity, I just add RigidbodyCreateInfo with it's properties (mass etc..) to entity. And physics system create rigidbody in box2d world and attach it to entity with Rigidbody component and remove old RigidbodyCreateInfo.

This method works good for adding physics component. But how can I remove them if entity is destroyed? Should ECS has OnAttach, OnDetach events for components? Or can we workaround this problem with only systems?

Advertisement

Initially, I used callback (cooler) OR check at the end of every time-step (safer) - both waste some CPU cycles.

Later, I found out a more controllable way is to create a new function e.g. :-
 


class Component_MyRigidbody :  public MyComponent{
    //some Box2D ptr;
    public: void destroy();
};

void Component_MyRigidbody::destroy(){
     //delete internal Box2D structure here e.g. unregister/delete ptr;
}

I generally forbid users to delete Entity/Component directly.   It works for my ECS.

This topic is closed to new replies.

Advertisement