Learning To Fly
Now is the time to make our special effects fly. We will create one of those games that make people admire. A game that will take us to the sky, a Flight Simulator.
Flight Simulators have always been among the most impressive and processing intensive games. A simple Flight Simulator, with today's standards, was the main demo for graphics workstations in 1980's. Nowadays it is relatively easy to create one, and this is what we will do in this article.
We will begin our journey in the Flight Simulator territory with the physics involved in flight. Correct physics modeling is essential in a game like this. It doesn't matter if your graphics are great, the scenery realistic and the map precise. If your airplane feels strange you will be unhappy with your work.
Flight physics is a very big chapter in physics. It mainly involves aerodynamics and rigid body mechanics. Aerodynamics help us calculate the forces that act upon the airplane. These forces with gravity are the ones that make the airplane move and stay in the air. Rigid body mechanics on the other hand describe how the airplane responds to these forces, how fast it turns, rolls etc.
In this article I will try to present the basic principles of the physics involved in modeling a flying airplane. Keep in mind that this is not a complete theoretical approach to flight. Only the basic stuff to help you understand the flight simulator we will develop.
Let's start by describing the basic parts of the airplane.
Fuselage: The body of the airplane
Wings: The generally flat structures that generate the lift which make the airplane fly
Tail: The back of the airplane which consists of a vertical and a horizontal section. Its main role is to stabilize the airplane.
Ailerons: Movable areas on the outer part of the wings, used to make the airplane roll.
Flaps: Movable areas at the inner part of the wings, used to increase lift at low speeds and drag.
Elevators: Movable areas of the horizontal tail section used to control pitch.
Rudder: Movable area of the vertical tail
Forces acting on an airplane
The main force acting on any object is of course GRAVITY. In order to fly we must generate an equal and opposite force we call Lift. Lift is generated by the wings as the airplane moves forward. The move is powered by the force we call Thrust and because of aerodynamic resistance we encounter a force resisting the move that we call Drag.
The preexisting force is Gravity and the human controlled force is Thrust. Lift and Drag are the result of the movement of the airplane.
What makes the airplane move ?
We start our journey with the main force we generate on the airplane. Thrust. Thrust is the source of energy the airplanes need in order to generate lift and finally fly. Gravity is there as one of the fundamental forces of nature. But if we want to generate lift we must make the airplane move. Thrust does just that.
Well then, what is thrust and where does it come from ?
Thrust is the force generated by the engine and pushes the airplane forward. There are several ways to convert mechanical energy generated by the engine to thrust. The oldest one is a propeller attached to the drive shaft of the engine, to the most modern jet engines.
In the image we can see the three basic types of thrust generators as they are built for larger airplanes that require more power. For smaller airplanes the propeller is powered by smaller reciprocating engines instead of turbines.
The first engine type is turboprop, the second is turbofan and the third is the jet engine. Getting into the working details of these engines if far beyond the scope of this article.
Lift and Drag, how are they generated ?
We know that lift is a result of the movement of the airplane due to the thrust. Let's see in more detail what happens when the airplane moves.
Any moving object within a fluid such as air generates two forces. One is opposite to the direction of the movement, the drag. This is proportional to the size, and the shape of the object, the viscosity of the fluid and the speed of the object squared. The other one is perpendicular to the drag and is called lift. Its magnitude and orientation is determined by the shape and orientation of the main surfaces of the airplane, mainly its wings. A badly designed airplane might generate negative lift. Negative lift is a designer's target in a sports car.
And how is lift generated? The shape of the wings makes the air that travels above the wing move faster than the air that travels underneath. This difference in air speed generates a difference in the pressure of the air at the two sides and the result is Lift. Another key factor is the so called "angle of attack". This is the angle between the longitudinal axis of the wing with the direction of the airplane.
Things are not so simple though. The more lift we try to generate modifying the shape of the wing the more drag is generated. If we increase the angle of attack very much the flow of the air on the top of the wing detaches of the wing and becomes turbulent. This increases its pressure and minimizes lift.
So we need a mechanism that will alter the shape of the wing and generate a lot of lift at low speeds to give us fast take off and low speed approach for landing. This mechanism is the flaps. The flaps extend and move downwards upon pilot's decision. Extending the wings this way generates more lift required at low speeds to keep the aircraft in the air. As the air speed increases the pilot retracts the flaps since the wing can generated the required lift without the extra help.
Movements of the airplane
In order to drive the airplane to the desired place, the pilot can use the controls and perform one of the three movements seen in the image.
Roll: Rotation around its longitudinal axis controlled by the ailerons. In order to generate Roll the ailerons move in opposite directions, one up and one down.
Pitch: Rotation around its lateral axis controlled by the elevators. Moving the elevators up make the tail go down and thus the front go up. Moving them down has the opposite result.
Yaw: Rotation around its vertical axis controlled by the rudder. The rudder behave pretty much like the elevators only the rotation occurs around the vertical axis.
Rolling to the left and pulling the pitch up generates a combined movement equivalent to a left turn.
And how does the airplane respond to these movements ? To find how it responds we must first try to analyze how the control surfaces influence the airplane and make it move. When a control surface like an aileron moves it diverts the air stream around it creating a force. All the forces generate torques around axes that pass from the center of gravity of our airplane. Summing up all these torques we have the final torque that actually changes the orientation of the airplane.
We can safely assume that the airplane itself is a rigid body. We all have seen the wings of airplanes we traveled with vibrating, which means that they are not so rigid, but for the needs of our simulator we can ignore it. So all we need to know is the total inertia of the airplane. This is a product of the local inertia of the elements we divided the airplane into, i.e. the fuselage, the wings, the flaps, the ailerons and the rudder.
Creating the simulator
Going into the details of the physics model of a flight simulator is beyond the scope of this article. All I wanted was to give you a simple picture of what is involved in the creation of the simulator. The physics model for the airplane was originally developed by David M. Bourg in his book "Physics for Game Developers" (ISBN: 0-596-00006-5 © O'REILLY 2002). This is one of the best books you can read that can help you approach game physics. The good thing about physics is that is is never outdated, so if you can find a copy of this book get it and read it.
The simple simulator we created divides the screen in two areas. The top displays the view from the pilot's window and the bottom is the instrument panel. The instrument panel displays a map of the world with the airplane always being in the center and pointing up. You will notice that this is actually the same as the top view only from a different perspective. To the right you will see three instruments. The first indicates the throttle and the sick position, the second is the banking indicator and the third is the artificial horizon.
The stick that controls the ailerons and the elevators is operated by the mouse. Moving the mouse up/down you operate the elevators, while moving right/left you operate the ailerons. The throttle is operated by the 'A' and 'Z' keys. The simulator is very simple and the scenery is not that great. There is no take off/landing procedure and the action starts in the air. No collision detection is performed and the earth is flat and limited. The visual effects developed in previous articles are there and the airplane physics is modeled very nicely.
The code for the flight simulator can be found here. Have fun modifying it and experimenting with it.