Unity Rope Simulation Using Distance Constraints
Cool Demo First:
And download link for the unity package: http://test.mclimatiano.com/rope/RopePhysics.unitypackage
Please note that the textures are NOT included in the demo scene as I am not allowed to redistribute them.
About The Implementation
I’m not going to delve into the specifics of the implementation because the source is available, and there are already two excellent sources linked below – instead I’ll just give some short footnotes:
A friend asked for help getting a rope simulation working.
If you played with Unity’s physics system, you probably encountered insane instabilities when it comes to constraints – to the point where simulating a piece of rope seems impossible.
Since the only two requirements were for a piece of rope that would behave physically pleasing while being able to be dragged around, there was less effort involved in rolling a custom solution than trying to convince PhysX to react sanely to multiple distance constraints chained together.
Before you run off re-implementing the entire physics functionality of Unity, lets highlight some of the issues with rolling out custom solutions:
- It requires additional work, as opposed to something that already exists
- There is no guarantee your solution will behave any better than the existing one
- Enabling interactions between Unity’s physics simulation and a custom physics simulation requires a HUGE amount of effort
So in short, if you need something very isolated and specialized going with your own solution will give much finer grain control over the results – but if you want a solution that integrates with the built in physics of Unity, I would think carefully before running off to implement custom solutions.
Lastly, I want to go over some insights I had while implementing this:
- Spring damper systems make questionable rope – this was the initial attempt I had at solving this, and at first seemed to be the most obvious one. The biggest issue I encountered with this method was that I was unable to achieve a rope that is stiff enough under extreme conditions. As soon as forces started piling up, the rope would either become too springy or explode.
- You can cheat with distance constraints – I learned his after stumbling on the tutorial by Jared Counts. this is cheating because there is nothing remotely physical accurate about this method – instead of applying forces to satisfy the constraints, you can simply move the points around to satisfy constraints
- Verlet integration has its uses – for a simulation that looks believable there needs to be some sense of momentum. When solving constraints by simply moving particles around, there is no momentum to that motion. Verlet integration solves this by inferring the particle’s velocity from it’s translation.
- Cheating with verlet integration helps – again, something I picked from Jared Counts – without any form of dampening on the velocity, using verlet integration can become very unstable very fast. The proper way to handle this would probably be to introduce a source of drag force – the simple solution is to simply dampen the velocity in the integration step by a constant.
- Realistic values are not fun – this is usually true for game physics, but it amplified by all the cheats made along the way. For example, the artificial velocity dampening and the rigid position constraints resulted in a very weak force pulling the rope downwards with a gravity of 9.8m/s2, simply amping it up by a couple orders of magnitude seemed to solve the problem.
If you want a more in depth overview on the subject please check out the links below.