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?
The simplest way is the Euler integration. You have to store position vector and velocity vector. At each frame:
(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.
As required some hint on implementation. You will need:
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:
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:
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.
As the other excellent answer seems a bit theoretical, here's the simple code version:
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.
|show 6 more comments|