0
\$\begingroup\$

I'm using Box2D 2.4.1 and trying to get a spring effect between two bodies. No matter how I change frequency or damping ratio with the Distance Joint, I see no effect..

Here's my setup

  b2BodyDef body1;
  body1.type = b2_dynamicBody;
  body1.position = b2Vec2(-1, -4);
  body1ptr = bWorld->CreateBody(&body1);

  b2BodyDef body2;
  body2.type = b2_dynamicBody;
  body2.position = b2Vec2(1, -4);
  body2ptr = bWorld->CreateBody(&body2);

  b2CircleShape circleShape;
  circleShape.m_radius = 0.40f;

  b2FixtureDef ballFixture;
  ballFixture.restitution = 0.8f;
  ballFixture.density = 5.f;
  ballFixture.shape = &circleShape;

  b2FixtureDef ballFixture2;
  ballFixture2.restitution = 0.8f;
  ballFixture2.density = 5.f;
  ballFixture2.shape = &circleShape;

  body1ptr->CreateFixture(&ballFixture);
  body2ptr->CreateFixture(&ballFixture2);


  float frequencyHz = 4.0f; // "Speed of oscillation" 1-5 typical
  float dampingRatio = 0.1f; // typical 0-1

  b2DistanceJointDef distDef;

  distDef2.Initialize( body1ptr, body2ptr, body1ptr->GetPosition(), body2ptr->GetPosition() );

  b2LinearStiffness(distDef2.stiffness, distDef2.damping, frequencyHz, dampingRatio, distDef2.bodyA, distDef2.bodyB);

  b2DistanceJoint* joint2 = (b2DistanceJoint*)bWorld->CreateJoint(&distDef2);

Box2D updates at fixed steps. Here is a gif showing what I have now:

Car moves rigidly

I don't know if I'm thinking of this whole thing wrong, but I was expecting it to compress as the little dude was applying pressure against the wall.

What am I doing wrong? Or thinking wrong?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Looking at the Release notes for Box2d 2.4.1, it states:

BREAKING: distance joint 0 stiffness now means the spring is turned off >rather than making the joint rigid.

BREAKING: distance joint minimum and maximum must be set correctly to get old behavior.

So setting minLength and maxLength of the joint did make it act more like the way i wanted. In my case, i set these parameters between distDef2.Initialize and b2LinearStiffness.

distDef.minLength = 0.1f;
distDef.maxLength = 4.f;

I did also change frequencyHz to 0.5f.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .