Jacobian for a rope constraint

ola
Posts: 169
Joined: Sun Jan 14, 2007 7:56 pm
Location: Norway

Jacobian for a rope constraint

Post by ola »

Hi all,

I need a constraint that works like a simple rope for my application. That means, no collision is needed between the rope and something else, it just has to keep the distance between the connection points on two rigid bodies below a defined rope length. As a second mode, it can be used to keep this distance constant, to simulate a rod connecting the two bodies instead.

So I started writing one. It is based on the btPoint2PointConstraint and the basics are simple enough as it is easy to figure out the direction of the impulse vectors -- always in the direction of the rope/rod.

But I have problems comprehending how to use the Jacobian properly to dimension the magnitude of the impulse in this case. I would really appreciate any hints on how to do this!

Below, the code so far. With the current impulse it works as if the rope was a rubber band (as expected).

If it works properly in the end I'd be happy to give it to bullet if you would like to have it.

Best regards,
Ola

Code: Select all

void btRopeConstraint::solveConstraint(btScalar timeStep)
{
 btVector3 pivotAInW = m_rbA.getCenterOfMassTransform()*pivot_in_a_;
 btVector3 pivotBInW = m_rbB.getCenterOfMassTransform()*pivot_in_b_;

 // vector from A to B
 btVector3 dist_a_b = pivotAInW - pivotBInW;
 // absolute distance between A and B
 double    abs_dist = dist_a_b.length();
 
 // if the distance is shorter than the rope length just quit here (comment out for constant distance)
//  if(abs_dist < length_) 
//     return;
 
 // Create a vector that contains the error (stretch/depth) of the rope 
 dist_a_b             = dist_a_b * length_ / abs_dist;
 btVector3 depth      = pivotAInW - pivotBInW - dist_a_b;
 btScalar abs_depth   = depth.length();
 btVector3 norm_depth = depth.normalize();
   
 // Velocity stuff
 btVector3 rel_pos1 = pivotAInW - m_rbA.getCenterOfMassPosition(); 
 btVector3 rel_pos2 = pivotBInW - m_rbB.getCenterOfMassPosition();
 btVector3 vel1 = m_rbA.getVelocityInLocalPoint(rel_pos1);
 btVector3 vel2 = m_rbB.getVelocityInLocalPoint(rel_pos2);
 btVector3 vel = vel1 - vel2;
 btScalar rel_vel;
 rel_vel = norm_depth.dot(vel);

 // TODO: Figure out the Jacobian value
 //  btScalar abs_impulse = abs_depth*tau_/timeStep * jacDiagABInv - damping_ * rel_vel * jacDiagABInv;
 // cheat instead for now
 btScalar abs_impulse = -abs_depth * 1.0;
 
 m_rbA.applyImpulse( norm_depth*abs_impulse, pivotAInW - m_rbA.getCenterOfMassPosition());
 m_rbB.applyImpulse(-norm_depth*abs_impulse, pivotBInW - m_rbB.getCenterOfMassPosition());
}
AlexSilverman
Posts: 141
Joined: Mon Jul 02, 2007 5:12 pm

Post by AlexSilverman »

How is this working for you? I am going to try and simulate a basketball net, and I'm looking at the best way to do so. Any suggestions from the peanut gallery?

- Alex
Zimbarbo
Posts: 14
Joined: Thu Jun 28, 2007 2:26 pm

Hinge

Post by Zimbarbo »

It sounds like you are doing a hinge? There is a simple example in the constraint demo. I used a hinge to simulate a grapple hook/swing effect for my game. You can also add constraints so it only swings certain ways.
ola
Posts: 169
Joined: Sun Jan 14, 2007 7:56 pm
Location: Norway

Post by ola »

I haven't gotten any further on this yet. It's still a bit above me to figure it out, so I've concentrated on other stuff in the mean time. :-)

Cheers
Ola
stevenLD
Posts: 10
Joined: Fri Sep 14, 2007 1:27 am

Re: Jacobian for a rope constraint

Post by stevenLD »

I hate necroing posts, but the alternative was starting my own thread for the same topic.

I also need a rope-like constraint and, unfortunately, I'm not all that good at physics, it seems. Like the previous poster, I have a rubberband-like effect now, but I just dont know enough to make it act like a rope.

Any help would be appreciated. :)

*edit*
My code is (obviously) very similar to that above:
*edit 2*
My code is trash. Not worth posting. :-(
Last edited by stevenLD on Thu Oct 04, 2007 6:07 pm, edited 1 time in total.
AlexSilverman
Posts: 141
Joined: Mon Jul 02, 2007 5:12 pm

Re: Jacobian for a rope constraint

Post by AlexSilverman »

I ended up adapting a basic stick constraint, usually seen with a verlet integrator, and writing a new constraint class. Instead of setting the position, I apply a central impulse to the body in question. I tuned mine so it has a bit of spring to it, but I would think you could easily change it to be more rigid.

- Alex
stevenLD
Posts: 10
Joined: Fri Sep 14, 2007 1:27 am

Re: Jacobian for a rope constraint

Post by stevenLD »

What're the chances I could get you to share that with me? :)

What I need to do is simulate a buoy floating on the water (done) but tied to an anchor point below the water surface so that 1) it cannot move "too far" from its origin, and 2) if an object (like a boat) pushes the buoy "too far", the constraint will cause the buoy to go under water and eventually under the pushing object to stay within its range.
Shreejan Shrestha
Posts: 7
Joined: Fri Aug 08, 2008 2:51 am

