April 28, 2017

Short April Updates - from forest fires to foam skirts

0 comments

Forest Fire

Many years ago I took a class in Differential Equations. One of the assignments was to simulate the evolution of a forest fire and the result was displayed on a graph. The problem was that you only got the end result and couldn't see how the fire evolved to get to the final result. A few years later I learned Unity, and decided to see if I could simulate a forest fire while at the same time see how it evolved. And it turned out I could.
The forest fire worked, but the visuals didn't. The first problem I had was smoke. A forest fire is large and you need a lot of smoke to cover the entire area - and that is not good from a performance perspective, and the resulting simulation was running in like 3 fps (30 fps is the goal). Luckily, both Unity and I have improved our skills.
The first thing I improved was to replace the fire particles with a glowing-ground-shader. Most of the time, you can't see the fire because of all the smoke, so by having just flat fire instead of fire particles makes no difference. I also decided to use animated smoke particles from Unity's free library of particles: Free VFX Image Sequences & Flipbooks. By using animated particles, the smoke will look more thick and you can use less particles to achieve the same result.
The second problem I had when I created the original forest fire was similar to the smoke problem: a lot of trees will decrease the performance of the simulation. To solve that problem when I created the original version was to combine all trees into a single mesh. This is also a complicated solution because it will make it much more difficult to remove the trees as they burn down. Luckily I could now replace all that code with a single shader called GPU instancing, which is something Unity recently added. If you add one of those materials to objects with the same mesh, Unity will automatically combine them, and the performance will improve. Anyway, this was the result:


Shaders

As said in the forest fire update I added an animated glow shader to simulate the fire. The reason was that I've also improved my shader skills. The first shader skill I learned was Interior Mapping, which is a technique used to simulate floors in buildings. The second technique was Parallax Mapping, which is a technique not that different from interior mapping, but you can simulate more "organic" deformations, such as this:


Tesla Simulator

I few years ago I made a Tesla Simulator to market a book I wrote about a guy called Elon Musk. It was available to the public through Unity's webplayer, which you could run directly in a browser like Chrome or Firefox. The problem now is that most browsers have stopped supporting the webplayer, so I had to make an offline version. At the same time I've cleaned up a lot of silly coding mistakes I made, because the Tesla Simulator was the first project I made in Unity - and I've apparently learned a lot since then.

"Make a boat" tutorial

One of my most popular Unity tutorial I've made is Make a realistic boat in Unity. What the tutorial didn't have was how to add foam to the boat, and I've thought a lot about what the best way is to add foam to a boat. Then I found this tweet:


...which was a cool idea on how to add foam, so I decided to implement it: Add foam. To make that work, I had to learn a technique called Convex Hull, which I wrote a separate tutorial on how to implement: Find the convex hull of random points.

April 24, 2017

Aircraft Carrier Simulator - or how to make a game in 48 hours

0 comments
Ludum Dare was on this weekend. It's a competition where you are supposed to make a game during 48 hours, and you may win fame and glory if you make a good game, so no money is involved. But people have come up with good ideas by participating in the competition, and they have later modified the game, sold it, and thus made money from it. One example is the game Broforce.

There are two different competitions going on at the same time: Jam and Compo. If you are participating in the Compo, you have to make everything yourself, including the textures, models, and sounds. Not a single piece can be found by googling. This is a however a gray-zone. Are you allowed to steal an engine sound from a YouTube video, or do you have to record it yourself? While making my game, I watched a Twitch streamer who was also participating in the competition, and he had a really hard time making sounds for his game. You could see on the stream how he squeezed a coca cola can to make a specific sound, so you have to be a little creative. I usually get my sounds from this site: Bfxr, which is a site you officially are allowed to use. I usually click a million times on the "generate-random-sound-button" until I find something useful and then I modify the sound in Audacity.

Each Ludum Dare competition has a theme. The theme this time was "A small world," so I decided to make an Aircraft Carrier Simulator. You can say that an aircraft carrier is like a small world because you can find everything you need on the ship. A few years ago I made an airport simulator for the same competition. It failed miserably, and I have hopefully improved since then.

This was my 9th Ludum Dare competition. One thing I have learned is that sleep is important. The competition begins at 3 am local time and ends 48 hours later. I have learned that sleeping until like 7 am, work the entire day until like 10 pm, and then go do something else like watching a streamer while thinking about the game until 12 pm which is bedtime, and then repeat the process the next day is the way to go. Anyway, this was the result:





The game idea is that you are in command of this aircraft carrier's aircraft operations. It's your responsibility to steer the aircraft so they can refuel, land, and launch from the catapult so the enemy is not coming closer than zero meters from the aircraft carrier. If the enemy get any closer, you fail! The more aircraft you have in the air, the more difficult it will be for the enemy to come closer. If you think it looks and sounds interesting, you can test the game (and download the source file) here: Aircraft Carrier Simulator.

March 10, 2017

Evolution of helicopters in the Vietnam War

