Search code examples
design-patternsrendererphysics-engine

How should I structure my very simple physics simulation/renderer?


I'm trying to program a really simple rigid body physics simulation (using c++ and OpenGL) for learning purposes (also please excuse me in advance but English is not my first language).

Now, so far my classes are structured like this:

Agent class:

  • Moving entity in the simulation
  • Contains typical variables like position, velocity, max speed, etc etc

Obstacle class:

  • Fixed entity in the simulation (like walls)

Simulation class:

  • Contains a list of all Agent and Obstacle objects and update theirs state at every tick of the program loop

Renderer class:

  • Responsable to initialize OpenGL
  • Update method takes a pointer to the simulation object and renders the state to screen.

Now everything works but I'm having problems on how to structure my program further to implement the following features:

  • Stuff like vertex, textures and which shader to use is currently hard-coded in the renderer. I'd like to being able to change those on a per Agent object basis but I recognize that those variables have no place in neither the Agent class nor the Simulation class. Where should I keep track of those?

  • I'd like to add some debug / visualization stuff in the program. For example, given a selected agent, the program will plot a dot after a certain amount of distance traveled (eg: 1 dot every 2 unity traveled) so I can have kind of a trail to visualize where the agent went. What class would be responsable of:

    1. Tracking which one is the selected agent
    2. Creating and tracking dots on the basis of the selected agent distance traveled and position
    3. Eventually fading and than deleting the older dots I don't think that neither the Simulation class nor the Render class should have the responsibility to track this extra data (ideally the Render should only be aware of the dots in order to render them).

Sorry for the long-winded post but I've been trying to solve those problems for two weeks now but nothing good spring to mind.

I know I could probably mitigate those problems using an Entity Component Systems architecture but I really prefer to keep it as simple as possible.


Solution

  • Entity / Component architectures are what you need I guess.There is a lot of stuff about this over the internet and I encourage you to google it and look for it on stack overflow and gamedev stackexchange.

    An example of this (not the only one and maybe not good for you, but it's an example) could be something like

    struct Entity
    {
        int m_Id;
    };
    
    struct PhysicsComponent {
        unsigned long m_Flags;
        // Some data
    };
    struct GraphicsComponent {
        Texture m_Texture;
        Mesh    m_Mesh;
        // Some other data
    };
    
    Actor actors[MAX_ACTORS];
    Wall walls[MAX_WALLS];
    GraphicsComponent graphics_components[MAX_GRAPHICS_COMPONENTS];
    
    void UpdatePhysics(float dt) {
        // Do you logic stuff, using your flags and so on
    }
    
    void Render() {
        // Render your stuff by retrieving position / orientation via entities ID 
    }