0
\$\begingroup\$

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 moves vertically and I don't know about the algorithms used for horizontal movement.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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! \$\endgroup\$ Apr 15, 2013 at 10:33
  • \$\begingroup\$ 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! \$\endgroup\$ Apr 15, 2013 at 10:39
  • \$\begingroup\$ bear in mind that in the actual game, distance from the center of the paddle also affects ball speed. \$\endgroup\$ Apr 16, 2013 at 0:14

You must log in to answer this question.

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