Inheritance is taught in school as the be-all end-all of flexibility and code reuse, but it has limitations.
I prefer to treat Box2D entirely as a black box, and keep my game code as a separate tree of subclasses. I am able to do anything with the userData, including changing the type of gameobject that is attached to a b2Body, without having to remove/insert bodies into the world.
The void* lack of type safety sucks, but its not a big deal, since your game objects should all come from one base class. Also, your game objects are probably going to be responding to render events, mouse/touch events, and other things that are best left separate from Box2D, not hacked into it in any significant way.
Here's my base game object class, maybe you can see how it is used to bring everything together, physics, rendering, user interaction:
Code:
class TSBox2DBody {
public:
static const int Solid = 0;
static const int OneSided = 1;
static const int Empty = 2;
TSFloat z;
std::string id;
std::string type;
TSBox2D* box2D;
b2Body* body;
TSSprite* sprite;
int behavior;
bool onewayReversed;
static Dictionary<b2Contact*, int> behaviorForContact;
TSBox2DBody(std::string type, TSBox2D* box2D, b2Body* body, TSFloat z, std::string sequencePath, int behavior);
virtual ~TSBox2DBody();
virtual void render(/* destination:BitmapData, */ TSFloat offsetX, TSFloat offsetY, TSFloat timeStep, GLuint color = 0xFFFFFFFF, bool isOffScreen = false, TSMatrix* matrix = NULL);
virtual void update(TSFloat timeStep, bool isOffScreen = false);
virtual void HandleBeginContact(b2Contact* contact);
virtual void HandleEndContact(b2Contact* contact);
virtual void complete();
virtual void touchBegan(TSTouch* touch, b2Vec2& box2DPos) {}
virtual void touchMoved(TSTouch* touch, b2Vec2& box2DPos, bool isTouching) {}
virtual void touchEnded(TSTouch* touch, b2Vec2& box2DPos, bool wasTouching) {}
static TSBox2DBody* getUserDataA(b2Contact* contact);
static TSBox2DBody* getUserDataB(b2Contact* contact);
static void BeginContact(b2Contact* contact);
static void EndContact(b2Contact* contact);
static void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
static void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
};
I do use Box2D for rendering and touch, but in a way that goes with the grain. I query the Box2D world for objects that are visible, and I query using a combination of raycast and tiny aabb to track touches. Thing is, my local copy of Box2D itself still knows nothing about touches or rendering. I just leverage the efficient 2D scene queries to my advantage.