• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Finalspace

Entity component system and physics

4 posts in this topic

Hi there,

 

i am in the phase to adding my 2d physics engine over to my freshly created entity component system and have some design headaches.

 

It is a simple entity component system, which has "Components" which are just containers and "Systems" which may require n-components and can update or draw a list of entities. There is a component registration which register new component classes (to support unlimited components) who creates new handles and cache this by the class name. Then we have the mighty manager which handles the initialization / update / draw of the systems, keeps a entity list + a list for each system and gets notified when components are added or removed from an entity. An entity is just an component container which do have only the methods for adding/removing and retrieving components. Thats basically it - nothing too fancy and works so far.

 

But, i have a problem - my current physics engine uses a rigidbody class, which contains all the required properties (like position, velocity, mass, shape etc.) to work with and my entities on the other hand do just contains the "containers" of this properties - some cannot be used (simple types, like numbers, floats) as references which are no references/pointers at all - because its implemented in javascript (numbers are no references).

 

Is there a good way to integrate a rigidbody/physics engine in a entity component "system"?

 

 

 

My current naiive approach is:

 

Splitting the rigidbody into multiple components:

 

- PositionComponent (position vector only)

- VelocityComponent (velocity, angular velocity and both damping factors)

- TransformComponent (rotation angle and transformation matrix)

- MassComponent (mass, inertia and both invert parts of it + density factor)

- ShapeComponent (contains just a reference to actual shape class - like CircleShape which just contains the radius and the massCompute stuff)

- PhysicsComponent (contains all the rest properties from the rigidbody like force, torque, tmp acceleration, next position, friction and restitution coeffs)

 

Copying code over from my physics engine into the new physics system and change this to use the entity and components like this (seems to be ugly):

        PhysicsSystem.prototype.update = function(entities, dt){
            var i, entity, posComp, velComp, massComp, phyComp, transComp, gravityComp;

            Profiler.begin("Physics step");

            // Add forces to dynamic bodies
            Profiler.begin("Integrate forces");
            var gravityForce = Vec2Pool.get();
            for (i = 0; i < entities.size(); i++) {
                entity = entities.item(i);
                velComp = entity.getComponent(this.velHandle);
                massComp = entity.getComponent(this.massHandle);
                phyComp = entity.getComponent(this.physicsHandle);
                gravityComp = entity.getComponent(this.gravityHandle);
                if (!massComp.isStatic() && gravityComp != null) {
                    math.vec2MultScalar(gravityForce, gravityComp.gravity, massComp.mass);
                    math.vec2Add(phyComp.force, phyComp.force, gravityForce);
                }
            }
            Profiler.end();

            // Integrate velocities
            Profiler.begin("Integrate velocity");
            for (i = 0; i < entities.size(); i++) {
                entity = entities.item(i);
                posComp = entity.getComponent(this.posHandle);
                velComp = entity.getComponent(this.velHandle);
                massComp = entity.getComponent(this.massHandle);
                phyComp = entity.getComponent(this.physicsHandle);
                if (!massComp.isStatic()) {
                    this.integrateVelocity(phyComp, massComp, posComp, velComp, dt);
                } else {
                    math.vec2Clone(phyComp.nextPosition, posComp.position);
                }
            }
            Profiler.end();
        };

which is orginally this:

        PhysicsEngine.prototype.step = function (dt) {
            var i, body;

            Profiler.begin("Physics step");

            // Add gravity force to dynamic bodies
            Profiler.begin("Integrate forces");
            var gravityForce = Vec2Pool.get();
            for (i = 0; i < this.world.size(); i++) {
                body = this.world.item(i);
                if (!body.isStatic()) {
                    math.vec2MultScalar(gravityForce, this.world.gravity, body.mass);
                    body.addForce(gravityForce);
                }
            }
            Profiler.end();

            // Integrate velocities
            Profiler.begin("Integrate velocity");
            for (i = 0; i < this.world.size(); i++) {
                body = this.world.item(i);
                if (!body.isStatic() && body.awake) {
                    body.integrateVelocity(dt);
                } else {
                    math.vec2Clone(body.nextPosition, body.position);
                }
            }
            Profiler.end();
            
            ...

The only thing which comes to my mind, is to create all the bodies for each entity which references all the properties from the components directly (i could do that because i have an notification system already) - which would force me to change my rigidbody class to use only property types which can be used as references. Other idea is to create a rigidbody component - which justs includes all the properties from all required components (of courses needs also be references as well)

 

Another things which bugs me is the number of components - is it a good idea to separate the rigidbody properties like this or is it better to use a single component (RigidbodyComponent) which just have all the properties from the original one, but would require that the position component uses the rigidbody position to not need to syncronize positions..... ahhh too much pain ... i have an headache now...

Edited by Finalspace
1

Share this post


Link to post
Share on other sites

I've also experienced that fine-grained components aren't all that good. I'm using a similar approach as phil_t, and also I'm not writing the physics-code in the component-system at all, each component will just register a rigid-body with my encapsulated "world"-class, which handles all the physics. So components are merely used as "Put it together", which IMHO is where they are best at.

 

Also, if you want a more "professional" proof for working with "larger" components instead of fine-grained onces, e.g. Unity does this too. I hardly know any practical, half-decent project that uses such broken-down components, though I've seen many people post this kind of design around here. Would really advice against it (I can write why in more detail tomorrow, if wanted).

Edited by Juliean
1

Share this post


Link to post
Share on other sites

I'll add my voice to the chorus and say that I find it much more practical to simply have a PhysicsComponent and a TransformComponent, similar to what phil_t and Juliean suggest. That's about the level of component granularity that I've seen In my professional experience with component based game engines. It's far simpler to manage and lets you have your physics stuff live in its own world, while the physics component basically just communicates stuff from the physics simulation (which is run by either your own home brew physics engine or Bullet/Havok/box2d/whatever physics library you choose to use) to the rest of the game.

1

Share this post


Link to post
Share on other sites

Ah ok - then i will use 3 components (ShapeComponent, PhysicsComponent, TransformComponent) and just instantiate the physics engine in the physics system and syncronize the entities with the bodies and call the step() method - The physics component will just have a "body" property which is a reference of the full rigidbody class - but the shape in the rigidbody class will be the reference from the shape component. Of course i need to also syncronize the transform properties like position and orientation, but should´t be too hard. Thanks for the great answers.

Edited by Finalspace
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0