March 5, 2017

One of the greatest intellectual achievements of history

How are you making a rope in a computer game? The most common answer to the question is: you approximate the rope with springs. This may first sound a crazy idea because you don't want a rubber band but a rope and a rope is not bouncy. But the truth is that all materials can be approximated with springs. When the famous entrepreneur Elon Musk first wanted to learn how to design rockets, he read the book Structures: Or why things don't fall down. It says:
The idea that most materials and structures, not only machinery and bridges and buildings but also trees and animals and rocks and mountains and the world itself, behave very much like springs may seem simple enough - perhaps blindingly obvious - but, from [Hooke's] diary, it is clear that to get thus far cost Hooke great mental effort and many doubts. It is perhaps one of the greatest intellectual achievements of history.

So if you know how to make a rope, you will also know how to make cloth. To make cloth, you just add more springs in other directions. This is also how Pixar is simulating hair in their animated movies: by approximating hair with springs.

I've been prototyping a helicopter game. The helicopter has a winch so it can winch people up from a stormy sea: 


It took a while to figure out how to make a realistic rope in Unity with C# code, so I've summarized it in a tutorial: How to create a swinging rope tutorial in Unity. You will learn how to make two different ropes:
...and this is the difference between the results: 

Realistic rope

Simplified rope

You should use the more realistic version if you are making a rope with low mass and the rope can collide with the surroundings. But if you increase the rope's mass in the realistic version then you have to increase the spring constant if the rope is going to keep its original length. But now you will encounter something called numerical instabilities, and you will observe how the rope is going out of control and finally "explode." Explode means here that the numbers are growing exponentially until they get so large that the computer can't handle them anymore.

So if you are making a helicopter winch where the wire is made out of metal and you have to winch heavy people out of the sea, then you need to use the more simplified version. The idea behind the simplified version is that you use one of Unity's built-in Spring Joints. If you add more or less rope, then the spring constant and damping constant have to be recalculated, and then you just add the new values to the Spring Joint. To actually add more or less rope you just change the Spring Joint's max and min distance. 

But you also need to make a curvy rope - the rope shouldn't be a straight line from the start of the winch to the basket. Now you have two alternatives:
  1. Use the ideas from the more realistic rope and add it between the start of the winch and the basket. This rope can have "fake" values to avoid numerical instabilities, while still looking as a real metal wire. 
  2. Use a Bezier curve and change the control points based on the rotation of the basket and the rotation of the helicopter. This will look like a real rope, will be fast to generate, but the most important difference is that you will never encounter numerical instabilities when generating a Bezier curve. So this solution is more robust while still looking like a real rope would look like. If you lower the altitude of the helicopter, you will get the characteristic shape of a rope falling down - the rope will bend like a real rope would:

No comments:

Post a Comment