Let's Go to Mars with Martian Lander
This is the beginning of a series of articles where I develop a variation on the classic lunarlander game themed around the planet Mars. To do this in three dimensions can be rather complicated, so in the spirit of the original arcade game (that I became rather obsessed with, I should admit), I'm going to tackle the simplified twodimensional problem. I also am going to discount terrain issues, although clearly landing on the very edge of one of the mysterious Martian canals would be more tricky than a flat plain in the Schiaparelli crater.
Oh also, I'm not going to have any graphics at all. This is going to be a game where you enter thrust commands second by second and either shoot off into orbit and land smoothly on the Martian surface or crash into the planet. You want to add graphics? Excellent. But I'm going to leave that as an exercise for you, the reader, as that's pretty far afield for this shell scripting column.
Gravitational Mathematics
I can't begin Martian lander without talking about physics, because it's Newton's laws (they're not just a good idea!) that describe the process of an object coming into the gravitational field of another and being pulled toward its center.
The Newtonian gravitational formula is F = G m1 m2 / r2, and the big idea is that every object in the universe attracts every other object with a force that is proportional to the product of their masses and inversely proportional to the square of the distance between the two objects.
I'm not going to worry about other planets, however, because the gravitational force that far distant objects have on a ship attempting to land on Mars is quite negligible (to say the least). The difference in mass between a planet and a spaceship are enormous too, allowing me to simplify the formula: velocity = gravity * time.
If I drop something out of a stationary helicopter (or off the side of a building), in second 0, it'll be falling at 0 ft/s. After one second, it'll be traveling 32 ft/s, and after ten seconds, it'll be going 320 ft/sec. I'm discounting air resistance, and so on, but this example is regarding landing a spaceship on Mars, so there really isn't much atmosphere to worry about here.
The next question is how far has the object fallen in a specified number of seconds? This is a more complex equation: distance = ( gravity * time**2 ) / 2.
So after those same ten seconds, the object will have fallen (32 * (10**2))/2 = 1600 feet. If I begin the Earthly descent one mile above the surface, that means that without any rockets to slow things down, it'll take just more than 18 seconds to crash.
Mars, however, has a different gravitational force than Earth does. Earth is 32.1 ft/s, while Mars, with only 15% of the mass of our home planet, has a gravity of 12.1 ft/s.
This means that from a one mile orbital trajectory, it would take a lot longer—almost 30 seconds to crash into the surface of the planet. That's a lot more time to wonder why you forgot to add thrusters to your Martian lander, for sure!
There's a third equation also needed for this game: horizontal velocity. The idea is that the lander will begin in orbit, so it'll have a starting horizontal velocity but no vertical velocity. The rocket boosters will be able to be turned to a specific angle and fired, so with just the right effort, you can stop the horizontal motion entirely, allowing the craft to descend straight onto the planet—a good thing, because hitting the surface with lots of horizontal motion is going to be a crash!
In this case, the formula is really easy, because there's no force "pulling" the craft ever faster around the planet nor any force (such as wind resistance) that's slowing it down either. So if the craft starts with a 100 ft/sec horizontal velocity, it'll land with exactly the same velocity if the thrusters don't slow it down.
For simplicity, the formula I'll use is speedH = initial speed – thrust. So to stop all horizontal speed, a burst that exactly matches the current speed is all that's required. I'll assume this all happens essentially instantaneously.
But, the thrusters can operate within a 90° rotation, ranging from straight downward (all vertical thrust, no effect on horizontal speed) to straight forward (all horizontal thrust, no effect on vertical drop). How do you model that? Here are my shots at the formulas, with zero degrees being down and 90 degrees being forward:

thrustH = thrust * (angle / 90)

thrustV = thrust * (1 – (angle / 90))
So a thrust of 100fps at 45° should balance out, and 100*(45/90) = 50fps and 100 * (1 – (45/90)) = 50. Good. What about a 10° thrust? ThrustH = 11.11 fps and ThrustV = 88.88 fps.
Building the Program from the Math
Now I have the basic mathematics required, remembering that each second of time adds (gravity) vertical acceleration toward the Martian surface. To make this more fun, I'm going to assume that the onemile mark is the very edge of the Martian gravity, so if at any point the craft goes farther than a mile from the surface, it's lost in deep space.
You simply could burn the retro rockets at exactly the force of gravity for as long as it takes to land on the surface, but of course, you don't have that much fuel (predictably!). There are constraints to the rocket boosters too: no burst greater than 100fps is allowed, or it'll tear the lander apart, which is definitely not a desired outcome!
That means you can't wait until the last second and slam a huge burst of rocket power just before you crash—not to mention that the gforces would be more than a bit problematical!
Let's start pulling things together. At any given second, you'll have the options of firing the thruster, how much to fire it and at what angle it should be fired. Against that, at any given second, you'll have both horizontal and vertical velocity and vertical gravitational pull. Like this:

thrustH = thrust * (angle / 90)

thrustV = thrust * (1 – (angle / 90))

velocityH = velocityH – thrustH

velocityV = (velocityV + gravity) – thrustV
The initial values are thrust = 0, angle = 0, velocityH = 100 (everything will be in feet per second for simplicity) and velocityV = 0. Gravity on Mars = 12.1.
There's another formula required, and that's height. At any given second then, height = height – (velocityV).
If the craft starts at 5280 feet off the Martian surface, then in second zero:

thrustH = 0

thrustV = 0

velocityH = 100

velocityV = 0

height = 5280
And, in second one, assuming you fire the thrusters at 20fps for one second at 45 degrees:

thrustH = 20 * ( 45/90 ) = 10

thrustV = 20 * ( (1 – (45/90) ) = 10

velocityH = (100 – 10) = 90

velocityV = (0 + 12.1 – 10) = 2.1

height = (5280 – 2.1) = 5278.9
See how that works? It's not too bad once you get through all the basic calculations.
Having gone through all of this physics and mathematics, in my next column, I'll jump into the coding, because it's going to be pretty straightforward. So stay tuned!
Note: thanks to my friend Brad Waller for pointing out all the major oversimplifications in my physics modeling. My defense is that it's just a game, and I'm sticking with that, so take a deep breath, physics nerds.