[Math] calculation to change a helicopter’s vertical position, adjusted with gravity

physics

This is my very first question, so don't be too hard on me 🙂

I am a beginner programmer, and I am working on the typical helicopter game, working with a one-button push:

When the button is pushed, the helicopter ascends, starting ascending slowly, accelerating upwards.
When the button is released, the upward acceleration slows down, then the helicopter starts falling.

This calculation would run in a loop.
As a result, I would have to get a floating point number between 0 and 1, which would correlate with the vertical position of the helicopter on screen.

What I have so far is very wrong, but I post it here anyway:

newPosition = (oldPosition * gravity) + (acceleration * sensitivity)

where gravity and sensitivity are constant. Acceleration is the length of the button push, which becomes 0, as soon as the button is released.

Of course, this is wrong, because this way the helicopter descends quickly first, then slows down as it gets closer to the ground (to value 0).

Any help would be greatly appreciated — as you can see, I am not a math genius. It would also be great to get some help on how to approach these calculations, so I could come up with similar calculations in the future.

Best Answer

Ah, the old helicopter game. No sweat, it's actually very simple. But because you're modelling acceleration, you really do want a variable which represents velocity. Then you can just do this:

every frame {

    if(mouse down) {
        velocity += [gravity constant];
    } else {
        velocity -= [thrust constant];
    }

    position += velocity;

}

Without a velocity variable, it's very tricky to get the effect you want.

Related Question