Depending on your goals you can approach the
task of modelling airplane physics in different degrees of realism. More often than not, some sort of an arcade
controller will suit your game better and feel cooler than a true to life simulation. Yet, In this video I am going to show you
how to make a more or less realistic model. I have two reasons for that. The first one is, of course, you can make something
that flies kind of like a real plane. How cool is that? But it can also be handy for someone not interested
in making hardcore flight sim. Recently I played Ace Combat 7. It is an arcade game about fighter jets that
is not concerned with adhering to the laws of physics. But it still in some form includes effects
like stalling or dependence of maneuverability on airspeed. For me it makes the game kind of charming. I think, getting a feel of a realistic simulation
can give you good ideas even for an unrealistic game. So, the question is, how do airplanes fly
and how can you model it in a game? Let’s consider the forces acting on an aircraft
which are important for our problem. There is of course force of gravity or weight
pulling the plane down. For the powered aircraft there is also thrust,
which pulls it forward. It can be created by various methods of propulsion
like propellers or jet engines. In our simulation we will just apply a force
in the direction of the airplane nose. And then there are aerodynamic forces which
result from the interaction of the plane with the air as it moves through it. Aerodynamic forces are usually divided in
two components: lift and drag. Drag is parallel to the flow direction, while
lift is perpendicular. Instead of looking at the plane as a whole
let’s break it down into several parts and consider them separately. By doing so we neglect the changes of airflow
around an element created by the other ones. For example the disturbance of air behind
the wing or downwash can alter the performance of the tail. There are ways to account for that but in
this video we will stick to individual components for simplicity. How can we calculate aerodynamic forces, acting
on a single plane’s part, like the wing? We can’t solve Navier-Stokes equations describing
the underlying physics in real time, so we need to simplify. For this we can use much simpler equations. They describe the forces and torque acting
on a wing in terms of airflow speed, air density, wing area and numerical coefficients. But why do these formulas look like they do? Let's figure it out! Consider a flat plate submerged into the flow
of air with speed V and density RHO directed perpendicular to its surface. I know it doesn’t look like a normal situation
for a wing, but bare with me here. We want to know how much force the air applies
to the plate. According to the Newton’s second law the
force equals the time derivative of the body’s impulse. Or to put it in a different way, the change
in impulse divided by the time during which the change happened. Let’s make an estimate of the impulse change
DELTA P during some small time DELTA T. Suppose that impulse transferred to a plate is proportional
to the combined impulse of the air particles, which can reach the plate during this time. The value of the numerical coefficient depends
on the details of the particles interaction with the plate and themselves. I’ve also multiplied the right hand side
by a half to follow the convention. The impulse equals mass times velocity. The combined mass of the particles is volume
times density of the air. During the time DELTA T particle can cover
distance V DELTA T. The volume containing the particles which
will reach the plate is then equals this distance times area of the plate. When we substitute the result into the Newton’s
law the DELTA Ts will cancel out and we’ll get something very similar to the formulas
we have seen earlier. Coefficients of lift, drag and torque are
not constants and depend on many factors. Three most important ones are angle of attack,
Reynolds number and Mach number. The angle of attack is an angle between the
airflow and the wing’s direction. The Reynolds number depends on the density,
speed and viscosity of the fluid and the size of the wing. It describes how turbulent the air is. The Mach number is the ratio of the air velocity
to the local speed of sound. It is very important for the flight at the
speed near or over the sound barrier. In this video we’ll consider only flight
at low Reynolds numbers with subsonic speeds, so coefficients will only depend on angle
of attack. I may come back to Reynolds and Mach numbers
in a future video. Let’s look at typical lift and drag coefficients
dependencies on angle of attack. At low alpha drag is small and lift is proportional
to alpha. How the wing performs in a low angle of attack
range depends on its cross section. There are special shapes designed to maximize
lift, while minimizing drag, called airfoils. At larger angles of attack lift becomes larger,
but so does drag. At some critical angle, usually around fifteen
degrees, so called stall or flow separation happens. The way the air flows around the wing changes
and the amount of lift decreases. The effectiveness of control surfaces also
diminishes. In this mode the wing performs more or less
like a flat plate. Aerodynamic coefficients are usually computed
in detailed simulations or measured in wind tunnels. You can compose an aircraft for your game
by digging up data for various parts obtained in these ways. But this is not a flexible solution, because
you are limited to the stuff you found. There is also problem of control surfaces. So far we have looked at the coefficients
of the wings with fixed shape. But they often have a moving part, that can
change its parameters on the fly. With those in mind you would have to either
find a detailed data table for the various alphas and control surface positions or come
up with it on your own. This is quite difficult. But while researching for this video I stumbled
upon an article, concerned with modelling toy planes in real time. It describes analytical equations for calculating
aerodynamic coefficients for an airfoil in the full range of angle of attack. The model also considers the effects of a
flap, which allows for easy control surfaces implementation. The aerodynamic element in this model is described
by few understandable and easy to tweak parameters. LiftSlope describes how much lift the surface
generates at low angles of attack. SkinFriction coefficient determines how much
friction the surface creates with the air. ZeroLiftAoA is an angle of attack at which
surface generates zero lift. StallAngle is a critical angle at which stall
happens. Chord is the length of mean chord in meters. FlapFraction is the size of the control surface
relative to the mean chord. AspectRatio is the ratio of the surface area
to the square of mean chord. With lift, drag and torque formulas and aerodynamic
coefficients we can calculate the forces acting on an element. Now let’s put them all together. We need to know the flow velocity around each
element. It consists of aircraft center of mass velocity,
velocity of the element due to rotation, and the additional velocity due to wind, propeller
wash or other things. Then we need to transform the result into
the coordinates relative to the element. We’ll discard the component of the velocity,
which goes along the surface perpendicular to the chord. It only causes skin friction and isn’t involved
in normal lift and drag creation. From the resulting velocity we can get dynamic
pressure and angle of attack, from which we can compute the coefficients and eventually
forces and torque. Then we need to apply them somewhere. The point of aerodynamic forces application
or aerodynamic center is usually placed around the quarter point of the chord. So to transfer these forces to the aircraft
we need to apply them directly to it and also add corresponding torque, due to their displacement
relative to the center of mass. Then we also need to add torque acting on
the element itself. I’ve implemented this thing using Rigidbody
to control the motion. I calculate aerodynamic forces and apply them
to the body using AddForce in FixedUpdate. But, this means that forces can change only
once every physics update. Since the values of the forces are dependent
on the velocity and angular velocity of the body, having large fixed delta time can lead
to some nasty effects. In this example I have lowered the physics
frame rate to 30 fps. With forces updating once in a frame the physics
can sort of ‘overshoot’ and we’ll get this jitter. With more nimble aircrafts it can get bad
even for 60 fps. You can solve it by making fixed delta time
smaller, but it is not always desirable. So let’s solve this problem in a cheaper
way. The algorithm is as follows. 1) We calculate the forces according to the
current velocity and angular velocity values. 2) Predict the velocity and angular velocity
in a half of timestep according to the forces that we’ve got in the step one. 3) Calculate the forces according to predicted
velocities. 4) Apply an average of the initial and predicted forces to the body. By this we are effectively creating a substep
specifically for aircraft physics. Since it's quite cheap, it won’t make a
toll on performance, unlike increasing the general physics frame rate. Source code is on my github as well as some
examples and tips on how to make a plane.