merging bodies dynamically

Hello,

I'm completely new to Box2D, so bear with me if there's something I'm not quite getting. 🙂

I'm trying to build an editor type program in which the user could create shapes, and then choose to merge them dynamically, with a "weld joint." I'd like to make the joint have complete rigidity, so as I understand the way to do this is to destroy one of the bodies and add its shapes to the other body. I'd also like to preserve any other types of joints already attached to either of the bodies, so just recreating the shapes in the one body from their b2ShapeDefs is not quite enough.

My idea at this point is to write a function
Code:
b2Body *b2World::MergeBodies(b2Body *b1, b2Body *b2);

which would attach all the shapes and joints from b2 onto b1, destroy b2, and return a pointer to b1. This may be a decent amount of work, as I'd need to preserve the rotations of all the shapes in b2, and recreate b2ShapeDefs for them so that I can create new shapes attached to b1.

So basically I'm just wondering if anyone has attempted something like this already, if anyone has ideas on a better design, or if there's some way in Box2D to do this already that I'm missing. Thanks!
Well, I'm not quite sure if this helps your exact situation, but I was trying to do the same thing the other day and found that this worked pretty well. I basically just add a Revolute joint to the two objects and set its upper and lower angle to zero.

(Note: Code is in ActionScript.)

Code:
         var jointThing:b2RevoluteJointDef = new b2RevoluteJointDef();
         jointThing.Initialize(body1, body2, body1.GetWorldCenter());
         jointThing.enableMotor = false;
         jointThing.lowerAngle = 0;
         jointThing.upperAngle = 0;
         jointThing.enableLimit = true;
         
         _myWorld.CreateJoint(jointThing);



The physics engine still treats the two bodies separately, which may or may not be what you want, depending on your situation.

Hope that helps. (You know, in case you're still working on the problem 5 months later. 🙂 )
I've been hoping for something like a weld joint as well. To the best of my knowledge, the approach you describe is currently the only way to do it. I've tried KingSnorky's suggestion of using Revolute joints, and although that might work for your game, for mine I found that there was still a bit too much "give", and joined bodies still tended to wiggle a bit in relation to each other.

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