I'm thinking of making a lander game, where you control a spaceship and need to land it without crashing. What is a simple formula to calculate speed of falling or acceleration with relation to working time to time engines?
3 Answers
The simplest way is the Euler integration. You have to store position vector and velocity vector. At each frame:
- measure the time passed since the last integration step: dt
- compute the force vecor due your engines: F
- compute the acceleration vector: A = F/m where m is the spaceship mass.
- add the gravity vector: A = A + G make sure that G points toward the planet's center
- update the velocity vector V = V + A·dt
- update the position vector X = X + V·dt
(x for scalars, X for vectors)
be sure that dt is small...
|G| is about 9.8 m/s² for earth and about 1.6 m/s² for moon
in general the actraction Force due the gravital interaction is:
It affects each body and it points toward the other one.
The G scalar is the very famous Gravitational constant it is about 6.67e-011 N(m/Kg)²
Since you are interested in acceleration:
You only need to know the planet's mass (m2) and radius (r) to compute your acceleration.
Typically the acceleration that moves the planet toward your spaceship is negligible because usually m1 is negligible compared to m2.
However, if you are trying to land to a small asteroid you probably have to use the general formula adding that force to the total force vector in the second step.
EDIT:
As required some hint on implementation. You will need:
- A vector library
- Engine Model
- Phisics Model
- Collision Detection
- User Interface (input and graphic rendering)
First of all the vector library: your game can be mono/bi/tree/four... dimentional, as far as you consider your case to be a projection of a 3D word, the physical roules hold.
If n is the dimension you choose (probably 2 or 3 in your case), the library must have:
- a vector storage entity (a list of n floating point numbers for each vector)
- a sum operator (sum component by component)
- a scalar multiplication operator (each component multiplied by a float)
- a dot-multiplication between vectors (multiplicate component by component and sum up all)
- the vector length (square root of a vector dot-mutiplied by itself)
You can use a library that does this or implement one by yourself; a vector can be a struct or a class, the choice is yours.
Each engine should be described by:
- a vector indicating its thrust strength and direction
- a scalar that indicates the fuel usage per second when on full power;
your user input will be used to provide to each engine a number that will be between 0 (unused engine) and 1 (full power): the engine (usage) factor.
Multiply the engine factor for its thrust vector to obtain the engine real trust and sum up all the results of all the available engines; this will give you the F of the second step.
Your engine factor can be used to know the real fuel usage for each engine: multiply the engine factor by the fuel usage and by dt to know the instantaneous fuel usage; you can substract this value from the total fuel capacity variable (this gives you the opportunity to update your total mass m if the fuel mass is considerable).
Now you can proceed using the integration to compute the new position, check for the collision with your planet surface; if any, use the length of the velocity vector to say if the landing was a success or a disaster.
Obviously other collision checks can/should be made, some surface entities cant be allowed as landing point so every collision is a fatal one.
I leave how to get input and how to render your spaceship to you; you can use the engine factor to render the engine status frame by frame, for example.

- 3,996
- 20
- 20
-
That is great, but some ideas how to impelement it in code would be great. – Dvole Jul 14 '11 at 18:39
-
1where I learned physics http://www.shiffman.net/teaching/nature/ – Ming-Tang Jul 14 '11 at 20:49
-
This guy knows his Physics! – MGZero Jul 15 '11 at 13:49
-
@Ming-Tang Dead link. – Pharap Dec 20 '22 at 23:42
As the other excellent answer seems a bit theoretical, here's the simple code version:
// Position of the lander:
var positionX = 100.0;
var positionY = 100.0;
// Velocity of lander
var velocityX = 0.0;
var velocityY = 0.0;
// Acceleration due to gravity
var gravity = 1.0;
// If the player is pressing the thrust buttons
var isThrusting = false;
var isThrustingLeft = false;
var isThrustingRight = false;
// Thrust acceleration
var thrust = -2.0;
// Vertical position of the ground
var groundY = 200.0;
// Maximum landing velocity
var maxLandingVelocity = 3.00;
onUpdate()
{
velocityY += gravity;
positionX += velocityX;
positionY += velocityY;
if (isThrusting)
{
velocityY += thrust;
}
if (isThrustingLeft)
{
velocityX += thrust;
}
else if (isThrustingRight)
{
velocityX -= thrust;
}
if (positionY >= floorY)
{
if (velocityY > maxLandingVelocity)
{
// crashed!
}
else
{
// landed successfully!
}
}
}

