Physics only, nothing on screen yet.
Started with the part I was least confident about, which is the atmosphere. I
wanted drag to actually matter instead of being a made up constant, so I looked
up the International Standard Atmosphere and typed the layer table in by hand:
base altitude, base temperature, base pressure, lapse rate, seven layers up to
84.8 km.
The barometric formula has two cases, one for layers where the temperature
changes with height and one for the isothermal layers where the lapse rate is
zero, and I lost about an hour to the first one. My pressures came out roughly
three times too low above 20 km. I had written the exponent as
-G0 * lapse / AIR_R instead of -G0 / (lapse * AIR_R). Staring at it did
nothing. What actually found it was printing my pressure at 0, 11, 20 and 32 km
next to the published numbers, at which point it was obvious the error grew
with altitude and therefore lived in the exponent.
The second problem was the top of the atmosphere. Above 84.8 km the tables run
out so I bolted on an exponential tail, and then noticed the vehicle was still
picking up a tiny bit of drag at 300 km, which slowly ate the orbit over a long
coast. Added a linear fade so density genuinely hits zero at
ATMOSPHERE_TOP = 140 km. Not physically correct, but the alternative is
modelling the thermosphere and I am not doing that for a game.
Last thing was the integrator. I wrote Euler first because it is four lines,
and wrote a test that drops a mass from 200 km with no thrust and no air and
checks that the specific orbital energy stays constant. It did not. It drifted
about half a percent per simulated minute, which sounds harmless until you
realise a long coast to apoapsis is fifteen minutes and the apoapsis walks
while you watch it. Rewrote derivatives() and pushed it through RK4, holding
thrust and mass fixed across the four evaluations. The mass changes by a few kg
over a 20 ms step so it makes no difference, and it keeps the state vector down
to four numbers instead of five. Energy now holds to about seven decimal
places.
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.