May 7, 2016

Making a realistic boat in Unity - Part 2

Let's say you want to make a boat float inside of your computer. There are multiple ways to make a boat float in a computer, but most of them are not realistic models. One example is that you could add a simple collider slightly above the water line, but it's still a cheat. The reason is that making an object float in a realistic way is super-difficult. When I was in engineering school I took a class in fluid dynamics and in it I learned that it can take several hours to just simulate a little bit of water around a boat. And that's too long time if you for example want to make a boat game.

But about a year ago, I found an article called Water interaction model for boats in video games. The article told how you can develop a computer model to make an object, like a boat, float in a realistic way by adding the real physics equations. This is how it works: All computer 3D models consists of triangles:


To make an object float you have to figure out which of the triangles are below the water line. If only a part of the triangle is below the water line, then you have to cut the triangle into pieces. It looks like this in Unity:


You can see that the model cuts the hull into two pieces: the yellow one which is the part of the hull that's above the water line, and the red one which is the part of the hull that's below the water line. You can also see that the model has modified the triangles that are only partly submerged by dividing them into new triangles.

When you know which triangles are below the water line, you can add the buoyancy force to make the boat float. But the buoyancy is not the only force acting on a boat. What you need is also the resistance forces. Luckily, the guy who wrote the first article published another article called Water interaction model for boats in video games: Part 2, describing the forces that you should add to make the boat behave more realistically in the water.

The forces you have to add include: viscous water resistance, pressure drag, and slamming force. But that's not enough, you also have to add the fifth force: air resistance, which is acting on the part of the boat that's above the water line. But since you now know which part of the boat is above the water line, then that force is easy to add. When you've added all five forces, the boat should behave in the water like this:


And if you don't want to figure out how to write the code yourself, I've written a tutorial on how to do it in Unity with C# code: Make a realistic boat in Unity. In the tutorial you will also learn how to make an endless ocean with waves.

No comments:

Post a Comment