0 comments
The Vietnam War was the first conflict where helicopters played an important role. While the future Cold War battlefield in Europe would have consisted of great tank battles, the jungles of Vietnam prevented tanks from moving efficiently. There were American tanks in the Vietnam War, but their main purpose was to keep roads clear from the enemy by using a tactic called Thunder Run, which is the same tactic that years later would be used by American tanks to capture Baghdad.

Tanks in the Vietnam War were always staying close to roads because a tank in a jungle is a big target for the enemy. But the tanks would clear jungle close to roads if needed. There's an example of a helicopter that crashed in the jungle. To rescue it, tanks on a nearby road were ordered to move through the jungle and secure the crash site. And if the enemy had built a bunker, a tank would drive up to the bunker, stick the barrel into the bunker, and fire. But again, these bunkers had to be close to roads or the tank would simply not be able to get there.    

With tanks out of the picture, the US came up with the idea to use helicopters. In the Korean War, the main purpose of the helicopter was to transport wounded to the rear two at a time. But when the Vietnam War happened, technological improvements had transformed the helicopter to a fighting machine, so they were no longer just transportation vehicles. A guy called Jim Gavin wrote an article called "Cavalry - And I don't mean horses" where his vision was that bigger, faster helicopters could carry infantry into the battle and make it a three-dimensional nightmare for an enemy commander. US decided to add helicopter transported infantry to it weapons of choice, and the recently developed Bell UH-1 Iroquois (nicknamed Huey) was chosen as the main helicopter for this unit:

The Huey

The helicopter transported infantry was sent to Vietnam. But if you've seen the movie We Were Soldiers, you know that helicopter transported infantry wasn't a three-dimensional nightmare for an enemy commander. If the enemy could cut off the few landing zones available in the Vietnamese jungle, where the helicopters could land, the infantry would be on its own. Each Huey has two machine guns operated by relatively unprotected soldiers, so the damage they could make was limited. The solders on the ground could get support in the form of artillery and air support from winged aircraft, but this support wasn't accurate enough to rescue the infantry in all situations. So something else was needed.

The Huey was a great helicopter, so someone realized that it was possible to take the basic engine and rotor, but add a new body to get a helicopter that could support the infantry. So the Bell AH-1 Cobra was quickly developed and would arrive to the Vietnam War.

The Cobra

The Cobra could kill the enemy but it couldn't find it. So someone came up with idea idea that you could combine the Cobra with the smaller, but much more agile helicopter Hughes OH-6 Cayuse (nicknamed Loach). Now you would get a so-called hunter-killer team, where the Loach was finding the enemy and the Cobra was killing it. The Loach could seat five: two pilots and three passengers. But in the Vietnam War, the Loach had either two pilots and one gunner, or one pilot, one gunner, and one mini-gun fired by the pilot. Why not four in the helicopter and a mini-gun? Because that would make it too heavy!

The Loach

One of the pilots who flew a Loach as part of a hunter-killer team in the Vietnam War was Hugh Mills. He wrote about his experiences in the book Low Level Hell - A scout pilot in the Big Red One. A helicopter scout in the Vietnam War had the following jobs:
  1. Find enemy base camps, fighting positions, supply caches, trails, and other signs of enemy movements.
  2. Assess damage made by high-altitude bombers, such as the B-52.
  3. Find landing zones for the infantry carried by the Huey helicopters.
  4. Help the infantry and tanks on the ground by giving them information, such as which terrain is the most advantageous, and if the are moving in the correct direction.

If you are a scout pilot (the hunter), the easiest way to find out where the enemy is hiding is to fly as close as possible to where you think the enemy is and hope that the enemy will fire at you. If you are fired at, then you drop smoke, so the Cobra (the killer) know where the enemy is and can fire the rockets. The result of this suicide tactic was that Mills was shot down no less than sixteen times and wounded three times.
Though I was getting shot at almost every day, I never got used to it. But getting shot at was usually the way a scout found the enemy, and finding the enemy was our basic job.

It might sound strange to sacrifice one helicopter, while the other helicopter is waiting for the scout the be shot at. But the pilot in a Loach has a better view of the surroundings, and the Loach is also a smaller helicopter making it more difficult to hit, so the tactic makes sense. To help the Loach, the Cobra was always staying within sight of it and did everything the pilot in the Loach didn't have time to do. The Loach pilot had to fly the helicopter while having his eyes constantly focused on the ground, so the Cobra read the map and transmitted radio messages.
The good scout pilot never stops talking to his gun [the Cobra] from the moment he goes down out of altitude until he comes back up again. It not only keeps the Cobra happy and informed, but it tends to keep your own guts stabilized when you're down low working and, at any instant, could catch a bellyful of AK-47 fire.

March 5, 2017

One of the greatest intellectual achievements of history

0 comments
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:

February 26, 2017

The Complete Beginner's Guide to Helicopters

0 comments
While aircraft played an important role in both the first and second World War, you can't remember any helicopters. It's true that Germany had a reconnaissance helicopter Flettner FI 282, nicknamed the Hummingbird, which was introduced in 1942 and is considered the world's first series production helicopter. But only 24 were built and they didn't play an equally important role as the famous Spitfire or the Messerschmitt.

