views:

249

answers:

6

I'd like to build a game to learn cocos2d. Lunar lander is the first exercise coming in my mind. Any pointer/source code/tutorial of the physics calculations required will be appreciated. Thanks!

+6  A: 

The math you need for a lunar lander game is pretty straightforward. Newton's Laws of Motion are all you really need - just pick up a basic physics textbook. You should be set after the first chapter. There are only two force inputs in the system - gravity and thrust from the engines. Just calculate the vertical & horizontal components of the motion, and animate your spaceship accordingly.

Carl Norum
+4  A: 

The physics are very simple: http://csep10.phys.utk.edu/astr161/lect/history/newtongrav.html

I assume you won't be worrying about drag or wind, so depending on your angles of inclination (user input), you'll be implementing:

alt text

Sourced from: http://en.wikipedia.org/wiki/Trajectory. You can even probably get away with simplifying it. If you don't want to be super-accurate, you can just do something like F=ma where is is whatever you decide the gravitational acceleration to be (9.8 m/s² on Earth).

David Titarenco
No ability to fire thrusters to counteract gravity in this equation. That's the problem with canned solutions: no context, no explanation, no way to tell if it's appropriate or not.
duffymo
Actually, that's why I linked the Trajectory page and pasted the function. You use that function to fire thrusters at arbitrary angles (theta). The math should work out quite wonderfully. Am I wrong?
David Titarenco
Didn't bother reading the link - no time. Couldn't tell if theta was thruster angle or initial angle of descent. Don't see any time stepping capability or turning thrusters off or on. I think a more numerical approach is needed - integrating multiple coupled ODEs. See below.
duffymo
+1  A: 

If your game is in 2D, You don't need much math, you need physics, Specifically basic Newtonian motion. Probably intro college or late high school. The math is some grade school algebra with early high school calculus.

If you look at up-down motion, then your ship is essentially an object that is exposed to a force of gravity (the constant depends on your "moon") negated by the force emitted by its engines. You can use that to determine acceleration and from there velocity. Using the velocity, you can do your collision-outcome. The left-and-right motion is easier, since if your moon has no atmosphere, you are merely applying a constant force.

If you want something more realistic, you can modify the gravity constant based on distance from the surface, and can add an atmospheric friction force (though it wouldn't really be our moon).

If your game is in 3D, and your ship has side thrusters in addition to bottom thrusters, then you would not only have motion in location but also rotation. That has to do with rigid body physics. AFAIK that involves college level calculus.

Uri
But you do need math for physics, and I think Newton would agree with me. :)
Robusto
+4  A: 

You'll need stuff like this:

  1. Newton's laws of motion in 2D.
  2. Ability to change effect of gravity. 9.8 m/s^2 is the right acceleration on earth, but you should be able to change this to the appropriate value for Mars, moon, Jupiter, etc.
  3. Ability to turn thrusters off and on to counteract the effect of gravity. Not a very interesting game if you don't, because every one ends in a crash.
  4. Way to relate duration of thruster fire with fuel consumption. If you don't manage fuel well you crash.
  5. Initial conditions (e.g., height above surface, initial velocity, initial fuel, etc.)

You'll start with initial conditions and loop over a number of time steps. At the end of each step you'll check the position and velocity. If the y-position above the surface is zero or negative you'll have landed. If the velocity is greater than a critical y-value you'll have a crash; less than the critical value means a safe, soft landing.

You'll solve Newton's equations of motion numerically. In your case it's four coupled, first order ordinary differential equations: rate of change of velocity in x- and y-directions and rate of change of position in x- and y-directions. If you have the thrusters in place you'll add another equation for conservation of mass for the fuel.

You can eliminate two equations if you assume that there are no x-components: the lunar lander moves perpendicular to the surface, the thruster force only has a non-zero component in the vertical direction. If that's true, you're down to three equations.

You'll do time stepping, so it'll be good to read up integration techniques like explicit Euler or implicit 5th order Runge-Kutta.

A challenging problem - not trivial. Good luck.

duffymo
+1  A: 

This may be overkill, but I recommend looking at Numerical Recipes -- read the chapter on ordinary differential equations. You don't even need to study the entire chapter; just the first couple of sections.

comingstorm
A: 

In two dimensions, on every time tick you want to add the ship's rotational thrust to its rotational velocity, add its rotational velocity to its current heading, compute a thrust vector by multiplying the sine and cosine of its heading by its main thruster output, add that vector and a gravity vector (a straight downward vector of some magnitude) to its current velocity, and add its current velocity to its position. If the timer ticks are small enough, that's pretty much all you have to do, other than check to see if the craft is in contact with the ground. Experiment with the magnitude of your thrust and gravity values until you have a playable game.

supercat