Aviator on the BBC Micro

# Matching the code to the flight model

## Tracking down the code that implements the flight model

Every single clue is useful when you're trying to analyse a complex game like Aviator. This is particularly true when there's nothing out there to point you in the right direction - prior to this project, Aviator had no disassemblies, no analyses, nothing other than oblique references to the complexity of the simulation in magazine articles from back in the day. Luckily there is a section on pages 4 and 5 of the game's manual that's absolute gold dust to those of us trying to map the code: it's called "The scope of the simulator", and it lists all of the features of the game's flight model.

In this deep dive, I'm going to try to tie each of the features listed in the manual to the corresponding section of the game's code, but first let's look at what the manual says.

## The scope of the simulator --------------------------

This section of the game's manual shows just how sophisticated Aviator's simulation is. Spread across two pages, it's an impressive read, particularly for an 8-bit flight simulator from the early 1980s. This is how the manual describes the game's simulation:

For the technically minded, here is a guide to the scope of the simulator itself.

The simulator models the forces that would be encountered by the real aircraft due to gravity, propeller thrust, aerodynamic forces due to airflow and contact with the ground. The equations of motion cover the full six degrees of freedom. A detailed list of those forces follows:

Linear forces
-------------

1. Wing lift
2. Aircraft drag (parasitic)
3. Induced drag
4. Side forces due to aircraft slip
5. Lift due to flaps
6. Drag due to flaps
7. Drag due to undercarriage
8. Propeller thrust (as a function of aircraft speed)
9. Propeller drag when gliding
10. Forwards, downwards and sideways gravitational forces
11. Upward reaction due to wheels on the ground
12. Sideways frictional forces of wheels
13. Wheel braking
14. Ground steering

Angular forces
--------------

Pitching moments due to:

• Elevator deflection
• Centre of gravity/centre of pressure
• Tail forces due to linear motion
• Tail forces due to pitch rate

Rolling moments due to:

• Aileron deflection
• Wing forces resulting from roll rate
• Wing forces resulting from aircraft slip

Yawing moments due to:

• Rudder demand
• Aircraft side, fin and rudder forces due to aircraft slip - Aircraft side, fin and rudder forces due to yaw rate

In addition, all aerodynamic related forces are adjusted with increasing altitude to allow for the decreasing density of air.

The simulator includes three axes of linear momentum and three axes of angular momentum.

Other features include:

• Stalling
• Wings breaking under excessive g-forces
• Tail wheel configuration
• Belly landing
• Bumpy ground

The aircraft bullets, once fired, will continue to move through space in the direction fired, independent of subsequent aircraft manoeuvres.

That's quite a list! Let's see if we can match all the information above to the actual game code, to see how it's done.

## Matching the code to the manual -------------------------------

The following table lists all of the above features, along with the relevant snippet from the flight model calculations. Use the links to take you to the relevant part of the source code or the corresponding deep dive.

Note that these represent my best guesses, so take the following with a healthy pinch of salt!

Wing lift* yLiftDrag = yVelocityP * 2 * 4

* Code: ApplyAerodynamics (Part 3 of 3)

* Deep dive: The flight model (calculation 1)
Aircraft drag (parasitic)* zLiftDrag = zVelocityP * 2

* Code: ApplyAerodynamics (Part 3 of 3)

* Deep dive: The flight model (calculation 1)
Induced drag* Code: ScaleFlightForces

* Deep dive: The flight model (calculation 3)
Side forces due to aircraft slip* xLiftDrag = xVelocityP * 2 * 4

* Code: ApplyAerodynamics (Part 3 of 3)

* Deep dive: The flight model (calculation 1)
Lift due to flaps 1* yFlapsLift = zLiftDrag

* Code: ApplyAerodynamics (Part 3 of 3)

* Deep dive: The flight model (calculation 1)
Lift due to flaps 2* yFlapsLift force factor

* Code: ScaleFlightForces

* Deep dive: The flight model (calculation 3)
Drag due to flaps* zLiftDrag force factor

* Code: ScaleFlightForces

* Deep dive: The flight model (calculation 3)
Drag due to undercarriage* zLiftDrag force factor

* Code: ScaleFlightForces

* Deep dive: The flight model (calculation 3)
Propeller thrust (as a function of aircraft speed)* thrustScaled

* Code: ApplyTurnAndThrust (Part 2 of 2)

* Deep dive: The flight model (calculation 5)
Propeller drag when gliding* zLiftDrag force factor

* Code: ScaleFlightForces

* Deep dive: The flight model (calculation 3)
Forwards, downwards and sideways gravitational forces* dyVelocity = ... - 4096

