How NetWars Works Chapter 4 of 13, plus appendices

4. Motion: flying the ship

How your keys and mouse become turning, how the engine fights the ship's drift, and what happens when you bump into something. Every rate here is "per frame": 18.2 frames per second in single player (chapter 2). All of it lives in one function that runs once per frame, player_update in src/control.c.

The ship is the camera

NetWars has no separate camera object. Your ship's position and its three axes (forward a, right b, "up" c from chapter 3) are the camera: the picture is always drawn from the cockpit, looking along a. Everything in this chapter therefore moves both the ship and your view.

The ship's state is small:

PartWhat it is
positiona 64-bit fixed-point vector (chapter 3)
velocitythe same format: how far the ship moves per frame
axes a, b, cthree Q14 arrows
pitch, yaw, roll rateshow far to turn this frame, in 1/2048 of a turn
throttlethe forward speed you have asked for, −112 to 144 units per frame
timerstumble (frames of forced spin after a collision), bump immunity

Steering: turn rates

You never set the ship's direction directly. You set turn rates: how many steps (of 2048 per full turn) to turn in this frame. The player controls two of the three:

With the keyboard: a ramp

Turn rates are kept from frame to frame. While an arrow key is held, its rate grows by 2 per frame up to 24 (a rate in the other direction is first dropped to 0, so reversing is immediate). With no key held, the rate shrinks by 2 per frame back to 0. So a tap turns a little, holding the key accelerates the turn to at most 24 steps per frame (about 4.2° per frame, 77° per second in single player), and letting go eases it out.

With the mouse: a joystick

The mouse pointer is not something you drag onto a target. Each frame the game looks at how far the pointer is from the centre of the screen and turns that offset directly into rates:

"Reversed Mouse" in the Controls menu mirrors the pointer vertically, so pushing the mouse forward lowers the nose like a flight stick (with Normal Mouse it lifts the nose) (pitch then ranges about −33 to +37). (The exact rounding near the dead zone is spelled out in the comments of src/control.c.)

Turning the axes each frame

After the rates are known, the three axes are turned in a fixed order: pitch, then roll, then yaw, each only if its rate is not 0. Each turn follows the same two-step recipe (chapter 3):

Turn1. rotate2. rebuild
pitchforward toward "up" by the pitch rate"up" = forward × right
rollright toward "up" by the roll rate"up" = forward × right
yawforward toward right by the yaw rateright = "up" × forward

Step 1 turns arrow p (the first one named) toward arrow q (the second) with p′ = p · cos θ + q · sin θ, using the sine and cosine tables, followed by a cheap length fix: the game computes how far the squared length of p′ is from 1 and moves each non-zero component toward (or away from) 0 by half that amount, which is about the error in the length. Step 2 uses the cross product to make the third arrow exactly perpendicular to the other two again. Repeated every frame, this keeps the three arrows close to length 1 and at right angles without ever computing a square root.

Quirk

The original's cross product loses a "borrow" in its 32-bit subtraction, so a component can come out 4 units of 16384 too large (about 0.02 %). Its length fix is only approximate as well. Neither is visible in play; a rebuild does not need them.

For a rebuild

With floating-point numbers you can apply the same three turns in the same order with exact sine and cosine, rebuild with cross products as in the table, and normalise the arrows to length 1 every frame. (A quaternion works too, as long as the turn order and directions are the same.) Keep the rates as "steps per frame" with 2048 steps per turn; that is what makes the ship feel the same.

Throttle and inertia

