2

Based on a request for help in the Pico-8 Discord, I ended up working on a rope/string/ribbon/chain/etc. simulation:

Cart #rope_sim_demo-0 | 2022-05-30 | Code ▽ | License: CC4-BY-NC-SA
2

The behavior of the rope changes substantially depending on the gravity and drag settings.

This sim tries to model the object as inelastically as possible, although it's far from perfect in that regard. The rope here has 128 segments, which is very difficult to handle without (a) instability, (b) excess springiness in the rope, or (c) tons of damping. So i ended up having to write a slightly clever constraint solver (multigrid for n log n instead of n^2 performance), and I chose a step schedule that uses almost 100% CPU at 60fps. But if you use fewer segments and tune the solver differently, going down to 16 segments or so and using a less conservative step schedule, like one suggested in the cart comments, you can get the CPU usage closer to 5% at 60fps. That might be more suitable for inclusion in a game.

The underlying technique is Position-Based Dynamics, in which the constraint solver works directly on positions, and then velocities are updated accordingly at the end of the timestep. The nice thing about this technique is that hard constraints can be really easy to add - just push your vertices to where you want them! The bad thing about this technique is that friction or other forces can be hard to model since velocities aren't really considered in the constraint solution step.

I've included a ground plane here to demonstrate how easy it is to get the rope to collide with objects - just project the rope vertices out of any objects it's in! (Assuming your steps are small enough that the rope is still near the surface, your objects tend to be pretty smooth, etc. etc.)

Still, this was fun to write and I think it's kind of fun to play around with.

There are still some shortcomings here: static friction with the ground is not modeled, rope self-collision isn't modeled either, the solver still allows for noticeable elasticity in the rope at high velocities, and the drag model is very simplistic and doesn't attempt to model internal forces in the rope.

The default parameters give you something that feels vaguely rope-like, for something that feels more like a light ribbon or streamer, try drag=0.05 and gravity=0.01.

P#112468 2022-05-30 04:18 ( Edited 2022-05-31 23:01)