Why did it take so long before the world could fly around in helicopters? Isn't a helicopter as easy to design and fly as an aircraft? The answer is no! Even today it's more dangerous to be a helicopter pilot than it is to fly a normal plane: the helicopter accident rate is 30 percent higher than the accident rate for fixed-wing aircraft.

Helicopter 101
To understand why a helicopter is more complicated than an aircraft, you have to make sure you understand how an helicopter is working. Basically, a helicopter pilot has to use three controls: collective, cyclic, and pedals - and the pilot has to use all three to stabilize the helicopter.


The collective is always located to the left of the pilot and is looking like a car's handbrake. To move the helicopter up and down, the helicopter pilot is moving the collective up and down. When the collective is moved, the angle of the rotor blades are changing. A helicopter's rotor blade are working in the same way as a wing as they have the characteristic wing profile:


A wing profile is producing lift. If the angle of the wing is changing, the wing will produce more or less lift, which will move the helicopter up or down. So a helicopter is not moving up or down by changing how fast the rotor blades are rotating because that speed is always the same. As the angle is changing, the helicopter needs more or less power to rotate the blades with the same speed. In some helicopters, the pilot also has to control this power from the engine to the rotor blades, and this control is also located on the collective. If the lift from the rotor blades is higher than the mass of the helicopter, the helicopter will hover:


As the helicopter pilot is controlling the up/down movement of the helicopter with the collective, and as the power to the rotor is changing, the rotor will also produce more or less torque. To stop the helicopter from rotating because of this torque, a helicopter has a tail rotor. Some helicopters have two rotor blades, either on the same axis or after one another, to stop the torque, but most helicopters have a tail rotor. To control this torque, the pilot has two pedals, which will control the angle of the rotor blades attached to the tail rotor. In the same way as the angle of the main rotor blades produces more or less lift, if the angle of the tail rotor blades change, the helicopter will rotate around its axis.

But a helicopter can also fly in several directions, such as forward, and not just hover. If you look at a helicopter, you see something that looks like jet engines:


You might think these engines produce forward propulsion, but they are only there to rotate the helicopter blades. To fly in a certain direction, the helicopter pilot has a joystick between the legs called the cyclic. The cyclic will again control the angle of the main rotor blades, but this angle will change depending on where the rotor blades are in relation to the helicopter's body. So a helicopter pilot can control the angle of the main rotor blades by combining the collective together with the cyclic. To make this work, most helicopters have an ingenious construction called swashplate, located on the main rotor axis. This is how the swashplate is working:



From the video you can see that the main rotor is rotating forward. This will produce both a lift force and a forward force, which will make the helicopter move forward. But it will also produce a torque because the lift force is not distributed equally across the circle formed by the rotating blades, so the helicopter itself will get an angle, so it begins to lean forward:


Leaning forward is fine at certain low speeds, but at higher speeds, you want to avoid this angle. To make a helicopter fly forward without an angle, most helicopter have a horizontal stabilizer which will help the helicopter to fly straight forward at certain speeds.


Why is it difficult to fly a helicopter?
A helicopter pilot is controlling the helicopter by using the collective, cyclic, and pedals. So why is it more difficult to fly a helicopter than it is to fly an aircraft? The problem is the forces produced by the different helicopter blades. As the main rotor is rotating in different directions, the wind from the rotor blades will affect the helicopter, so to keep the helicopter in a hover position, the pilot has to make adjustments to the collective, cyclic, and pedals. It's said that a helicopter pilot has to develop a certain "feel" for the helicopter. Small deviations can be felt and seen, so corrections should be made before the helicopter actually moves. And even when flying the same model of helicopter, wind, temperature, humidity, weight, and equipment make it difficult to predict just how the helicopter will perform.

This might be confusing, so maybe a real helicopter pilot can explain it. Someone who really knows how it was to learn how to fly a helicopter is Robert Mason, who was a helicopter pilot in the Vietnam War. He has written about his experiences in the book Chickenhawk, and it includes a chapter on how he first learned to fly a helicopter:
"Next thing to do, now that you've got the cyclic down, is to let you try all the controls at once. Think you're up to that kid?"
"Yes, sir."
"Okay, you got it."
"I got it." The cyclic tugged, the collective pushed, and the pedals slapped my feet, but for a brief moment I was in complete control. I was three feet off the ground hovering in a real helicopter. A grin was forming on my sweaty face. Whoops. The illusion of control ended abruptly. As I concentrated on keeping us over one spot with the cyclic, we climbed. When I pushed the collective back down to correct, I noticed we were drifting backward, fast. I corrected by pushing forward. Now I noticed we were facing ninety degrees away from where we started. I corrected with the pedals. Each control fought me independently. I forgot about having to push the left pedal when I raised the collective. I forgot the cyclic-control lag. We whirled and grumbled in a variety of confusing directions, attitues and altitudes all at once. There were absolutely too many things to control. The IP [instructor], brave man that he was, let the ship lurch and roar and spin all over that field while I pushed the pedals, pumped the collective, and swept the cyclic around, with little effect. I felt like I had a handful of severed reins and a runaway team of horses heading for a cliff. I could not keep the machine anywhere near where I wanted it.