Suppose it's about that time, again!
Today I decided to try my hand at drawing a decent physics-based rope. I didn't spend a whole lot of time on it, so the result is pretty surprising for the time investment. :)
Clicky for make big:
This example renders two objects (a blue circle and a yellow circle), a black rope, and the two "control points" (used to draw the curve for the rope). In a game, you wouldn't render the control points. They are here only for debugging purposes. You can click and drag the blue and yellow objects around, and watch how the rope stretches, bends, and twists with the physics.
It's far from perfect (you can break the simulation easily by forcing the objects through the floor, and the rope will happily stretch to infinity if you let it) but it's a good demonstration of how easy it is to do fairly complex things with Chipmunk, melonJS, and a little creativity.
How does it work?
The shapes (four circles) are attached with joints in a specific order, and rendered with a cubic
Bézier curve. The two object circles make up the end-points on the curve, and the two control circles are used for the control points. The blue circle (aka P0) is attached to the red circle (P1) with a slide joint that has a min length of 0, and a max length of 100. (I initially tried an even simpler pin joint, which has a fixed length, but that caused real badness when the control points collided with the ground, making the rope go in odd angles.) It's the same slide-joint-setup between the yellow circle (P3) and the green circle (P2).
With this configuration, the rope acts really unnatural, because the control points can (and often do) move in arbitrary and independent directions. My solution was attaching the control points with a damped spring joint. In the demo, I used a rest length of 0 on the spring, and set the strength and damping to 10. This makes the spring act a bit like a pneumatic pump; it takes a good bit of force to slowly pull or compress it, but it wants to compress completely.
The end result is fairly convincing. In fact, we'll probably use this rope-rendering technique almost verbatim in the final game! I'll be using it for the "attach" gameplay mechanic. Expect to see ropes attached between rocks, balloons, rubber balls, etc. It should be fun to play with!