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:
| Part | What it is |
|---|---|
| position | a 64-bit fixed-point vector (chapter 3) |
| velocity | the same format: how far the ship moves per frame |
| axes a, b, c | three Q14 arrows |
| pitch, yaw, roll rates | how far to turn this frame, in 1/2048 of a turn |
| throttle | the forward speed you have asked for, −112 to 144 units per frame |
| timers | tumble (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:
- pitch turns forward toward "up". Because "up" is drawn toward the bottom of the screen, a positive pitch makes the nose dip and the world slide up: the ↓ key. ↑ gives a negative pitch.
- yaw turns forward toward right: → positive, ← negative.
- roll (right toward "up") is never controlled by the player. Only a collision tumble rolls the ship.
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:
- pitch ≈ (pointer below centre in pixels) ÷ 4, yaw ≈ (pointer right of centre) ÷ 8;
- a small dead zone, just up and left of the centre (where the game parks the pointer at start), gives 0, so the ship flies straight there;
- no ramp: moving the pointer changes the rate at once. Rates can exceed 24 (the keyboard maximum) if the pointer is far out: during flight the pointer is kept inside a box from 276 pixels left to 267 right and from 147 up to 138 down of the centre, so pitch reaches about −35 to +35 and yaw −33 to +33.
"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):
| Turn | 1. rotate | 2. rebuild |
|---|---|---|
| pitch | forward toward "up" by the pitch rate | "up" = forward × right |
| roll | right toward "up" by the roll rate | "up" = forward × right |
| yaw | forward toward right by the yaw rate | right = "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.
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.
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:
- 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).
- 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).
- 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–2 | 3–4 | 5–8 | 9–16 | over 16 | over 32, with extra thrust |
|---|---|---|---|---|---|---|---|
| Correction this frame | none | 0.5 | 1 | 2 | 4 | 8 | 16 (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:
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
- The bump-immunity and tumble timers count down (main loop).
- 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.
- Throttle keys, fire, Enter (missile or cloak), cloak time drains.
- Velocity measured along the ship's axes and corrected, unless tumbling.
- 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):
- for 8 frames no further collision can hurt (bump immunity), so one contact costs one shield, not one per frame;
- for 48 frames the ship tumbles: pitch and roll get a random rate of +48 or −48 each (a coin toss per axis), shrinking by 1 per frame (in practice applied for 47 frames with rates 47, 46, … 1, about 200° on each axis). With the keyboard the tumble replaces your steering, so the arrow keys do nothing; with the mouse it is added to it;
- during the tumble the velocity controller is off, so the ship drifts about 3000 units before it can brake;
- the velocity is replaced by a fixed push of 64 units per frame along a world axis that depends only on your slot: +z in single player (and for PLAYER 1), −z for PLAYER 2, +y for PLAYER 3, −y for PLAYER 4. It does not depend on which way you face or what you hit;
- the screen flashes, "COLLISION" is shown for 48 frames.
A missile that hits you only costs a shield and flashes the screen: no tumble, no push. Pods never collide with you at all.
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
- Single player, start of a game: at the origin, standing still, forward = +z, right = +x, "up" = +y, throttle 0, 32 shields.
- Single player, next level: the "Level completed" screen puts the ship back at the origin, standing still, with the starting orientation; shields carry over plus 8 (at most 32); missiles, extra fire power and extra thrust carry over; the bonus goes to the score.
- Multiplayer, joining a game (or after Esc): at the slot's corner and heading (chapter 3), throttle 0, 32 shields.
- Multiplayer, after being killed: you stay where you were, moved 1024 units along your "up" axis (so you do not restart inside what killed you H), throttle 0 (the old velocity is kept and then braked), 32 shields, cloak, extra thrust and fire power gone.
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".
- Keyboard ramp. At the start of each frame a pitch or yaw rate of exactly ±1 (left over from mouse steering) is set to 0, and roll is set to 0. Then, for pitch (↓ positive, ↑ negative) and yaw (→ positive, ← negative): if the key for the positive direction is held, a negative rate is first set to 0, then the rate grows by 2 if it is below 24; otherwise if the other key is held, the mirror image; otherwise a positive rate shrinks by 2 and a negative one grows by 2. So reversing gives ±2 in that frame.
- Tumble. In a frame with tumble left, the three tumble rates (pitch, roll, yaw; yaw is always 0) first move 1 toward 0, then replace the steering rates (keyboard) or are added to them (mouse). When the tumble ends the rates simply continue from their last values and the keyboard ramp takes them back to 0.
- Velocity controller order. The three parts are corrected in the order sideways, vertical, forward, each measured once at the start (with the velocity's whole-unit parts and rounded-down dot products, chapter 3). Each correction that uses the 16-unit step takes one unit of extra thrust, so up to three per frame. Extra thrust doubles the wanted speed in both directions (reverse too).