managing b2Body/Fixture/Joint userData

having first used a dynamic language port of box2d, i'm unhappy with m_userData (b2Body/Fixture/Joint) being void*.

managing deallocation of objects pointed to by m_userData is tedious since destroy hooks are not provided for the 3 classes mentioned.

is it expected that only data which is managed elsewhere will be assigned to m_userData?
There are destroy hooks for fixtures and joints since they can be implicitly destroyed when you destroy a body. Bodies can only be destroyed when you destroy them (assuming you destroy them before destroying the world). So when you destroy a body, you should also destroy the user data. It would be inefficient and confusing to invoke a callback in this case.

User data is null by default and Box2D cannot take ownership of such data because it doesn't know how it was allocated.

In the games I work on, I usually create user data from object pools. All bodies will have the same type of user data, so casting is never an issue. So I will have structures like:
Code:
struct MyBodyData;
struct MyFixtureData;
struct MyJointData;


Then I usually have a class that owns the world. That class maintains the user data pools and has create and destroy wrappers that manage the user data when a physics entity is created or destroyed. Then the rest of the game uses those wrappers instead of directly calling the b2World::Create/Destroy functions.
hmm, wrapping b2world is one solution, although i'd prefer a more direct approach.

one issue i can think of is deciding who will handle the SayGoodbyeFixture / SayGoodbyeJoint callbacks. the wrapper would have to free my userdata there, but the game code may also be interested in these events.

would making the destroy/create of b2Body/Fixture/Joint virtual be problematic?

that way i can subclass and handle their lifetime myself.

i'm just suggesting this as a solution in my own code, not as a feature request - although i don't see why b2 doesn't support subclassing in general.
It is not always simply a matter of wrapping b2World. Your game should have some sort of scene class already that stores all your actors. This is a good place to store the b2World. If you don't have a scene class you can create a physics manager that owns a b2World. Even then, it is not simply wrapping. Such a class is useful for adapting the physics engine to your game specific needs.

Different games use physics engines in different ways and Box2D cannot serve them all in an immediate way. Therefore, it is useful to write some code around the engine that adapts the physics engine to your game's usage patterns. For example, suppose your game has unique force fields, you can implement those within your scene class or physics manager. You may also write some code here that processes contact points and feeds the results to your actors and sound system.

The destruction listener should be a friend of your scene class. When the destruction listener is called, it can inform the scene and the relevant actors. That way the user data can be returned to the pools.

C++ Box2D uses a high performance allocator for small objects. It would not know how to allocate your objects. This is why Box2D uses factories.
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.

Original thread: https://box2d.org/forum/viewtopic.php?t=6686