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. 

February 23, 2017

How to become the MacGyver of surviving terrorist attacks

0 comments
It's true you're more likely to be fatally crushed by furniture than killed by a terrorist, but preparing for unexpected events is always a good idea. So how can you prepare for a terrorist attack? The government tells you to:
  • Be prepared by observing the emergency exits
  • React quickly and not freeze from panic
  • Make yourself a small target by hiding
  • And if everything else fails you have to fight for your life

But I don't think reading a 4-point bullet list is enough. The list says you shouldn't freeze from panic, but who's voluntarily freezing from panic? Isn't that an involuntarily decision made by your brain? Anyway, last year I read the book The Unthinkable - who survives when disaster strikes - and why by Amanda Ripley. The reason I read it was because a guy on a radio show talked about zombie attacks and he said the book would really help you prepare for the zombies. I'm not a big believer in zombie attacks, but the book doesn't mention the word zombie, and it was actually one of the best books I read during the year. 

The Unthinkable mentions several disasters, including 9/11, the sinking of the Estonia ferry, and a huge explosion in Canada. It's true you can't survive all disasters. If you were caught in one of the floors above the floors where the aircraft hit the Twin Towers, then this book will not help you. But you can maximize the probability to survive if you know what's going on in your brain when you are in the middle of a disaster.

In an emergency situation, your brain will not always help you survive. It can begin to make stupid decisions:
  • You will not accept this is happening to you. It's common to laugh, which in these situations is a form of denial: "This can't be happening to me!" Then you might get angry and then you may become quite, so mood swings are normal. But you will most likely not panic. Most people who die in disasters die from doing nothing at all and not from running around like a wet hen. In hindsight, they might think they panicked, but they only felt afraid and that is not the same thing as panic. So what you have do is to go from the denial phase to the do-something phase while remaining positive. Studies have shown that people perform better under stress if they think they can handle the situation.   
  • You will begin to gather stuff you want to take with you but you will not need. 
  • Your memory and senses will be switched on and off. This is happening at certain key points, so you may lose your vision or hearing. Remember the soldier in the reality-based Band of Brothers series who became blind during combat, but would later regain his vision when things calmed down.
  • You will forget your obligations. One person who survived 9/11 was actually in charge of evacuating the floor, but the person forgot all about it only remembered it several weeks later.
  • You will observe what other people around you are doing. This is the reason why you won't react quickly because you don't always know if you are in a terrorist attack. For example, it's easy to confuse firing guns with fireworks. If you begin to flee and it turns out to just be fireworks, then you will be laughed at and perhaps your fleeing will end up on YouTube. So you will observe what other people around you are doing, and if no-one else is doing something then neither will you. In the past, people have died because they followed the crowd, while ignoring closer exits. 
  • Your brain will freeze. The reason is you have never before been in this situation so your brain freeze while trying to figure out what to do. One person in the book who was under fire began thinking about the The Naked and the Dead, a book about soldiers in combat, because his brain was searching though its archive of experiences. This also happened when Estonia sank. One of the survivors could see how people on the top deck just sat down in chairs and where doing nothing at all instead of trying to reach the life boats, which were very close. 
  • You will come up with stupid ideas. One woman almost died from a fire because the person outside didn't want to crush the window out of fear of getting in trouble for crushing a nice window. 
  • You will follow the wrong leaders. One of the companies with most 9/11 survivors was a company with a security chief who made sure everyone knew where the emergency stairs were, and when the attacks happened everyone listened to him. But following leaders can also be the wrong decision. Firemen have seen people moving along their fire trucks towards the fire, instead of moving away from the fire because firemen are seen as leaders in a disaster.       

But your brain will also make smart decisions. Opposite from what you may first think, you will be more kind than you would have been on a normal day and you will behave orderly. During the evacuation of the towers during 9/11, people were calm and didn't run over each other on the stars on the way down. Your brain can also toughen up itself. Research shows that special forces soldiers tend to have more traumatic backgrounds, so their brains have learned something from the trauma, which makes them more capable of handling stress.   
   
So what can you do? How can you help you brain not to freeze? Remember that your brain is freezing because it doesn't have any experience from the situation. So the solution is to prepare to get experience. One police office from the book explained how his subconscious mind made better decisions than his conscious mind, so he always trusted his subconscious mind when the bullets were flying above his head. When I was in the army I recall how we had to disassemble and assemble our rifles hundreds of times until the process became unconscious. I've luckily never had to perform this process while the bullets are flying, but if you listen to the book it wasn't a waste of time.

But preparing for disasters is easier said than done. The author of the book suggests we should build amusement parks where you can prepare for disasters. Someone in Britain had the idea to install aircraft cabin simulators in airports, so passengers who are waiting for their flight could practice open emergency doors, but they idea went nowhere...

The government bullet list says you should prepare by observing where the emergency exits are. You now know why this is helpful: it will give your brain experience and it will not freeze if something is happening. This is also true when flying. Disaster experts always read the "useless" safety briefing cards because they know each aircraft is different and they know their brain will use this information if a disaster happens. They also test to exit the hotels they are visiting by using the stairs because they don't trust that someone will show them how to escape. This is important: everyone has to know what to do if a disaster happens. Remember the person who survived 9/11 and was in charge of evacuating the floor, but the person forgot all about it? So if you just have key employees in charge of the evacuation, it is not good enough.

You need to forget your experience of what has happened before. This might sound contrary to what I said above: you need experience to survive a disaster. But experience from disasters can also harm you. You may believe that many of those who died in Hurricane Katrina were poor and couldn't escape the city. But the truth is that many of those who died because they had survived previous hurricanes and thought they also would survive Katrina. So many of those who died were not poor, but old. There's also a story about a tsunami that killed people because they had locked themselves in their house because they thought the sound of the tsunami was gun shots because that's what their experience told them.  

You have to learn not only that you should do something, but also why you should do it. For example, we have all listened to flight attendants explaining what to do if the oxygen masks drop down from the ceiling of the plane. They always say you should put on your own mask before helping others. But why is it important to always put on your own mask before helping others? The answer is that in the event of a rapid decompression, your have not more than 15 seconds before you pass out. Now you understand why you have to prioritize putting on your own mask before helping others!

You have to learn how to breathe. Both FBI agents and special forces are taught how to breathe correctly to master their fear. This is how you should do it: breathe in for four counts, hold for four counts, breathe out for for counts, hold for four counts, and repeat all over again. This will help you from hyperventilating or panicking. One police office was playing a tape recording of a police siren while breathing to make it an automatic response to the siren.