jBox2D rope joint

I have been using jbox2d in Android for the past few months and wanted to make a rope for use in my game, but as jbox2d didn't have one I had to work it out. Going back through the box2d repository history I found a version that would convert nicely and have pasted my rope code below, using my test box it works perfectly and I can swing an object backwards and forwards fine. Haven't done any tests with dynamic falling ropes like in the standard box2d test box but it works how I want it so I'm quite happy.

Any improvements or comments, please leave them below.

RopeJoint.java

Code: Select all

import org.jbox2d.common.Mat22;
import org.jbox2d.common.Settings;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.TimeStep;
import org.jbox2d.pooling.IWorldPool;

public class RopeJoint extends Joint
{
   protected Vec2 _localAnchorA = new Vec2();
   protected Vec2 _localAnchorB = new Vec2();
   protected float _maxLength;
   protected float _length;
   protected float _impulse;
   protected float _mass;
   
   protected Vec2 _u = new Vec2();
   protected Vec2 _rA = new Vec2();
   protected Vec2 _rB = new Vec2();
   protected Vec2 _localCenterA = new Vec2();
   protected Vec2 _localCenterB = new Vec2();
   protected LimitState _state;
   
   protected RopeJoint(IWorldPool argPool, RopeJointDef def)
   {
      super(argPool, def);
      
      _localAnchorA.set(def.localAnchorA);
      _localAnchorB.set(def.localAnchorB);
      
      _maxLength = def.maxLength;
      
      _mass = 0.0f;
      _impulse = 0.0f;
      _state = LimitState.INACTIVE;
      _length = 0.0f;
   }

   @Override
   public void getAnchorA(Vec2 argOut)
   {
      m_bodyA.getWorldPointToOut(_localAnchorA, argOut);
   }

   @Override
   public void getAnchorB(Vec2 argOut)
   {
      m_bodyB.getWorldPointToOut(_localAnchorB, argOut);
   }
   
   @Override
   public void getReactionForce(float inv_dt, Vec2 argOut)
   {
      argOut.set(_u.mul(_impulse * inv_dt));
   }

   @Override
   public float getReactionTorque(float inv_dt)
   {
      return 0.0f;
   }

   public float getMaxLength()
   {
      return _maxLength;
   }
   
   public void setMaxLength(float length)
   {
      _maxLength = length;
   }
   
   public LimitState getLimitState()
   {
      return _state;
   }

   @Override
   public void initVelocityConstraints(TimeStep step)
   {
      Body bA = m_bodyA;
      Body bB = m_bodyB;
      
      _rA = Mat22.mul(bA.getTransform().R, _localAnchorA.sub(bA.getLocalCenter()));
      
      _u.set(bB.m_sweep.c.add(_rB).sub(bA.m_sweep.c).sub(_rA));
      _length = _u.length();
      
      float C = _length = _maxLength;
      
      if (C > 0.0f)
         _state = LimitState.AT_UPPER;
      else
         _state = LimitState.INACTIVE;
      
      if (_length > Settings.linearSlop)
      {
         _u.mulLocal(1.0f / _length);
      }
      else
      {
         _u.setZero();
         _mass = 0.0f;
         _impulse = 0.0f;
         
         return;
      }
      
      float crA = Vec2.cross(_rA, _u);
      float crB = Vec2.cross(_rB, _u);
      float invMass = bA.m_invMass + bA.m_invI * crA * crA + bB.m_invMass + bB.m_invI * crB * crB;
      
      _mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
      
      if (step.warmStarting)
      {
         _impulse *= step.dtRatio;
         
         final Vec2 P = pool.popVec2();
         
         P.set(_u.mul(_impulse));
         
         bA.m_linearVelocity.subLocal(P.mul(bA.m_invMass));
         bA.m_angularVelocity -= bA.m_invI * Vec2.cross(_rA, P);

         bB.m_linearVelocity.addLocal(P.mul(bB.m_invMass));
         bB.m_angularVelocity += bB.m_invI * Vec2.cross(_rB, P);
         
         pool.pushVec2(1);
      }
      else
      {
         _impulse = 0f;
      }
   }

