# How to take into consideration gravity when moving AI

I'm making a 2D game. There is currently a helicopter flying around, controlled by the player. It's controlled using the arrow keys: UP, LEFT and RIGHT.

It's speed along the y axis is dy, and speed along the x axis is dx.

It's physics is as follows:

Whenever UP isn't pressed, dy accelerates in constant acceleration, indefinitely towards down. (Gravity). dx stays in it's current value.

When UP is pressed, dy accelerates in constant acceleration from whatever it currently is, up to 4 (upward, until it reaches speed 4). dx stays in it's current value.

When LEFT is pressed, dx accelerates in constant acceleration, from whatever it currently is, up to -4.

When RIGHT is pressed, dx accelerates in constant acceleration, from whatever it currently is, up to 4.

(When LEFT or RIGHT are pressed and UP is not pressed simultaneously, as I said: dy increasingly becomes smaller and smaller, because gravity is affecting the helicopter)

All of this makes the helicopter often follow arches in the air, rather than straight lines.

This creates physics that seem quite realistic.

My question is:

The opponent helicopter, an AI, should move using the same physics system.

Lets say the AI wants to get from where it currently is, to point B. If there was no gravity and no gradual acceleration in the game, it would be easy. I would simply draw a vector from the AI's position to point B, and make the AI follow it.

But since there is gravity and gradual acceleration, the AI can never move in a straight line (almost). what would be the best way to make the AI go to point B, as time-efficiently as possible?

How can I take the gravity into consideration when moving the AI toward a specific destination?

(If it's easier to explain, please consider point B to be on the same level on the y-axis as the AI, and not in diagonal to it.)

Thanks

• Can you disable physics on the AI whilst it's moving? If so, then you could disable it while it's moving towards the point, and enable when it reached it. – Zhafur Jan 31 '14 at 9:10
• @Zhafur This will make the movement of the AI seem unrealistic, or at least difference from how the movement of the player seems. I want the movement of the AI to look the same as the movement of the player. – user3150201 Jan 31 '14 at 9:12

tl;dr:

TimeToStop.x = CurrentSpeed.x / Accelaration.x;

if (TimeToStop.x * CurrentSpeed.x >= 1.99 * DistanceFromTarget.x)
slow_down_x(); // CurrentSpeed += Acc.x * direction;
else
speed_up_towards_target_x(); // CurrentSpeed += Acc.x * direction;


Same with y. Remember to bound speed so it's between zero and the max speed. If the enemy has a very slow speed at some point and it"s trying to stop, it may begin to move in the opposite direction. Do not allow it. Stop it if it's slowing down and its speed is under 1 * Acc.

Long version: If there are no obstacles, movement on y-axis is completely irrelevant to (and does not affect) movement on x-axis. So the question you describe can be broken into two separate questions.

1. Moving there on x-Axis
2. And moving there on y-Axis.

CS.x & CS.y are our current speed on the x and y axises.

TS.x & TS.y Is the time it would take you stop only vertically or horizontally considering your current speed in the relevant axis.

D.x & D.y are the distance on each axis.

tl;dr:you continue to accelerate (if possible [unless you reached top speed]) on x-axis until you reach a spot where the following condition is true:

if (TS.x * CS.x >= 1.99 * D.x) hit_the_breaks_on_x();


The same with y.

One approach I've used for a similar problem was to first draw a vector to the target, then compare that to the current velocity direction.

So, take the signed angle between the velocity vector and the target vector and apply a thrust relative to the size of the vector.

So if the angle a between the red velocity vector and the green target vector is negative then apply positive thrust, otherwise apply no thrust and let gravity sort it out. Note that you'll have to treat the four quadrants where the target may be differently (as if the target had been in the upper left corner a positive angle would have required applying thrust).

Also, make sure to apply the amount of thrust relative to the deviation from the target velocity.

Technically this isn't taking gravity into account, it's compensating for the current state and it means the AI helicopter may not fly in a straight line to the target but will wobble a bit. This may or may not work for your game.

• Thanks. Several questions about this: 1- "...apply a thrust relative to the size of the vector". Did you mean, "size of the angle"? 2- By 'signed angle', you mean positive or negative - correct? If so, than an angle is positive if it's above the brown line, and negative if it's below the brown line. Right? 3- If I understand correctly, what you mean is: Every frame, get the angle between the current velocity vector and the current vector to the target. Then add/subtract from dx and dy accordingly. Is this correct? – user3150201 Jan 31 '14 at 11:36
• @user3150201 Yes on the angle question. Yes on the signed/unsigned question, atan2 can give you the signed angle between two vectors. It looks like you understood my point, apologies if I was unclear. – bornander Jan 31 '14 at 13:08

You could simply recalculate the movement each X frames. Assuming you don't put too many frames between, but enough so it doesn't affect performance, the gravity on a helicopter shouldn't be strong enough to really see the trajectory changing. Plus, I think a player could possibly move the same way, adapting to the gravity while moving and not having it planned.