- 6,518
- 3
- 33
- 46
-
2Keeping it simple. I expect this is along the lines of what he was looking for. – Beska Jul 20 '11 at 20:34
Unfortunately, the math here gets hairy. FxIII's answer is fine for the general case of a falling object but you are talking about a rocket--and rockets burn off fuel.
I have seen code that does it but it was completely undocumented and I never managed to figure out the math behind it. Unless you're CPU-limited somehow I wouldn't bother and simply brute force it--FxIIIs approach applied on a quite short timescale and adjust the thrust (or fuel use if you figure the rocket throttles back as the fuel burns off to maintain the specified acceleration rather than the specified thrust) between each iteration as the rocket burns off fuel.

- 992
- 4
- 8
-
Going out of fuel just represents a falling parameter. It's simply a vectorial force with decreasing value. Am I wrong? In a simple case you have G against F, where F is the engine of the rocket and G the earth's gravition: In this case you simply have to measure both against each other, so if no fuel is left F falls to 0 so you only have to apply G as vectorial force to your object. Works for every value of F. As long as F >= G the object "should" move in direction of F. – daemonfire300 Jul 14 '11 at 23:56
-
2@daemonfire300: You misunderstand--I'm not talking about the effect of running out of fuel, but the effect of burning fuel. A constant throttle setting on your engine produces a continuously climbing acceleration figure, a constant acceleration produces a continuously declining fuel burn rate. This means the simple calculations that FxIII gives will produce incorrect fuel use numbers. – Loren Pechtel Jul 15 '11 at 02:20
-
The amount of acceleration gain from A=F/M as fuel is burned is going to be a negligible value unless either the proportion of fuel mass to vehicle is absurdly high or fuel burn rate is similarly high. This is a game, after all. – Patrick Hughes Jul 16 '11 at 00:22
-
@Patrick Hughes: The amount of fuel carried by any true rocket lander (I'm not counting the Mars aerobrake/rocket hybrid cases) is a substantial portion of it's total weight. Assuming the fuel:acceleration ratio to be fixed will produce a very wrong answer. – Loren Pechtel Jul 16 '11 at 01:25
-
@Loren Pechtel I hadn't imagined, I guess energy density is a trouble to more than just cars =) Thanks for the info. – Patrick Hughes Jul 16 '11 at 08:51
-
Obviously as time (t) passes [whilst engines running] the mass (m) will decrease; therefore the following
A = F/m
equation will still hold an increasing acceleration vector. The burn rate should only affect mass (m, negatively) and the engine force (F, positively) for positive values. I think your thinking about it wrong, why try and derive the fuel usage from this, the fuel usage should be what determines your force and resultant mass. – hiddensunset4 Jul 16 '11 at 13:39 -
@Patrick Hughes: Shuttle gross weight: 240,000lb. Total liftoff weight: 4,500,000lb. Admittedly, that includes 58,500 pounds of tank & 400,000lb of SRB. Still, though, you'll get more than 10x the acceleration from the last pound of fuel than from the first. – Loren Pechtel Jul 16 '11 at 18:20
-
@Loren: True...but is that something he'll want to worry about in the sim, which sounds like a simple game? Most games don't worry about fuel weight, because it adds complexity for not much added fun. Don't get me wrong, the point is very valid...I'm just saying that it should be evaluated as to whether the added realism will add anything. – Beska Jul 20 '11 at 20:33
-
@Beska: Your rocket engine isn't limited in acceleration, it's limited in thrust. The ratio of thrust/acceleration will change as the tanks get used. A rocket with full tanks is a lot less responsive than one that's nearly empty. – Loren Pechtel Jul 20 '11 at 23:21
-
@Loren: True. My point remains. Will the added complexity add anything to the game? If the rocket seems horribily unresponsive when the user starts out, it may be more realistic, but less fun. No point in adding in the extra work if there's not a clear win in the entertainment. (If he's creating a realistic "simulation", then probably he should do the work. If he's trying to get his feet wet with a simple coding project, the answer is probably "skip it.") – Beska Jul 21 '11 at 14:44
-
1This is not an answer to the question, just a comment on @FxIII's answer. – Jonathan Connell Jul 26 '11 at 12:49