5

I have two dynamic bodies attached together with a revolute joint. Both of the bodies are also connected to a static body on the other ends. I need the joint between the two dynamic bodies to break when more than a certain amount of force is applied, that is, when there's more mass than the joint can resist. How is this done in Box2d? I guess this is not handled by Box2d automatically. Here's the graphical overview of what I want to achieve.

enter image description here

Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206

1 Answers1

5

Use b2Joint::GetReactionForce.

In each time step check, if reaction force of the joint is less then some critical value. When force became greather, destroy the joint.

void update(float timeStep)
{
    b2Vec2 reactionForce = joint->GetReactionForce(1/timeStep);
    float forceModuleSq = reactionForce.LengthSquared();
    if(forceModuleSq > maxForceSq)
    {
        world->DestroyJoint(joint);
    }
}

world - pointer to b2World, joint - pointer to your b2RevoluteJoint, maxForceSq - square of the max force.

Look, there calculates LengthSquared, and compares with squared maxForce. This will improve performance, because no need to calculate square root.

Max force can be calculated as gravitational force: maxMass*9.8.

Pavel
  • 2,602
  • 1
  • 27
  • 34
  • thank you for the answer. I've also been rehearsing GetReactionForce except I do not understand the role of the argument it takes. I've just been passing 1.0 without having any clue about it. Can you tell me what that argument is for? – Mikayil Abdullayev Feb 25 '13 at 19:04
  • It is inverted time step, i.e. 1/timeStep (as you can see in my code). timeStep is the value, that you pass to b2World::Step as first parameter. – Pavel Feb 25 '13 at 19:15
  • And where do I get the maxMass? – Mikayil Abdullayev Feb 25 '13 at 19:34
  • It is the mass of "object with certain mass" from your question. Try, and find needed value. – Pavel Feb 25 '13 at 20:31