Rope Physics in 2D

Please don't post Bullet support questions here, use the above forums instead.
Post Reply
cycloverid
Posts: 8
Joined: Mon Sep 03, 2007 5:11 am

Rope Physics in 2D

Post by cycloverid »

I'm having problems implementing a simple rope simulation in 2D. I have been reading articles, as well as studying implementations. One example of an implementation I looked at is the Box2D "multi-pendulum".

All simulations of ropes I've seen so far avoid any kind of mesh, and instead use masses connected to each other via springs, or rigid bodies connected with joints.

For now, I'm not concerned with the rope colliding at all. My primary problem is coming up with a stable equation for motion. The simulations I have seen are typically more like pendulums or springs ( which end up being very touchy, and oftentimes erratic ). I'm trying to develop something that is gentle balance between not allowing too much elasticity, while still getting a smooth motion ( even if masses and force are added mid-simulation ). Literally, just a rope.

I'm getting the idea that this is a difficult problem to solve. Spring constants ( which I think Erin Catto calls "softness" when dealing with joint stiffness ) and damping don't seem to accomplish the effect of a rope, no matter what values I try.

Can anyone either explain how a rope effect would be accomplished ( either using the conventional rigid-joint, mass-spring, or any other method ). Perhaps I'm doing something wrong, or perhaps the problem is too difficult to solve, meanwhile still being simple :). I certainly admit to not being very knowledgeable about the subject.
ewjordan
Posts: 26
Joined: Sat Jun 30, 2007 4:34 am

Re: Rope Physics in 2D

Post by ewjordan »

If all you need is rope stuff (and you don't need a full rigid body simulator), Verlet integration plus distance constraints by position projection is the simplest approach, and it's pretty fast. For each particle you store the current position and the last position, and time stepping is done as follows, where ax is the x component of the acceleration, assuming a fixed time step:

Code: Select all

float xBuffer = x;
x = 2*x - xLast + ax*deltaTSquared;
xLast = xBuffer;
Do the same for the y coords. You also need constraints, which look more or less like this, where pA and pB are the two particles, and you're trying to keep them a constant distance apart (distanceSqr is the desired distance squared):

Code: Select all

    float myDistSqr = pA.position.distanceSqr(pB.position);
    float alpha = sqrt(distanceSqr/ (myDistSqr + .001)); //ok if distanceSqr == 0, since alpha is then zero
    float oneminusalpha = 1.0-alpha;
    float oneoversum = 1.0/(pA.mass + pB.mass);
    float cmx = (pA.mass*pA.position.x + pB.mass*pB.position.x) * oneoversum;
    float cmy = (pA.mass*pA.position.y + pB.mass*pB.position.y) * oneoversum;
    pA.position.x = cmx*oneminusalpha + alpha*pA.position.x;
    pA.position.y = cmy*oneminusalpha + alpha*pA.position.y;
    pB.position.x = cmx*oneminusalpha + alpha*pB.position.x;
    pB.position.y = cmy*oneminusalpha + alpha*pB.position.y;
I didn't optimize this completely, so you may be able to clean it up a bit by playing with the algebra. If you make a whole string of particles and then loop through them applying this constraint (you may have to do several iterations of the constraints, more will lead to a tighter, less springy rope), you'll end up with a fairly reasonable rope, much better than you'll get if you do a particle + spring simulation. If you're pinning one end of a rope to a wall, I would suggest setting the mass of the pinned particle to a fairly high value and applying the constraints starting from that end, as it should speed up the convergence a bit.

If you need more, you can even try connecting springs to every other particle (i.e. a spring between particles 0 and 2, 1 and 3, etc) and/or every third particle, which will give the rope a bit of stiffness. Once you start adding springs, you need to watch your spring constants lest you run into stability issues; pure position projection + Verlet integration should be almost unconditionally stable without the springs, though, at least for a rope configuration.

This is not the ideal approach for more general physics, though, so don't try to build a full featured rigid body engine out of these types of particle systems...
cycloverid
Posts: 8
Joined: Mon Sep 03, 2007 5:11 am

Re: Rope Physics in 2D

Post by cycloverid »

This is not the ideal approach for more general physics, though, so don't try to build a full featured rigid body engine out of these types of particle systems...
No, I'm not trying to build a generalized simulation. I will probably need to add collision detection along the way, but that won't be difficult and probably won't require extending the system to rigid body dynamics. I'll play around with it some more. Thanks for the ideas.
ewjordan
Posts: 26
Joined: Sat Jun 30, 2007 4:34 am

Re: Rope Physics in 2D

Post by ewjordan »

Well, good luck with it - if you need any help, let me know, I have used this method in games before: http://www.ewjordan.com/rgbDemo/ has a Java demo (applet) of a game I did this in, which is still unfinished. If you're on a 64 bit machine it might not run, it uses JOGL and I don't think I have the right binaries online. You can peek at the source if you'd like (there's a link), though it's not very intelligible...unfortunately, for this game I did end up doing essentially an entire physics engine using this type of stuff (this level doesn't have rigid bodies or anything in it, but others do), and it was a real pain in my butt to get anywhere close to right. I pretty much never did. But for the rope stuff it works extremely well, so...
Post Reply