# Pong Ball movement

## Recommended Posts

hi all, I am trying to make my ball in my pong game to move. It is doing fine, but how do I make my ball move smoothly? this is my code:
void MoveBall()
{
if(ballX < targetX)
ballX += ballspeed;
if(ballX > targetX)
ballX -= ballspeed;
if(ballY < targetY)
ballY += ballspeed;
if(ballY > targetY)
ballY -= ballspeed;
}


but as the ball reaches my targetX sooner then my targetY, from the moment he reaches targetY, he goes straight forward. How do I make him gradually go to the point (targetX, targetY)?

##### Share on other sites
This is simple with a bit of algebra and graphing. You have two points, start x,y and end x,y. Find the slope of the line between them and then assign the rise to delta y and the run to delta x. Bingo.

(sorry, I haven't done any serious algebra for god knows how long so I have no forumulas memorized anymore :P)

##### Share on other sites
actually we just got done covering this i think the formula you need is
Y=mx+b
(X,Y),(X2,Y2) <-------ordered pairs or points

m(slope) = (Y2-Y)/(X2-X)

Y2 = mX2 + B(y-intersept)

B = Y2 - (mX2)

hope i helped

##### Share on other sites
Yea, that's it [smile]

##### Share on other sites
lol that sounds complicated (even more cause I just came out of bed). before I went to bed I thought about something like:
void ChangeTarget(){targetX = (oldX - targetX) + oldX;targetY = (oldY - targetY) + oldYoldX = targetX;oldY = targetY;}

offcourse at the start I set oldY & X = to targetY & X. would this work?

##### Share on other sites
Well I'm just going to bed, so I can't make head or tails of that myself. However if you're scared off of the math because it's algebra well... tough. (I'm kinda grumpy too, sry). You're going to need lots of algebra if you want to do any serious graphics programming (not so much programming in general, but yea it helps there too). Have you had any algebra? if not you could prob Google the above formulas and learn all you need to know for this application at least. Just do it. it works, no sense reinventing the wheel.

##### Share on other sites
No need for algebra here.

For 'smooth' movement, just use floats to store your balls coordinates, and floats for the velocity.

eg:
float bx=0.0,by=0.0;
float vx=0.1,vy=0.2;

bx += vx;
by += vy;

Then just draw the ball as usual (motion will still be *slightly* jerky, as your ball is likely only drawn to the nearest pixel).

This allows you to have very very slow speeds, and still update the positions many times a second.

Anyway, its pong, I don't want to have to write more lines in this response post then it takes to code the thing! :)

- Jacob

##### Share on other sites
lol, spent 3 days on it already... :D

##### Share on other sites
just have two speeds, one for x-axis, one for y-axis.

than when the ball hits a paddle, reverse the speed on the x-axis and when the ball hits the top or bottom of the screen, reverse the speed on the y-axis. Like this:

void HandleBallMovement(void){    ballX += xspeed;    ballY += yspeed;    if(ballX == PaddleX1 || ballX == PaddleX2)       xspeed = -xspeed;        if(ballY <= 0 || ballY >= SCREENHEIGHT)       yspeed = -yspeed;}

This is what I used when I made pong and it worked just fine.

##### Share on other sites
void Movement(){        while(!GameOver)    {    SDL_Event event;            while(SDL_PollEvent(&event))        {            switch(event.type)            {                case SDL_QUIT:                   GameOver = true;                   running = false;                   break;                case SDL_KEYDOWN:                    switch(event.key.keysym.sym)                    {                        case SDLK_UP:                              Movement(-1);                            break;                        case SDLK_DOWN:                          Movement(1);                        break;                        case SDLK_LSHIFT:                             speed++;                            break;                        case SDLK_LCTRL:                            if(speed < 1)                            {                            speed--;                            }                                break;                        case SDLK_ESCAPE:                            GameOver = true;                            game = false;                            break;                   }                    break;             }            SDL_FillRect(screen, NULL, 255);            DrawGame();            MoveBall();            DrawBall();            SDL_Flip(screen);        }           }     GameOver = false;   }

and it works fine. The only problem I have is that he ball won't move if there is not INPUT. I try'd adding a "case NULL:", but it didn't work either ("default:" didn't either). any idea of how I could make the ball move without having to wait for input?
Thank you very much in advance,
Joshua

##### Share on other sites
I don't really know much about SDL, but shouldn't your code to move and draw the ball be located outside the event handler?

The same problem happened to me when I was making pong for Windows. It was because I put the code in the message handler of WinMain.

Anyway, try putting the code to move and draw the ball outside the event handler.

##### Share on other sites
thanks a lot, it works! :D I almost finished my first pong hehe

##### Share on other sites
I remember making pong on my graphing calculator in physics class using its half-baked BASIC language. I used the algebra.

##### Share on other sites
you mean that you actually sat there and coded on a graphing calculator...
ummm by the way how did you do that?.....I'm having trouble with storing and returning variable values.

##### Share on other sites
The issue of drawing an line was already solved by:

The Bresenham Line-Drawing Algorithm
http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html

There is no need for reinvention!

##### Share on other sites
<I'm having trouble with storing and returning variable values.>
To store a value: value -> variable
I can't remember the displaying. It's either: print "variable" -or-
disp "variable"

I'll try to dig up the code. It was unreliable last time I looked at it though, some glitch was happening and their wasn't enough pixels to actually make it interesting. I might take another look at it and remake it.... I'll get back to you later.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627689
• Total Posts
2978648

• 13
• 14
• 12
• 10
• 12