NetWars ships are not cars: they drift. Turning changes where the nose points, not where the ship is going. The engine then slowly pulls the motion in line with the nose. This is done by a small controller that runs every frame while you are alive:

  1. Throttle. Shift raises the throttle by 8 per frame while it is below 144, Ctrl lowers it by 8 per frame while it is above −112 (reverse); starting from 0 it stops exactly at 144 and −112. The throttle stays where you leave it. It is the forward speed you want, in units per frame. If both are held, both steps are applied (+8, then −8), which cancels except at the top limit, where only the −8 applies (144 becomes 136). Extra thrust (a pick-up) doubles the wanted speed (up to 288) for as long as any is left; it is used up only by hard corrections (step 3).
  2. Measure. The ship's velocity is split into three parts along its own axes with dot products: sideways (along right), vertical (along "up") and forward. Only whole units count: each world coordinate of the velocity is first rounded down to a whole unit (so 0.5 becomes 0, but −0.5 becomes −1).
  3. Correct. Each part is pushed toward its target: sideways and vertical toward 0, forward toward the throttle. The push is a fixed step along that axis whose size depends on how big the error is:
Error (units per frame)0 (exactly)1–23–45–89–16over 16over 32, with extra thrust
Correction this framenone0.5124816 (uses up one unit of extra thrust; this can happen on each of the three axes in one frame)

So the ship accelerates by at most 8 units per frame per frame (16 with extra thrust), gently at first when the error is small. Sideways drift after a turn dies away the same way. The graph shows the forward speed when you hold Shift from standstill for 18 frames, wait, then hold Ctrl for 18 frames:

1440 020 406080 frames throttle (wanted speed) actual forward speed Shift heldCtrl held
Forward speed in units per frame, simulated from the rules above (straight flight along a world axis, no turning). The speed trails the throttle by about 10 units and settles about 8 frames after the throttle stops changing. The tail at the bottom right is the residual drift described below.
Quirk

Because only whole units are measured, a drift of less than one unit per frame in a positive direction is never corrected: the error reads as 0. So after braking to throttle 0 the ship can keep creeping at up to about one unit per frame (0.5 in the example above), and at full throttle it settles anywhere between the throttle and one unit more D. Harmless, and a rebuild may drop it.

Finally the ship moves: position = position + velocity. There is no other speed limit and no friction: without the controller a ship would drift forever.

When the shields are 0 (dead, waiting for Enter), the ship can still turn and look around, but the throttle, the weapons and the movement are switched off: it hangs still (its velocity is kept, it just is not applied).

The order inside one frame

  1. The bump-immunity and tumble timers count down (main loop).
  2. Turn rates from the mouse or the keys; the tumble decays and is applied; the axes are turned (pitch, roll, yaw); the radar zoom keys are read. If dead, stop here.
  3. Throttle keys, fire, Enter (missile or cloak), cloak time drains.
  4. Velocity measured along the ship's axes and corrected, unless tumbling.
  5. Position = position + velocity.

Collisions are found later in the same frame, while the aliens and other players' ships are updated (chapters 5 and 6). When several keys conflict, ↓ wins over ↑ and → over ←; either Shift raises and either Ctrl lowers the throttle.

Bumps: collisions throw the ship

When the ship rams an alien (single player) or another player's ship (multiplayer) and survives, it is bumped (player_bump, src/players.c; how a touch is detected is chapter 6):

A missile that hits you only costs a shield and flashes the screen: no tumble, no push. Pods never collide with you at all.

Quirk

The push after a collision always goes the same way in the world, whatever the direction of the crash. PLAYER 1 and 2 (or 3 and 4) who collide are pushed in opposite directions (runtime #22); any other pair is pushed at right angles. In single player every bump shoves you along +z. Keep it for the original feel, or replace it with a push away from the obstacle.

Starting, restarting and the rolled camera

Quirk

The "Level n" introduction screen slowly rolls the camera (16 steps per frame, right toward the opposite of "up") as a visual effect, and nothing resets it before the flight starts. That screen runs at about 70 frames per second, so after 10 seconds the view has rolled about five and a half turns. A single-player level therefore starts with the world tilted by an angle that depends on how long the introduction was shown (in the measured run about 180°, upside down; runtime #35). Multiplayer is not affected. A rebuild should probably reset the roll; it changes nothing else, because space has no "down".

Exact rules for a faithful rebuild