Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 614419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:06:36+00:00 2026-05-13T18:06:36+00:00

In my game engine I have a State class that represents the game world

  • 0

In my game engine I have a State class that represents the game world at a point in time. This State contains a number of Body objects that define one rigid body each. Each State has a container to keep track of the Body objects it owns, and each Body has a pointer back to its parent State.

Outline of State class:

class State {
private:
  std::set<Body*> _bodies;

public:
  //It could be done here
  void addBody(Body* body) {
    //Remove from old State
    if (body->_state)
      body->_state->_bodies.erase(this);

    //Set Body's back-reference
    body->_state = this;

    //Add to this State
    _bodies.insert(body);
  }
};

Outline of Body class:

class Body {
private:
  State* _state;

public:
  //It could also be done here
  void setState(State* state) {
    //Remove from old State
    if (_state)
      _state->_bodies.erase(this);

    //Set back-reference
    _state = state;

    //Add to the new State
    if (_state)
      _state->bodies.insert(this);
  }
};

The problem, if there is one, is that adding/removing a Body to/from a State requires changing a private member of each.

At one point I considered having a State-Body-mediator static class, which would have friend access to both classes. It sounds nice, because the State-Body relationship would be explicitly managed by that name, but it’s a lot of declaration overhead for managing a simple relationship.

Is there a “better” way to do this? I’d like to see what kinds of ideas are out there.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:06:36+00:00Added an answer on May 13, 2026 at 6:06 pm

    Well, one way you could do it would be to have a friend function of both classes:

    void associate(State& s, Body& b)
    {
        s.addBody(&b);
        b.setState(&s);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an AS3/Flex4 game engine, and I want to have a base class
I've been working on a card game engine in C# .NET for some time
I'm writing a simple game engine, and I have the EntityComponent.h file: #ifndef Psycho2D_Core_EntityComponent_
I have a bunch of classes in a basic (and badly coded!) game engine
I'm making a component based entity system for a game engine. I have an
I have been dabbling in game development as a hobby for a while now,
I am building a game engine, and I am actually having a very difficult
I have a pretty reasonable amount of experience in PHP (around ~5 years of
I cannot for the heck of it find why this is throwing an error!
I'm having a major issue which has been bugging me for a while now.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.