# Specific path for ai for follow

I have an entity's following details:

• x, y

• vx, vy

• angle to x axis

• its target

I want to reach the target by a parabolic path, how to implement an ai that follow a particular path from any given point to another like parabolic or sine wave.

• Just add some control points on the path, so the entity has intermediate targets, simulating the curve you want – realUser404 Dec 12 '16 at 10:04

## 3 Answers

Any quadratic Bézier curve traces out a parabola, so we just need three control points. We already have two - the object's initial position and its target, so we need just one more.

To ensure continuity with the object's initial position, we can place the middle control point along the line (x + u * vx, y + u * vy) for any positive coefficient u we like.

Then we can plot the complete path from the start position P, via this middle control point M, to the target point T by varying the parameter s from 0 to 1:

$$p(s) = (1-s)^2 P + 2s(1-s)M + s^2 T$$

We can also think of this parabola as a ballistic trajectory under a constant acceleration a. over a time duration t = 2u, using the value of u we picked above to select the middle control point M.

The constant acceleration vector ais then:

$$a = \left(T - \left(P + t * V\right)\right)\frac 2 {t^2}$$

(Where V is the initial velocity)

This is based on the Bézier constructions used in this previous answer about acceleration planning.

Take a look at my answer here

With this basics of letting an Object follow a path, you just need to create an animation curve containing your specific shaped path and let the Object follow it. Just fill the animation curve with keyframes "by hand" or copy the values of e.g. the Mathf.sin function into it.

You can iterate the next point to move to by just passing the time as index (needs to be converted to integer) for the next keyframe of the animation curve. More keyframes smoothen the movement and makes the object follow your curve more accurately.

• Actually I am not developing any game but was practising in a codingame AI dev contest I.e. FB (fantastic bits) in which we can just output commands like move x y to move our player. So I wanted an efficient technique of how to make a strategy that I move parabolically to my target or in any specified. Thanks for the answer though. – Deep Devi Dec 15 '16 at 7:41
• Sorry, I might got you wrong. I thought this was related to the Unity3D, which my answer relies on. – Artery Dec 15 '16 at 12:14

The term "parabolic" is confusing here. I will assume that you just mean that the movement has to be smooth, without hard discontinuities.

If you properly steer your NPC, their routes will automatically be curve-like and smooth. What is proper steering? You need to steer them doubly-indirect.

This means:

1. you do not set their positions directly.
2. You do not even set their velocities directly.
3. You set an acceleration (or a force on their physics body.)

The force/acceleration will lead to a change in their velocity, which will lead to a change in their position.

Making an NPC reach a certain location in the world then becomes a hard task: you need to steer towards their goal, if still far away from it. When near, you need to steer in the opposite direction to slow them down, otherwise they will overshoot the target location.

The best tool to steer your NPC around a world by settings forces, is the Proportional Integral Differential Controller, or PID Controller.

I wrote a primer on PID Control that will get you up to speed. It includes code.

Personally, I use two PID controllers per NPC. One PID controller to move fw/bw, and a second PID controller (angular) to turn L/R.