Re: Jacobian for a rope constraint

Post by Shreejan Shrestha »

Hello Ola,

I am trying to simulate rope using btGeneric6DofConstraint. But it is behaving elastic which is undesirable. Can you share the rope constraint?

Thanks in advance.
Shreejan.
ola
Posts: 169
Joined: Sun Jan 14, 2007 7:56 pm
Location: Norway

Re: Jacobian for a rope constraint

Post by ola »

Hi Shreejan,

I'm sorry but I never finished that constraint. It's more than a year ago that I did anything with it, more important tasks kept coming in the way...

Nowadays I think you'll be far better off using the softbody ropes -- they even collide with other geometry, if you want them to! Take a look at the SoftBodyDemo for how to use them.

Best regards,
Ola
Dirk Gregorius
Posts: 861
Joined: Sun Jul 03, 2005 4:06 pm
Location: Kirkland, WA

Re: Jacobian for a rope constraint

Post by Dirk Gregorius »

I assume you mean a distance constraint. Here is how it works:

1) Position constraint
C = | x2 + r2 - x1 - r1 | - L0

2) Velocity constraint
C_dot = ( x2 + r2 - x1 - r1 ) / | x2 + r2 - x1 - r1 | * ( v2 + cross( omega2, r2 ) - v1 - cross( omega1, r1 ) ) = n * ( v2 + cross( omega2, r2 ) - v1 - cross( omega1, r1 ) ) = - n * v1 - cross( r1, n ) * omega1 + n * v2 + cross( r2, n ) * omega2

3) Find Jaobian by inspection
J = ( -n | -cross( r1, n ) | n | cross( r2, n ) )

4) Solve
J*W*JT * lambda = -tau * C / dt - J * v



The position constraint basically says, that the distance between the two pivot points on the rigid bodies should be constant (L0). The velocity constraint requires that the relative velocity in the direction of the pivot offset should vanish. The Jacobian encodes the forbidden velocity directions which we will cancel out with the sequentiel impulses.

One tip if you model a rope using the constraint also add a distance constraint between every second particle as bending constraints. For many constraints make your tau small when warmstarting, e.g. 0.005 - 0.001


HTH,
-Dirk
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Jacobian for a rope constraint

Post by Erwin Coumans »

ola wrote:Nowadays I think you'll be far better off using the softbody ropes -- they even collide with other geometry, if you want them to! Take a look at the SoftBodyDemo for how to use them.
Indeed, if you can please try using the new soft body ropes and give us some feedback on that.

Erwin
Shreejan Shrestha
Posts: 7
Joined: Fri Aug 08, 2008 2:51 am

Re: Jacobian for a rope constraint

Post by Shreejan Shrestha »

Hello Ola, Dirk Gregorius, Erwin Coumans

Thanks for your responses.

Have tried to use the softbody ropes, they work great.
For Dirk Gregorius:

Currently not being able to implement the constraint for rope as I have no what so ever mathematical/physics knowledge about the constraint. Anyway thanks for your explanation. Also if you have some resources for those please provide to me. For now I have implemented the rope using btGeneric6DofContraint which is behaving elastic. Also you have mentioned about the distance constraint can it be applied to it?

Thanks to all....
Last edited by Shreejan Shrestha on Wed Sep 03, 2008 1:34 am, edited 1 time in total.
Dirk Gregorius
Posts: 861
Joined: Sun Jul 03, 2005 4:06 pm
Location: Kirkland, WA

Re: Jacobian for a rope constraint

Post by Dirk Gregorius »

Erwin has a great reference here: http://www.bulletphysics.com/Bullet/php ... p?f=6&t=63
A nearly similar reference can be also found here: http://www.gamedev.net/community/forums ... _id=475753

A good start are the GDC presentations and associated 2D implementation by Erin Catto. Look at http://www.gphysics.com/downloads for the GDC presentation from 2006 - 2008. They contain a project called Box2D_Lite. It is a good start to learn and you can see the generalization in the full Box2D here: www.box2d.org. For a 3D implementation look at Bullet then.
Shreejan Shrestha
Posts: 7
Joined: Fri Aug 08, 2008 2:51 am

Re: Jacobian for a rope constraint

Post by Shreejan Shrestha »

Hello Dirk Gregorius,

Thanks a lot for providing me links. Had a look at www.box2d.org, seems great for learning....

Shreejan.