soft distance constraint

Hi.

I am trying to get somewhat physically correct suspension model using the b2DistanceJoint for mass-spring-damper simulation.
Given a mass resting on the joint. Running it with zero damping works great. The constraint will calculate a correction impulse that corresponds to the spring force and displacement. But things will get bad as soon as damping comes into play. It effectively reduces the impulse, the mass sinks lower with increasing damping value.
I wonder if there is a way to take this into account. To only apply damping if the velocity constraint solver is not able to reduce the relative velocity to zero?
I checked the math and it obey's Hooke's law.

Just in case, I made this test:

Code: Select all

class Web : public Test
{
public:
   Web()
   {
      b2Body* ground = NULL;
      {
         b2BodyDef bd;
         ground = m_world->CreateBody(&bd);

         b2EdgeShape shape;
         shape.Set(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
         ground->CreateFixture(&shape, 0.0f);
      }

      {
         b2PolygonShape shape;
         shape.SetAsBox(0.5f, 0.5f);

         b2BodyDef bd;
         bd.type = b2_dynamicBody;
         bd.allowSleep = false;
         bd.position.Set(0.0f, 2.0f);
         m_body = m_world->CreateBody(&bd);
         m_body->CreateFixture(&shape, 5.0f);

         b2DistanceJointDef jd;
         b2Vec2 p1, p2, d;

         jd.frequencyHz = 1.0f;
         jd.dampingRatio = 0.0f;

         jd.bodyA = ground;
         jd.bodyB = m_body;
         jd.localAnchorA.Set(0.0f, 4.0f);
         jd.localAnchorB.Set(0.0f, 0.0f);
         p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
         p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
         d = p2 - p1;
         jd.length = d.Length();
         m_world->CreateJoint(&jd);
      }
   }

   void Step(Settings* settings)
   {
      Test::Step(settings);

      b2Vec2 p = m_body->GetPosition();
      m_debugDraw.DrawString(5, m_textLine, "Height = %g", p.y);
      m_textLine += 15;
   }

   static Test* Create()
   {
      return new Web;
   }

   b2Body* m_body;
};


I tried various damping ratios and the height would always go to 1.7467. Are your bodies going to sleep?
Thanks for the test. The numbers are correct indeed. The error was in my code. Sorry for the noise.

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