May 20, 2016

How to achieve seamless infinite random terrain

I've made a prototype of an infinite terrain system for a project I'm working on. This is how it looks like:

The terrain is infinite and random, and I'm using Perlin Noise to generate it. If you are interested in making something similar then you should watch the beginning of this tutorial series: Procedural Landmass Generation.

To be able to distinct between sea, sand, land, and mountain, you have to discretize the continuous noise into squares because you can't have an endless amount of squares. So you will end up with something looking like this:

This is one so-called chunk, which the terrain is made up of. To generate an endless terrain you just add more chunks to coordinates around this square. But if you generate the noise at another position you will realize that the noise will not be the same.

In the image above I've moved the terrain left, and you can see that the bottom of the mountain now consists of 2 squares and not 3 as before the movement. Where did the last square go? The answer is that it's there but I've discretized the continuous noise at another position, so it now believe there's land where it used to be a mountain. This is how discretization looks like:

You discretize at the center of the square (the blue lines). But if you move the noise (the curve) then the noise in the same square may have changed. This is not a problem if you are generating terrain like the terrain in Minecraft where one square doesn't really depends on what's going on in the next square, so you will not notice strange seams. But if you're using an algorithm like Marching Squares to smooth the otherwise blocky terrain, then you will need to know what's going on in the next square, or you will end up with very ugly seams. To solve this mess you have to generate noise in a square with a border which is 1 longer than the original size of the chunk:

The values in the border have to be the same as if you had generated it in another of the surrounding squares. So first you are generating the noise for the center of the square, which is the same values as you generated before creating a border. Then in the right border (which is one of the borders you added), you generate noise as if you had moved the center of the square to the position of the square to the right. So in the right border you have to have the same noise as the noise in the left side (without a border) of the square to the right of the square you are generating noise for:

...and the opposite for the left side (and the top and bottom border):

And when you have generated the extra noise for the 4 borders, then you generate the mesh with the Marching Squares algorithm (or whatever algorithm you have). Finally you simply cut the extra border you added, and you will end up with a perfectly seamless endless terrain:

You can test the results here.

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.

May 2, 2016

How id software marketed Doom

I'm reading the book Masters of Doom: How Two Guys Created an Empire and Transformed Pop Culture, which is a biography on John Carmack and John Romero. They founded the company id Software where they created the popular games Wolfenstein, Doom, and Quake. All these games were popular, in fact so popular that the two Johns each could afford a few customized Ferraris. But why were the games so popular? One reason was that John and John (and the rest of the id Software team) were good at making games. The other reason was that they could market the games. A good game is not enough - people also have to buy it.

This is how they marketed Doom:

Word-of-mouth. First of all id Software realized that calling big papers and magazines wouldn't work. These magazines were not interested, in part because the game included violence not seen before. Instead they established a toll-free number to held orders and set up a deal with a fulfillment house that would package and send Doom to whoever ordered it. Id Software already had an audience who wanted to pre-order the game. Having a small initial audience is always good because they will talk about the game with their friends and so the game will market itself with the help of word-of-mouth.

Shareware. Id Software decided to self-publish Doom to make more money: they would make 85 cent for every dollar sold. To self-publish they decided to go shareware, so they would give away the first part of Doom for free and if a customer wanted more of the game the customer would have to pay for the rest of the game. This will increase the probability that a customer will test the game because the customer is not forced to pay for what's maybe a bad game.

Let someone else pay for the paid marketing. To distribute the shareware id Software contacted retail stores. This was before fast Internet speeds, so even though some customers downloaded the Doom for free from the Internet, all customers couldn't just download the game. Instead they had to buy it. At the time, retail stores were selling shareware and were forced to pay a royalty to the creator of the shareware because the store made money from the otherwise free shareware. But id software decided to not demand a royalty from the retail stores, telling them:
"Take Doom for nothing, keep the profit! My goal is distribution. Doom is going to be Wolfenstein on steroids, and I want it far and wide! I want you to stack Doom deep! In fact, I want you to do advertising for it too, because you're going to make money off it. So take this money that you might have given me in royalties and use it to advertise the fact that you're selling Doom."
So the retail stores would pay for all the paid marketing of the game and id Software wouldn't have to pay a single dollar for it. But id software decided to pay for one small ad in a gaming magazine - you can't get everything for free.