Editing Shapes

Hi,

I'm in the process of writing a level editor for my game - it's essentially just a GUI panel that pauses the game when you open it and allows you to tweak stuff to do with shapes and bodies (and sprites and various other stuff in the game). I don't plan on allowing this kind of user-editing of shapes and bodies in the game itself, and the game is paused whilst it's all happening, so I can cope with a degree of odd behaviour from Box2D during editing so long as nothing crashes or asserts or goes world-breakingly crazy.

For bodies I just hacked a couple of Set functions into b2Body so that the editor can set a body's angular and linear damping, and whether or not it has fixed rotation, without the need to completely destroy and recreate the body. Shapes seemed a little more complex, so I've been trying what seems to be the generally-accepted method of filling out a b2ShapeDef with the current shape's information, destroying the shape, and then recreating it using the b2ShapeDef. This is causing some headaches for me though, since parts of my code keep pointers to shapes around (my sprites keep pointers so they can use the vertices of b2PolygonShapes directly for rendering, for example), so I'd have to patch up all of those pointers when destroying and recreating a shape.

I'm wondering if, instead of having to fix up all of the pointers, I could just cheekily fudge b2CircleShape and b2PolygonShape to give me the non-const access I need to edit vertex positions/counts and circle radii/local positions, or perhaps just make my editor class a friend class of the b2CircleShape and b2Polygon shape so I can fiddle with stuff directly. Am I likely to run into big problems by doing things this way, and should I be aware of anything else I'd need to do to ensure that the internal states of shapes remains sensible and consistent?
This is just my personal opinion (which you can take or leave), but if it were me, rather than fiddling with the Box2D internals, I'd adjust my own code so that destroying and recreating bodies and shapes (where necessary) didn't pose any problems.

Perhaps Erin will offer a more in-depth answer, but I would guess that changing shape properties as you describe might be problematic due to the various interdependencies within Box2D (e.g. broad phase, mass properties, contact tracking, etc.). Of course you could always look at the source and see for yourself, but given the complexity of the source it might be somewhat difficult to identify all such dependencies without doing quite a bit of digging.

To me, what you describe sounds like a software design issue, more or less. You didn't mention what language you're using, but generally speaking, there are definitely ways you can set things up so that references to existing objects can be maintained, but dynamic modifications to those objects will still be reflected.
I'm using the C++ version, and my codebase is already reasonably mature and sizeable at this point. There are ways I could patch up all the pointers (i.e iterate through everything in the game that keeps a pointer to a b2Shape, and if it points to the old destroyed shape, make it point at the new recreated one), but that's a fair bit of work, and is prone to errors if I miss anything. Another way would be to have some kind of centralised table of pointers, and have things just have handles to an entry in that table, so I could just have one place I need to change - but that's a lot of work too, and comes with a runtime cost as well (since instead of just derefencing a pointer, I'd have to use a handle to look into the table, find the pointer, and dereference that). Generally speaking I'm quite careful to make the code resilient to things being created and destroyed but in the case of b2Shapes having to cope with that just for the sake of being able to tweak stuff in an editor seems like overkill. Modifying the shapes directly seems like the path of least resistance, so I'm just wondering as to the nature of the can of worms I'd be opening if I went down that route.
I would create a parallel object for each shape, say MyShapeHolder. This will hold the shape pointer. Then you can share a pointer to MyShapeHolder instead of the b2Shape. You can probably fit this into your code base without too much difficulty.

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