SCARY PHYSICS FOR PROGRAMMERS
Realistic space travel timer (source code)
Collision detection engine demo (source code, theory)
Vectors and acceleration/velocity equations (suvat equations)
First, a couple of quick definition-type-things:
I've put formulae in square brackets so they stand out.
Scalar: something like a speed, which has only a magnitude and no direction.
Vector: something like a velocity, with a magnitude and a direction. Can also be defined as the sum of movements in multiple dimensions (ie, an x movement plus a y movement), which makes life much easier when you're trying to add vectors. For some reason, the component in the x direction is measured in terms of i and the y direction in terms of j, for example the point (2,3) could be described with the position vector 2i + 3j. Yes, technically speaking i and j should be bolded as they're vectors. I suspect I'll start forgetting soon.
Acceleration: rate of change of velocity. You probably know that, but just to be safe.
In formulae, angles are denoted by the Greek letter theta. However, I'm lazy so I'm going to use g (as in angle) because I can't think of anything else that might be relevant that uses it, and I can for a and n. Magnitude is |v| for the magnitude of a velocity, |a| for the magnitude of an acceleration, |x| for the magnitude of whatever, etc.
I'm going to use standard units if I use any at all - metres for distance, seconds for time and the like - but you can easily use whatever units you like; units and turns, for example, or even quasits and rushyos should you so choose. (Further note: it's probably sensible to use SI (standard) units - it makes life easier, especially when dealing with constants like G)
Resolving direction/magnitude form vectors into components:
This is basic trigonometry. First, make sure the angle is in degrees clockwise from North. Then, with that as your angle and the magnitude as the hypotenuse, your i value is [i = |x| sin g] (magnitude multiplied by the sine of the angle) and your j value is [j = |x| cos g].
Going back to direction/magnitude form:
The magnitude is [|x| = SQRT(i^2 + j^2)] (magnitude is the square root of the i value squared plus the j value squared; Pythagoras' theorem).
The direction is [g = arctan (i/j)] (angle equals the inverse tangent of the i value over the j value).
All calculations will be done in component form; as mentioned earlier it's much easier that way (I'm not even sure if it's possible otherwise).
To get the final velocity (v), you need the initial velocity (u), the acceleration (a) and the length of time (t).
[v = u + at] (final velocity is the initial velocity plus the acceleration multiplied by the length of time the acceleration is applied for). Since I assume you'll be recalculating position every turn, you can basically ignore the time component, in which case the final velocity is simply [v = u + a]. I suspect the easiest way to do this will be to do the calculation twice, once for the x direction and once for the y direction.
For example, a ship with velocity 5i + 2j fires its engines at 45 degrees with a magnitude of root 8 (that is, the square root of 8).
Resolving the acceleration vector, the i value is root 8 sin 45 and the j value is root 8 cos 45, both of which come to 2, so the acceleration is 2i + 2j. (Yeah, I cheated to make the numbers easy.)
Therefore, to get the velocity after a turn:
i component: v = 5i + 2i = 7i
j component: v = 2j + 2j = 4j
So v = 7i + 4j.
Unfortunately, you still don't know where the ship actually is.
To get the distance (s) you need the initial velocity (u), the time (t) and the acceleration (a).
[s = ut + (1/2)*a*t^2] (the distance is the initial velocity multiplied by the time plus half the acceleration multiplied by the time squared). Complicated, but since we're working on a timescale of 1, you can reduce that to [s = u + a/2].
Therefore, for the position of the ship mentioned above, the change in position is:
x direction: s = 5i + (2/2)i = 6i
y direction: s = 2j + (2/2)j = 3j
Add those values to the coordinates of the ship's original position and you have your ship's new position and its velocity. Physics for idio- mathematicians.
Incidentally, if you want to find out what direction and how hard to fire your engines in order to stop in a turn:
[a = -u], that is, the acceleration must equal minus the initial velocity. If you resolve that value and it's greater than the ship's rated maximum acceleration, find how many times greater than its maximum acceleration that value is, round up, and do it again with a/that rounded value.
To get the acceleration (a)to stop at a point, you need the distance from you to the point (s), your initial speed (u), and your final velocity (v) which has to be 0.
[v^2 = u^2 + 2as] (final velocity squared equals initial velocity squared plus twice the acceleration multiplied by the distance). Because you're trying to stop, [0 = u^2 + 2as]. This re-arranges to [a = -((u^2)/2s)]. Again, if you've left it too late to give the command the acceleration may be too hard for the ship, while if you give the command too early, you'll be slowly deccelerating for ages. Fun fun.
Further equations of motion are available on their wikipedia page. With that, basic 2D vector theory, and the examples I've provided here you should be fine. Just bear in mind that while you're working in vectors to make adding accelerations and suchlike easier, you'll have to do every calculation twice, once in the x direction and once in the y direction. If your direction of motion, your ship and your destination all line up in a neat one-dimensional space, you can resolve things back to direction/magnitude and forget about the direction, but it's probably easier to do everything twice than check for alignment.
If you have further questions or individual applications you can't work out the formulae for, ask and trained monkey physicist will oblige. I hope this all makes sense; it's 3am.
Rate ship engines as force provided rather than acceleration provided. This can then be combined with the force due to gravity to get the total force and direction (see vector addition in previous section - convert to component form and add). Then use [f=ma] (force equals mass times acceleration) on each component to get the acceleration in component form.
Momentum is basically a measure of how hard something is to stop, and is defined as [p = mv] (momentum = mass times velocity). Momentum is conserved if no force acts, for example in a collision the total momentum of the colliding parties before and after is equal. I doubt we'll need this.
Mass increases as speed increases, as a consequence of special relativity. [M = ym] (Relativistic or effective mass equals gamma times the invariant or basic mass). Gamma is the Lorentz factor, defined as [y = c/(SQRT(c^2 - |v|^2))] (the Lorentz factor equals one over the square root of one minus the speed squared over the speed of light squared). Bear in mind you need the magnitude of the velocity, ie the speed, here. Use this value as the mass in f = ma, that is, use [f = Ma], to correctly simulate the drop in acceleration at higher speeds and thus prevent anyone from exceeding the speed of light.
Circular Motion, Gravity and Orbits
The equation for force due to gravity is [F = (GMm)/(s^2)] (Force due to gravity equals Newton's gravitational constant, 6.67e-11, multiplied by the mass of one object multiplied by the mass of the other object all divided by the distance between the centres of the two objects squared). The force applies to both objects, pulling them both towards each other with this force, but at the kind of scales we're talking about it's probably easier to assume the heavier object (probably a planet or sun) is fixed and the lighter object (ship) is the only one that does anything.
Angular velocity is [w = angle / t] (angular velocity (the symbol is omega) equals change in angle over time taken). Angle must be measured in radians, and the angular velocity is measured in radians per second. It can also be found from [w = angle * f] (angular momentum = change in angle multiplied by frequency), frequency being the number of revolutions around the circle per second. Note that a full revolution is 2pi radians, and thus [w = 2pi / time for one revolution].
Now the fun bit: orbits. If something is moving in a circle (you can also work out elliptical orbits, slingshots, etc, but that's more complicated and I'll get to it later), it's accelerating at a particular acceleration towards the centre of the circle such that the directional component of its velocity changes at a constant rate that produces a circular path. The actual speed (the magnitude of the velocity) does not change. You can demonstrate with a diagram why the acceleration is always towards the centre of the circle and produces a circular path. I might even draw one. The speed is related to the angular velocity and the radius of the circle: [v = r w] (magnitude of the velocity or speed equals the radius times the angular velocity).
Now, the acceleration holding the object in a circular path is called the centripetal acceleration and is also related to the angular momentum and radius: [a = r w^2] (centripetal acceleration equals radius multiplied by the angular velocity squared). Substituting in the acceleration linking angular velocity, speed and radius you can also get that [a = (v^2) / r] and [a = v w]. The centripetal acceleration is produced by a centripetal force. Since F=ma, you can just multiply the acceleration by the mass of the object to get the force, for example [F = mv w] (Force = mass times speed times angular velocity). I apologise for the confusion between f for force and f for frequency that may crop up here, as well as the complication of having two masses when you're talking about gravity, and I'm afraid I've been a bit inconsistent using capital letter F for force and small f for frequency which I technically should. Ahem.
Anyway, the main point here is that in an orbit, the centripetal force is the gravitational force, therefore you can equate the centripetal force and gravitational force equations. Note that the radius of the orbit equals the distance in the gravity equation. With a bit of cancelling, you get this: [(GM)/(r) = (v^2)] (the gravitational constant multiplied by the mass of the heavier object (planet, for example) divided by the radius of the orbit equals the speed squared). You can cancel with other equations to get angular velocity terms rather than speed, for example, but that's probably the most useful for finding stable orbits. Also note that if you attempt to predict multiple orbits whose gravity affects all the other objects in the orbit you get insanely complicated mathematical problems (with two bodies, it's a fairly simple two-body problem, with three it's a barely solvable three-body problem, and with more there are no known solutions), so the easiest solution is to ignore the gravitational effects of everything other than the orbited body (ie, the moon is only affected by the gravity of the planet, the planet is only affected by the gravity of the star, etc). It's generally easiest to take the position of an orbiting object as relative to the orbited object.
Coming soon: eccentric orbits!