- Home /
Custom 2D physics of a rope (top-down view)
I have been working on a custom 2D physics for a rope lately which I seem cannot realize due to lack of needed math. It is something that turned out quite more hardcore for me than I thought it would at the start.
I tried PhysX (HingeJoint2D) briefly, and I do not see it working any way with what I planned for the behavior. I would also like to avoid using PhysX completely for this rather basic game.
In short, the scene:
This should be a really basic 2D game without any PhysX used. Rope behavior will work completely in 2D. No gravity needed as rope always laying on a flat surface (in XY axes) and camera looks at it right from the top (on Z axis, a regular 2D unity setup). This is a top-down view. User can tap/click anywhere on screen to drag the rope on a flat surface. Dragging is always occurred on the first particle of the rope (user can’t drag any other part of the rope).
Rope properties:
Swinging. Rope can swing if treated accordingly by user. If user suddenly stops the rope while still holding the tap, so the first particle can’t move, the rest of the rope collapses (tap, quick slide and hold maneuver). If user releases the tap right before a very quick slide of a finger (tap, quick slide and release maneuver), the rope is “thrown” in the direction of the slide, while the body and the end of the rope might not follow directly in the direction of the slide due to their initial curved state. In case of tap, quick slide, brief hold and release maneuver the rope’s first particle would not be thrown, but the rest of the body will push it forward, while rope collapses.
Probably most important property to implement: Smoothness. Smoothness would control how quickly an initially curved rope is straightened when dragged constantly in one direction. Smoothness property equal to 1F would straighten the rope as fast as possible (imagine a golden chain or bracelet initially curved on a glass surface, then you pull it and it gets straightened quite quickly). Smoothness property equal to 0F would not straighten the rope at all. Every particle would follow whatever path the users defined while dragging the first particle of the rope. Not sure what to imagine here to help you understand it, maybe an eel in the water, idk. When smoothness is somewhere in between 0F and 1F the resulting behavior is interpolated accordingly.
Swinging behavior is strongly dependent on smoothness. If smoothness equals 0 swinging is not possible, without regard whatever forces user applies to it, as rope can’t change it’s path (defined by user via dragging) while smoothness is 0.
Most importantly, this smoothness behavior is not actually a property of the rope, but a property of the surface on which this rope is laying. So there can be situations where part of the rope is laying within the area of 1F smoothness (where rope can swing and straighten itself while user is dragging the rope, imagine this area is actually an ice), while the other part of the rope is located within 0F smoothness area, where it can’t swing or straighten itself (imagine this area is some kind of swamp, or oil). In this situation both parts of the rope should behave in different ways but still remain a single rope. Situation when three or four parts of the same rope are located in areas with different smoothness property are also possible as user just can keep zig-zagging between two or more areas continuously. The rope should keep behaving accordingly.
No particle collision. Rope can cross itself anyhow anytime.
No limits on particle angles. Rope can turn 180 degrees at anytime.
So that makes basically two behaviors to implement for a rope, smoothness and swinging (which is dependent on smoothness). And smoothness is a property of a surface, not a rope.
Studying:
I found several resources that I think might be useful.
http://nehe.gamedev.net/tutorial/rope_physics/17006/ This is fully fledged physics model of a 3D rope, it can swing, it has realistic smoothness property of a rope, it can wave and everything. I think I could rewrite it for 2D, remove several unneeded properties like gravity, friction, springiness. As I don’t need that precise simulation, this is a basic 2D mobile game after all. Still, this code lacks variable smoothness factor, as it’s too specific of a requirement, and I’m not sure I could modify the code to work that bit in.
http://processing.org/examples/follow3.html This is rather basic implementation of a rope without any physics in just 35 lines of code. I like complete absence of a physics model, but It doesn't swing, however, it still has nice smoothness similar to a rope. If it was possible to add smoothness to this rope as a parameter (to a surface actually) and if it could swing in described conditions with the help of very basic physics model this would be perfect.
I’m not asking to implement this rope’s behavior, as it would be probably too much of a work, so I shouldn't be asking that. I’m asking for an advice or method I should be reading and diving into that would let me build such a behavior. As currently I have two different approaches for implementation: for swings I could use custom physics model and for smoothness = 0 condition I could use a path that particles would follow. But I have no idea how to mix these two different approaches and how to interpolate the smoothness parameter. And that situation when rope is on several different surfaces (with different smoothness property) completely knocks me out.
Thanks for any help!
Is it worth asking the same question on stackoverflow? It's more of a general question about math and algorithms rather than Unity3D.
Answer by NoseKills · Sep 18, 2014 at 09:39 PM
The ages old Verlet paper from the maker of Hitman's physics is worth a look. It's simple and performant and easy to tweak by adding your own properties to the verlets.
I did this (otherwise rather crappy game :) ) ages ago with the verlet system and it's actually not so far from what you might be looking to implement.
I added some extra constraints to make the tail a bit more stiff...
Thank you for the answer! Played your game and liked the way tail behaved.
I heard about Verlet physics before, it's also used in few of my favorite games, such as Hammerfight and Spewer, and probably in dozens more games that I played.
However, I always sneaked past this technique in my projects for no apparent reason (thinking perhaps Euler integrator is good for everything). I feel, it's time to nail that topic down once and for all. The link to paper you gave me misses the images. I see it like this: http://i.imgur.com/TWBwI0J.png Let me know if it's the same for you. I might look for other versions of this document, if I feel lost with this version.
Thanks for the help!
Thanks :)
Yeah the images are missing. I can't seem to find a working version of it anywhere anymore.
Also, i didn't follow all the way through with the paper. The most useful bits i took from it were the optimizations they made (rid of SQRT) and general principle of estimating speed with last and current position + the whole constraints & adding forces principles.
Yeah, there is no reason to use SQRT. I'm also skipping that last and expensive bit of the formula, whenever I need to calculate a lot of distances every frame for example.
Paper is easy so far. I'm feeling like it would be very good for me to grasp everything from it.
Thank you for your help! Cheers!