Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
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! –  CPP_Person 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.