1
\$\begingroup\$

I am writing a top-down 2d game, in which the player can control an airplane via the WASD keys. The W key should move the plane upwards (north), the A key to the west, and so on.

Now I need a simplified aerodynamics model to control the flight direction and speed of the plane. Pressing W should accelearte the plane a bit to reach full speed, then while the player continues to press W the plane should continue to move upwards on the screen (remember, top-down view).

When the player releases the W key and presses the D key now, the plane should behave similar to the real world. That is, it must not immediately move to the right (east), instead it should continue to fly to the north, but gradually start to move to the east. Then if the player continues to press the D key, the plane eventually has to fly solely to the east. So all in all in this example the airplane has to fly a curve from north to east.

In general the plane must not change its direction immediately and directly when the player presses another key, but instead fly using a simplified inertia (?) model. This should of course also work correctly if the player presses two keys at the same time, for example W (north) and A (west).

How would I achive this general in a 2d top-down game?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Assuming you are not going for a full out physics simulator:

Your plane needs to have a position, velocity, direction(angle), acceleration and various maximum values. On each game step:

velocity += acceleration * dt;
velocity = clamp(velocity, 0, max_velocity);
acceleration = 0;
position += velocity * dt;

On (de)acceleration:

acceleration = getVecFromMagnitudeAndDirection(accelerationRate, direction);

On moving north/s/e/w:

direction += (desired_direction - direction)*rotationRate;
  • note that you probably want a calcAngleDifference function to handle various edge cases.
  • pressing multiple keys etc just requires that you set the desired_direction correctly.
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .