# Moving one object from one point to another with acceleration

How would someone implement moving an object from one point in 2D space to another using these formulae to update objects:

m_position.setX/Y( m_position.X/Y() + m_velocity.X/Y() * dt + 0.5 * m_acc * pow(dt,2);
m_velocity.setX/Y(m_velocity.X/Y() + m_acc * dt);


I'm trying to make a pong clone and I'm having some trouble programming the AI paddle to go to the y coordinate where the ball will hit. (the y coord is predicted in a function).

Currently I have it so that it applies an acceleration of 1 until half the distance then an acceleration of -1 after it, but naturally it is overshooting by a lot, then trying to go back, etc.

• You can try Easing the paddle into place. You can also dynamically control the paddle using at PID controller. – mklingen Mar 25 '15 at 14:52
• @mklingen Easing, respectively tweening, could be exactly what I need. I'll take a closer look into them. Thank you! – Cadhylo Mar 25 '15 at 16:00
• Why are you integrating acceleration twice? The numerical integration is fine, but the additional symbolic integration does not hold if m_acc is not constant. Even if acceleration never changed, you will still be doubling it. – MickLH Jan 22 '16 at 22:03

This is a job for a PID Controller:

Proportional Integral Differential Controller.

Consider this: if you are close to the goal, how hard do you push? It all depends on the velocity. If you are not moving, you push towards the goal. Yet, if you are already speeding towards the goal, you need to push in the opposite direction so you don't overshoot.

The PID controller will take an actual value (current pos) and a desired value (goal pos) and gives you a steer value.

This steer value is used to apply force to the paddle (or in other terms: accelerate/decelerate the paddle.)

This means you need to give your paddle two pieces of state: a position and a velocity.

Each frame you do:

accel = pidcontrol( desiredpos, actualpos, dt );
vel = vel + dt * accel;
pos = pos + dt * vel;


To tune the controller, you need to tune P, I and D constants associated with the controller.

I wrote a Primer on PID Control that explains it in detail, and comes with code too.

Your velocity is 0 when the ball stops, so the velocity must be 0 at the point where it would "overshoot" (when there's a direction change required in the paddle).