- Home /
Simulating a Rubber Band
I'll admit that I already asked a similar question a few months ago, but I didn't get a response and I've been completely stuck on this since then. This is going to be really long, but I hope you'll find it an interesting problem and help give some idea of what to do. So here we go...
I'm creating a game where the central mechanic is launching rubber bands in a 2D plane to solve puzzles. (It is not top-down, so gravity IS a factor.) The rubber bands must hang from one or more pegs at a time, be able to be stretched tightly between multiple pegs, be affected by objects that collide with them, launch appropriately when held taut and released, cause objects to bounce off of them when they are held taut, and break when too much tension is applied.
Previously, I have tried to do this by attaching several objects (the rubber band's "vertices") to each other with SpringJoints and giving each vertex a mesh collider that changes frame by frame to adjust according to its neighboring vertices. This worked decently when the rubber band was unstretched, but as soon as any tension was applied via moving pegs, the band clipped through and fell off. It seems to me that the band needs to have some kind of mechanism to "lock" vertices onto a peg so that it is impossible to clip through. I've come up with what may be a solution, but I have no idea how to implement parts of it.
Give all pegs a mesh collider composed of, say, 25 vertices arranged in a circle.
When a single rubber band vertex collides with a peg, it does not immediately "lock" to the peg, but collides with it as Unity normally would make things collide.
However, when any vertex has BOTH neighboring vertices touching the peg, it will become "locked." (Detecting whether a vertex is touching the peg could be one problem. Unity seems to have no boolean return function like "isCollidingWith" to make this job easier. Would a private boolean variable that switches on and off with OnTriggerEnter and OnTriggerExit really be efficient and reliable?) When one or neither neighboring vertices are touching the peg, it will become unlocked again.
When a vertex is "locked," it will be able to move, but only on the axis represented by the slope of the part of the peg to which it is locked. Think of a one dimensional train on a track, where at any given moment the train can only move forward and backward on the track tangent to the section of the track it is on. The track here is the perimeter of the peg, given by its mesh collider. The vertex is still affected by physics forces, like gravity and the tension in the band, but it is still restricted to its frictionless "track." (This is the main problem; I don't even know how to begin trying to do this. I believe that it is possible to restrict movement on the x-axis or y-axis, but I don't know if it can be done on a user-defined axis. I am not sure how the band vertices will behave when constricted by both their SpringJoints and this track system.)
When the band is pulled taut, the vertices that are not on a peg will be able to stretch and the band will be unable to clip and fall off. (I don't even know how to make the entire rubber band launch appropriately when the peg is quickly released. Maybe I can worry about that later.)
If you've read all this, I really appreciate it. If you have another different and hopefully simpler/more reliable way to do this, I'd like to hear it. If anyone can figure out how to make an object stick to a path and still be affected by physics forces, or a good way to know whether neighboring vertices are colliding with the peg, I would be so grateful. Sorry that this is so long, but making any progress on my project depends on figuring out a solution. Thank you!
Your answer
Follow this Question
Related Questions
How to make a Spring in a 2D game in unity? 0 Answers
How do I connect a spring joint 2d to a rigidbody in script? 0 Answers
Spring Joint 2D: Can't slow down anchored object while at high frequency. 0 Answers
Are point effectors (2D) simulated when you use the physicsscene2d simulate function? 1 Answer
Drag rigid body based on touch in 2D 0 Answers