# Pong Ball movement

This topic is 4970 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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) + oldY
oldX = 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;

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

1. 1
Rutin
24
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 19
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631760
• Total Posts
3002176
×