Pong Ball movement

Started by
14 comments, last by Think128 19 years, 4 months ago
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)?
-----------------------------Sismondi GamesStarted c++ in October 2004...
Advertisement
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)

Drew Sikora
Executive Producer
GameDev.net

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
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Yea, that's it [smile]

Drew Sikora
Executive Producer
GameDev.net

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?
-----------------------------Sismondi GamesStarted c++ in October 2004...
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.

Drew Sikora
Executive Producer
GameDev.net

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;

and in your gameloop:
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
"1 is equal to 2 for significantly large quantities of 1" - Anonymous
lol, spent 3 days on it already... :D
-----------------------------Sismondi GamesStarted c++ in October 2004...
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.
-----------------------------Play Stompy's Revenge! Now!
thanks a lot for your reply, it was VERY helpfull. I now have this function:
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
-----------------------------Sismondi GamesStarted c++ in October 2004...

This topic is closed to new replies.

Advertisement