* Code: ApplyFlightModel (Part 6 of 7)

* Deep dive: The flight model (calculation 6)
Upward reaction due to wheels on the ground* yGravity = -512 - (dyVelocity / 2 + 1)

* Code: ApplyFlightModel (Part 1 of 7)

* Deep dive: On-ground calculations
Sideways frictional forces of wheels* xLinear = -xVelocityPLo * 128

i.e. xLinear = -xLiftDragSc / 256

* Code: ApplyTurnAndThrust (Part 2 of 2)

* Deep dive: On-ground calculations
Wheel braking* zLinearHi = zLinearHi - A

where A depends on brakes, undercarriage and type of landing

* Code: ApplyFlightModel (Part 5 of 7)

* Deep dive: On-ground calculations
Ground steering* yTurn = groundSteering

* Code: ApplyFlightModel (Part 5 of 7)

* Deep dive: On-ground calculations
Pitching moment due to elevator deflection* xControls = zLiftDrag * elevatorPosition

* Code: ApplyFlightControl

* Deep dive: The flight model (calculation 2)
Pitching moment due to centre of gravity* dxTurn = ... + yGravity

* Code: ApplyAerodynamics (Part 3 of 3)

* Deep dive: The flight model (calculation 4)
Pitching moment due to centre of pressure* xMoments = xMoments + (zLiftDrag * 8)

* Code: ApplyAerodynamics (Part 3 of 3)

* Deep dive: The flight model (calculation 2)
Pitching moment due to tail forces due to linear motion* xMoments = (yVelocityP * 2) ...

* Code: GetMoments

* Deep dive: The flight model (calculation 2)
Pitching moment due to tail forces due to pitch rate* xMoments = ... - (xTurn * 250 / 256)

* Code: GetMoments

* Deep dive: The flight model (calculation 2)
Rolling moment due to aileron deflection* zControls = zLiftDrag * aileronPosition

* Code: ApplyFlightControl

* Deep dive: The flight model (calculation 2)
Rolling moment due to wing forces resulting from roll rate* zMoments = -zTurn * 2

* Code: GetMoments

* Deep dive: The flight model (calculation 2)
Rolling moment due to wing forces resulting from aircraft slip* zSlipMoment = xLiftDrag

* Code: ApplyAerodynamics (Part 3 of 3)

* Deep dive: The flight model (calculation 2)
Yawing moment due to rudder demand* yControls = zLiftDrag * rudderPosition

* Code: ApplyFlightControl

* Deep dive: The flight model (calculation 2)
Yawing moment due to aircraft side, fin and rudder forces due to aircraft slip* yMoments = (xVelocityP * 2) ...

* Code: GetMoments

* Deep dive: The flight model (calculation 2)
Yawing moment due to aircraft side, fin and rudder forces due to yaw rate* yMoments = ... - (yTurn * 250 / 256)

* Code: GetMoments

* Deep dive: The flight model (calculation 2)
Stalling 1* Stall when:

zVelocityP <= |yVelocityP| * 4

* Code: ApplyAerodynamics (Part 2 of 3)

* Deep dive: Stalling and recovery
Stalling 2* yLiftDrag force factor

* Code: ScaleFlightForces

* Deep dive: Stalling and recovery
Wings breaking under excessive g-forces* Break when:

|yLiftDrag| >= 2048 / 39 = 53 (stalling)

|yLiftDrag| >= 2048 / 156 = 13 (not stalling)

* Code: ScaleFlightForces

* Deep dive: Ripping the wings off
Tail wheel configuration* When taxiing with the undercarriage down, restrict pitching and tilt the plane backwards

* Code: ProcessLanding (Part 5 of 7)

* Deep dive: Take-offs and landings
Belly landing* When taxiing with the undercarriage up, restrict pitching

* Code: ProcessLanding (Part 4 of 7)

* Deep dive: Take-offs and landings
Bumpy ground* If this is an emergency landing, make it a bumpy one

* Code: ProcessLanding (Part 1 of 7)

* Deep dive: Take-offs and landings
All aerodynamic related forces are adjusted with increasing altitude to allow for the decreasing density of air* airDensity = ~yPlaneHi * 2

* Code: ScaleByAltitude

* Deep dive: The flight model (calculation 1)
The aircraft bullets, once fired, will continue to move through space in the direction fired, independent of subsequent aircraft manoeuvres* Rotate the bullet travel vector to the outside world's frame of reference using matrix 2

* Code: FireGuns

* Deep dive: Adding bullets to the world
The simulator includes three axes of linear momentum and three axes of angular momentum* 3-axis variables are used throughout the flight model

* Code: ApplyFlightModel

* Deep dive: The flight model