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! =)
  • \$\begingroup\$ 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. \$\endgroup\$ – bummzack Jul 26 '11 at 7:34
  • \$\begingroup\$ 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... \$\endgroup\$ – Griffin Jul 26 '11 at 19:13

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.