# Pong horizontal movement algorithm

I was just wondering about the horizontal movement of a pong ball? What is the general algorithm used with this? The ball in my pong game just move vertically and I don't know about the algorithms used for for horizontal movement. Thanks in advanced!

## 1 Answer

You have two speeds here: - ball speed (x,y) - paddle speed (x)

Ball speed is constant (positive or negative) by Y axis while speed on X axis is set depending how fast is the paddle while you hit the ball.

Simple calculation could be:

// this is called when ball hits the paddle
function OnCollision()
{
BallSpeed.x = PaddleSpeed.x * 0.5f; // 0.5f = custom factor
}

// you calculate new ball position here every frame
function OnUpdate(float dt)
{
BallPosition.x += BallSpeed.x * dt;
BallPosition.y += BallSpeed.y * dt;
}


Of course, you could also calculate X speed depending which part of the paddle it hit (if it was far edge, X speed is bigger). Usually both parameters are applied in pong games. Let me know how it went.

• Worked great. Although from what I see there is no real reason to make the ball go as fast as the paddle, but with little adjustments I was able to make it tuned for my preference. Anyways thanks again! – LiquidFeline Apr 15 '13 at 10:33
• I'm glad it worked. Yes, that's why I added that 0.5 factor so it's not as fast / slow as the paddle. Good luck with the project! – Blodyavenger Apr 15 '13 at 10:39
• bear in mind that in the actual game, distance from the center of the paddle also affects ball speed. – Benjamin Danger Johnson Apr 16 '13 at 0:14