   @Override
   public void solveVelocityConstraints(TimeStep step)
   {
      Body bA = m_bodyA;
      Body bB = m_bodyB;
      
      final Vec2 vpA = pool.popVec2();
      final Vec2 vpB = pool.popVec2();
      
      vpA.set(bA.m_linearVelocity.add(Vec2.cross(bA.m_angularVelocity, _rA)));
      vpB.set(bB.m_linearVelocity.add(Vec2.cross(bB.m_angularVelocity, _rB)));
      
      float C = _length - _maxLength;
      float Cdot = Vec2.dot(_u, vpB.sub(vpA));
      
      if (C < 0.0f)
         Cdot += step.inv_dt * C;
      
      float impulse = -_mass * Cdot;
      float oldImpulse = _impulse;
      
      _impulse = Math.min(0, _impulse + impulse);
      impulse = _impulse - oldImpulse;
      
      final Vec2 P = pool.popVec2();
      P.set(_u.mul(impulse));
      
      bA.m_linearVelocity.subLocal(P.mul(bA.m_invMass));
      bA.m_angularVelocity -= bA.m_invI * Vec2.cross(_rA, P);
      bB.m_linearVelocity.addLocal(P.mul(bB.m_invMass));
      bB.m_angularVelocity += bB.m_invI * Vec2.cross(_rB, P);
      
      pool.pushVec2(3);
   }

   @Override
   public boolean solvePositionConstraints(float baumgarte)
   {
      Body bA = m_bodyA;
      Body bB = m_bodyB;
      
      final Vec2 rA = pool.popVec2();
      final Vec2 rB = pool.popVec2();
      final Vec2 u = pool.popVec2();
      final Vec2 P = pool.popVec2();
      
      rA.set(Mat22.mul(bA.getTransform().R, _localAnchorA.sub(bA.getLocalCenter())));
      rB.set(Mat22.mul(bB.getTransform().R, _localAnchorB.sub(bB.getLocalCenter())));
      
      u.set(bB.m_sweep.c.add(rB).sub(bA.m_sweep.c).sub(rA));
      
      float length = u.normalize();
      float C = length - _maxLength;
      
      C = Math.max(0.0f, Math.min(C, Settings.maxLinearCorrection));
      
      float impulse = -_mass * C;
      P.set(u.mul(impulse));
      
      bA.m_sweep.c.subLocal(P.mul(bA.m_invMass));
      bA.m_sweep.a -= bA.m_invI * Vec2.cross(rA, P);
      bB.m_sweep.c.addLocal(P.mul(bB.m_invMass));
      bB.m_sweep.a += bB.m_invI * Vec2.cross(rB, P);
      
      bA.synchronizeTransform();
      bB.synchronizeTransform();
      
      pool.pushVec2(4);
      
      return ((length - _maxLength) < Settings.linearSlop);
   }
}


RopeJointDef.java

Code: Select all

import org.jbox2d.common.Vec2;

public class RopeJointDef extends JointDef
{
   public RopeJointDef()
   {
      type = JointType.ROPE;
      localAnchorA = new Vec2(-1, 0);
      localAnchorB = new Vec2(1, 0);
      maxLength = 0f;
   }
   
   public Vec2 localAnchorA;
   public Vec2 localAnchorB;
   public float maxLength;
}
Nice! Could you post your code for RopeJointDef as well?
Sorry about that, I didn't think about posting that because I really just copied the other ones.

Enjoy
Actually I just realized that RopeJoint is in the latest JBox2D version (2.2.1.1) - https://code.google.com/p/jbox2d/source ... s%2Fjoints
lol, uploaded about a week after I originally posted this. I hadn't been back to the jBox2D page since then because it hadn't been updated in so long (a full year).
Box2D 2.2 support rope joint.You can refer this if you are looking Rope Joint def please click here: http://blog.allanbishop.com/wp-content/uploads/2012/05/b2RopeJointDef.as .For more information about rope joint visit this: https://github.com/aaronfarr/box2Dweb

Get More Information Here about Business Phones.

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