Lunar Lander calculating gravity, acceleration and collision in GameMaker

Discussion in 'Programming/Internet' started by el_simmo, Aug 2, 2020.

1. el_simmoGuest

I'm making a little Lunar Lander game as my first foray in to GameMaker, I'm wanting to have the ship land and take off again too. I'm new to scripting and programming in general and I'm having trouble with acceleration, in particular with gravity.

Basically I need to:

• Be constantly pulled downwards to simulate gravity up to a maximum speed (terminal velocity)
• Activate thrusters to accelerate my ship towards its current direction
• Turn the ship left and right, therefore adjusting my current direction
• Land on surfaces (I've been accomplishing the collision by setting my gravity to 0) and then take off again (thereby activating gravity again)

The problem:

• I can't figure out a natural, correct way to accelerate the ship with gravity up to its terminal velocity
• I also can't figure out how to add the ship's thrust to gravity (i.e. if I'm facing downwards and firing the thrusters I should be going faster than if I wasn't firing the thrusters). As gravity is only pulling me down in the y direction and the thrust of the ship could be a force pushing me in any direction (according to my ship's angle) this is tricky (for me...).
• The whole thing doesn't feel like particularly smooth motion

An example of my code is as follows:

direction = image_angle

fallSpeed += grav

if fallSpeed > 2
{
fallSpeed = 2
}

y += fallSpeed

if keyboard_check(ord('W')) and speed < maxSpeed
{
shipThrust += 2/room_speed
}
else
{
shipThrust -= 2/room_speed
}

if shipThrust > 20/room_speed
{
shipThrust = 20/room_speed
}

if shipThrust < 0
{
shipThrust = 0
}

if place_meeting(x,y,oGround)
{
fallSpeed = 0
grav = 0
}
else
{
grav = 0.2
}

if keyboard_check(ord('A'))
{
image_angle += 3
}

if keyboard_check(ord('D'))
{
image_angle -= 3
}

speed = max(speed -0.1,0)

Some of that code is probably (read: clearly...) wrong and I figure I need an overall 'speed' value of the ship that I then affect with my two forces (gravity and thrust) but I can't for the life of me figure out how to do it. Any help is appreciated, thanks!