3

Hey i'm trying to make a class called BodyRep Which is supposed to use SFML too sync a graphical representation and Box2D Bodies orientation/location.

To do this i plan to ovveride some Box2D function so that it does it's original steps, plus one extra: to update the graphical representaions orientation based on the new Body orientation.

However I found this is not as easy as it seems as my class must be a descendent of another class that does the actual updates to the bodies in the World, so it CAN ovveride the function. On top of that, I know there is a ton of itterating in Box2D's solver, and i don't want my graphical reps. being updated if they don't have to yet....

So... What CLASS should I Descend from and what FUNCTION should I ovveride?

  • if you have any other ideas on how i could accomplish my goal of syncronization between SFML and Box2D bodies, feel free to suggest them! =)
Griffin
  • 630
  • 1
  • 12
  • 26
  • I think the common way is to just iterate through all box2d bodies in the graphics-update method and draw accordingly... Any reason you want to subclass a box2d class? That seems counter intuitive. – bummzack Jul 26 '11 at 07:34
  • the graphics update method is unfortunately const.... no changes alloud. I'm trying to keep my code consistent, instead of making a draw method that's specific only to BodyRep... – Griffin Jul 26 '11 at 19:13

1 Answers1

3

Box2D's body classes are not open for subclassing (nor are most of its other classes). They come out of a factory method on b2World, and they cannot (should not) be copied.

Generally, you should be using composition rather than inheritance:

struct BodyRep {
    b2Body *physics;
    sf::Sprite *visual;
};
....
for (BodyRep &e : drawable_bodies) {
    b2Vec2 pos = e.physics->GetPosition();
    e.visual->SetPosition(pos.x, pos.y);
    // + whatever other properties you need, e.g. size.
    window.Draw(*e.visual);
}