# Lunar Lander thrust code

Hi all

I just wanted to thank everyone who helped me with the problem I had of
moving my spaceship, got it figured out. Now, I ask for some tips on how
to simulate the Lunar Lander games thrust routine.

Basically, I assume I will need to alter the ship’s velocity such that
when the up key isn’t pressed, the ships y velocity vector is something
like this:
case SDLK_UP
yvel+=grav // where yvel is the ship’s y velocity and grav is a
gravitational constant

I have it setup now such that if no key is pressed the ship falls off
the bottom of the screen, but when I try to press the up arrow key to
slow it down, nothing really happens.

Thanks again,
Don

Hey donald, it’s even simpler than that, here’s what you do…

declare 2 variables float dx,dy;

dx is the ships momentum in the x axis, dy is the momentum in the y axis.

start the ship at the top of the screen (in middle or whatever) and set dx
and dy to 0.

within your main game loop, you do something like subtract 0.15 from dy (a
constant i pulled out of no where, gonna take some playing around to get the
right number for the amount of gravity you want).

if left is pressed , subtract 0.2 from dx
if right is pressed, add 0.2 to dx
if up is pressed, add 0.2 to dy
if down is pressed subtract 0.2 to dy

you can also subtract fuel for each case if you have a fuel gauge, as well
as display the appropriate animation for each thruster.

then in your game loop you do this…

shipx+=dx;
shipy+=dy;

that will move your ship each frame by the appropriate momentum.

then you can do something like lets say the horizon is at pixel 10 on the y
axis, you can do something like this…

if shipy<10
{
//the ship has hit the ground, so its time to test if they hit it hard or
soft or if they landed on the pad or not etc

if(dy<1.0)
{
//they hit the ground too hard and should blow up
}

if (shipx>10 and shipx<50) //did they land on the pad?
{
they win! go to next level or whatever
}
else
kaboom! they landed on rock and blew up
}

you can get alot fancier by having rugged terrain and not just a flat planet
to land on but this should be a good start, hope it helps!> ----- Original Message -----

From: dcathcart@ns.sympatico.ca (Donald Cathcart)
To:
Sent: Sunday, October 17, 2004 4:09 PM
Subject: [SDL] Lunar Lander thrust code

Hi all

I just wanted to thank everyone who helped me with the problem I had of
moving my spaceship, got it figured out. Now, I ask for some tips on how
to simulate the Lunar Lander games thrust routine.

Basically, I assume I will need to alter the ship’s velocity such that
when the up key isn’t pressed, the ships y velocity vector is something
like this:
case SDLK_UP
yvel+=grav // where yvel is the ship’s y velocity and grav is a
gravitational constant

I have it setup now such that if no key is pressed the ship falls off
the bottom of the screen, but when I try to press the up arrow key to
slow it down, nothing really happens.

Thanks again,
Don

SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

in the last chunk of code i made a typo, instead of this:

if(dy<1.0)
{
//they hit the ground too hard and should blow up
}

it should be:

if(dy<-1.0)
{
//they hit the ground too hard and should blow up
}

all the constants in the pseudo code are pulled outa no where and you should
play around with the numbers til you get the results you want (:> ----- Original Message -----

From: @atrix2 (atrix2)
To: "A list for developers using the SDL library. (includesSDL-announce)"

Sent: Sunday, October 17, 2004 5:13 PM
Subject: Re: [SDL] Lunar Lander thrust code

Hey donald, it’s even simpler than that, here’s what you do…

declare 2 variables float dx,dy;

dx is the ships momentum in the x axis, dy is the momentum in the y axis.

start the ship at the top of the screen (in middle or whatever) and set dx
and dy to 0.

within your main game loop, you do something like subtract 0.15 from dy (a
constant i pulled out of no where, gonna take some playing around to get
the
right number for the amount of gravity you want).

if left is pressed , subtract 0.2 from dx
if right is pressed, add 0.2 to dx
if up is pressed, add 0.2 to dy
if down is pressed subtract 0.2 to dy

you can also subtract fuel for each case if you have a fuel gauge, as well
as display the appropriate animation for each thruster.

then in your game loop you do this…

shipx+=dx;
shipy+=dy;

that will move your ship each frame by the appropriate momentum.

then you can do something like lets say the horizon is at pixel 10 on the
y
axis, you can do something like this…

if shipy<10
{
//the ship has hit the ground, so its time to test if they hit it hard
or
soft or if they landed on the pad or not etc

if(dy<1.0)
{
//they hit the ground too hard and should blow up
}

if (shipx>10 and shipx<50) //did they land on the pad?
{
they win! go to next level or whatever
}
else
kaboom! they landed on rock and blew up
}

you can get alot fancier by having rugged terrain and not just a flat
planet
to land on but this should be a good start, hope it helps!

----- Original Message -----
From: “Donald”
To:
Sent: Sunday, October 17, 2004 4:09 PM
Subject: [SDL] Lunar Lander thrust code

Hi all

I just wanted to thank everyone who helped me with the problem I had of
moving my spaceship, got it figured out. Now, I ask for some tips on how
to simulate the Lunar Lander games thrust routine.

Basically, I assume I will need to alter the ship’s velocity such that
when the up key isn’t pressed, the ships y velocity vector is something
like this:
case SDLK_UP
yvel+=grav // where yvel is the ship’s y velocity and grav is a
gravitational constant

I have it setup now such that if no key is pressed the ship falls off
the bottom of the screen, but when I try to press the up arrow key to
slow it down, nothing really happens.

Thanks again,
Don

SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl