A simple flight simulator
To demonstrate 3D graphics using Processing, I wrote a simple flight simulator:
Actually the "flying" motion is really grossly simplified... there's no gravity, and no aerodynamic forces, just some artificial laws of motion. So what you're flying is more like a spaceship than an airplane. With a weird control system. But you get used to it.
Actually the "flying" motion is really grossly simplified... there's no gravity, and no aerodynamic forces, just some artificial laws of motion. So what you're flying is more like a spaceship than an airplane. With a weird control system. But you get used to it.
How it works
At any instant of time, the behavior of the spaceship is described by four parameters:
To calculate where the spaceship will go, one can figure out all the different forces acting on it, and then use Newton's laws of motion. Here, though, we will just impose some artificial laws, in order to keep things simple. Here are the rules:
The above rules simplify the situation, so that we just need to keep track of the spaceship's position and orientation. We can write down these variables in the following way:
- Position (where it is),
- Momentum (what direction it's moving, and how fast) (note that momentum = mass * velocity),
- Orientation (which way it's pointing),
- Angular momentum (what direction it's turning, and how fast).
To calculate where the spaceship will go, one can figure out all the different forces acting on it, and then use Newton's laws of motion. Here, though, we will just impose some artificial laws, in order to keep things simple. Here are the rules:
- We can control the spaceship's orientation directly: we can point it in any direction we want.
- We have three types of control: pitch (pointing up or down), yaw (pointing left or right), and roll (rotating clockwise or counterclockwise around the "forward-pointing" direction).
- When we press a control, the spaceship turns in the desired direction by some fixed amount, then stops -- it doesn't continue turning. We are ignoring the spaceship's angular momentum.
- The spaceship is always moving in the "forward-pointing" direction. When the spaceship turns (changes orientation), its direction of motion (momentum) also turns as well.
The above rules simplify the situation, so that we just need to keep track of the spaceship's position and orientation. We can write down these variables in the following way:
- Position: this is a vector, expressed in Cartesian coordinates.
- Orientation (with pitch and yaw control only): we keep track of a single vector, written in spherical coordinates (similar to latitude and longitude). Pitch control changes the "latitude" angle, while yaw control changes the "longitude" angle.
- Orientation (with pitch, yaw and roll control): we keep track of three vectors, pointing "forward," "up" and "left," written in Cartesian coordinates. Pitch control causes a rotation around the "left-pointing" vector, yaw control causes a rotation around the "up-pointing" vector, and roll control causes a rotation around the "forward-pointing" vector.