I’m making a 2D game, I want the fastest and the simplest possible implementation of rope/cable physics. I can sacrifice some accuracy, the physics itself is mostly there for animation/cosmetics. No colliders in it so far.
I did more-or-less standard Verlet integration with iterative distance constraints, and then added angular constraints and stiffness. I’m still wrestling with masses; Orbital mechanics was much easier, I swear. But nvm, I got it working, cleaned it up, and I’m more or less satisfied, it’s pretty decent.
Now I want to introduce interaction with the scene colliders. Such fun!
Ok, so how on Earth am I supposed to find a point of contact with Collider2D?
What I have tried so far
Verlet integration itself is fine, I can raycast there, but this doesn’t help me because the actual points relax iteratively, which is the main trick of this algorithm. So I’m supposed to resolve collisions after everything is done, not before. But once it’s finished, some points are already embedded inside the colliders. “Fine” I thought, “I can push them back onto the perimeter”. Nope, I can’t!
I can find the collider with OverlapPoint, and there is ClosestPoint both in Collider2D and Physics2D – but it doesn’t work! It works when the point is outside of the collider, but doesn’t work from the inside, which makes very little sense to me because it obviously works with the edges. Why wouldn’t it work with the edges, when it has all the info I don’t? Is this because of concave shapes?
In the end I’ve found a (moderately expensive, but worth-a-try) hack from the days when ClosestPoint wasn’t there at all, and that didn’t actually solve anything, though I’m not saying it’s impossible, I just lost my patience and the hack is ugly. At the moment, I’m just really curious, how is one supposed to work with colliders, if I’m supposed to explode their edges on my own and run my own nearest point computation, how’s that useful? I mean, I’m this close to also slap a quadtree, implement my own collider-like shapes and call it a day. Should I use only PolygonCollider2D?
Also, before anyone suggests it, I have no GameObject to use any kind of EdgeCollider. I mean, I will likely reorganize everything if that’s the only solution, I will have a GameObject eventually because I need to render this anyway, but for now it’s just points in space that I visualize with gizmos.
I was really thinking that the collider part would be the easiest. Sigh.
@MelvMay What would you recommend? How do we optimally and reliably interact with colliders in such a situation? I have very little experience with custom physics.