request: some kind of elastic spring

I'd appreciate,if above Springs.h (resp. derivated version) example could be added to the TestBed.
Image


Attached a first example.
you may remark, i like this "elastic compound body" 😀
Image
(above gif's velocity may be slower as the real one 🙄 )
to be continued....
I'll add this. I'll also plan to add the ability to define a soft distance constraint.
i got it working with a little workaround using a prismatic joint. when the distance between the connected elements becomes to far, i apply a force in the opposite direction to the bodies:

Code: Select all

Vector2DF posBody1 = Vector2DF(m_joint->GetBody1()->GetWorldCenter().x, m_joint->GetBody1()->GetWorldCenter().y);
      Vector2DF posBody2 = Vector2DF(m_joint->GetBody2()->GetWorldCenter().x, m_joint->GetBody2()->GetWorldCenter().y);

      float posX = posBody1.x - posBody2.x;
      float posY = posBody1.y - posBody2.y;
      float absPos = sqrt(posX * posX + posY * posY);

      if(absPos > 80.0f)
      {
         m_joint->GetBody1()->ApplyForce(b2Vec2(-posX * 50, -posY * 50), m_joint->GetBody1()->GetWorldCenter());
         m_joint->GetBody2()->ApplyForce(b2Vec2(posX * 50, posY * 50), m_joint->GetBody2()->GetWorldCenter());
      }
🙂 added some more joints and a lift ( conditional in step() )
Image
(above gif's velocity is not box2d's)

attached TestBed compatible Factory.h
Box2D/TestBed/Tests/ElasticBody.h update for SVN 139
I have been trying to replicate this in AS3 to use as a "soft joint" or suspension as it were.

The fact its for AS3 is quite irrelevant but i cant seem to get the maths to work in 2.0.1 ....

b2Vec2 diff = pB - pA; for example in the AddSpringForce method does not work. Also, b2Cross does not exist, i used b2Math.b2CrossVV(... , ...) but this is just a guess and does not work anyways because like above, i cant just subtract 2 vectors anymore?

Can anyone update this

void AddSpringForce(b2Body& bA, b2Vec2& localA, b2Body& bB, b2Vec2& localB, float32 k, float32 friction, float32 desiredDist)
{
b2Vec2 pA = bA.GetWorldPoint(localA);
b2Vec2 pB = bB.GetWorldPoint(localB);
b2Vec2 diff = pB - pA;
//Find velocities of attach points
b2Vec2 vA = bA.GetLinearVelocity() - b2Cross(bA.GetWorldVector(localA), bA.GetAngularVelocity());
b2Vec2 vB = bB.GetLinearVelocity() - b2Cross(bB.GetWorldVector(localB), bB.GetAngularVelocity());
b2Vec2 vdiff = vB-vA;
float32 dx = diff.Normalize(); //normalizes diff and puts length into dx
float32 vrel = vdiff.x*diff.x + vdiff.y*diff.y;
float32 forceMag = -k*(dx-desiredDist) - friction*vrel;
diff *= forceMag; // diff *= forceMag
bB.ApplyForce(diff, bA.GetWorldPoint(localA));
diff *= -1.0f;
bA.ApplyForce(diff, bB.GetWorldPoint(localB));
}
bump. same question as clark.

this is what i have


Code: Select all

      private function AddSpringForce(bA:b2Body, localA:b2Vec2, bB:b2Body, localB:b2Vec2, k:Number, friction:Number, desiredDist:Number):void {
         var pA:b2Vec2 = bA.GetWorldPoint(localA);
         var pB:b2Vec2 = bB.GetWorldPoint(localB);
         var diff:b2Vec2 = b2Math.SubtractVV(pB,pA);
         //Find velocities of attach points
         var vA:b2Vec2 = b2Math.SubtractVV(bA.m_linearVelocity, (b2Math.b2CrossVF(bA.GetWorldVector(localA), bA.m_angularVelocity)));
         var vB:b2Vec2 = b2Math.SubtractVV(bB.m_linearVelocity, (b2Math.b2CrossVF(bB.GetWorldVector(localB), bB.m_angularVelocity)));
         var vdiff:b2Vec2 = b2Math.SubtractVV(vB,vA);
         var dx:Number = diff.Normalize();
         //normalizes diff and puts length into dx
         var vrel:Number = vdiff.x * diff.x + vdiff.y * diff.y;
         var forceMag:Number = -k * (dx - desiredDist) - friction * vrel;
         diff = b2Math.MulFV(forceMag, diff);
         //diff *= forceMag; // diff *= forceMag
         bB.ApplyForce(diff, bA.GetWorldPoint(localA));
         //diff *= -1.0;
         diff = b2Math.MulFV(-1, diff);
         bA.ApplyForce(diff, bB.GetWorldPoint(localB));
         bA.WakeUp();
         bB.WakeUp();
      }
I cannot see anything wrong with this code, but I've got something very similar already working here:
http://boristhebrave.com/project/controller-grab-pack
Thanks BorisTheBrave, your SpringControler is working fine 🙂

.. But, is it possible to had a limit to the spring ?
Something like :

m_spring.enableLimit = true;
m_spring.maxLength = ..;

Thanks again for your great controller grab pack !

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