Jump to content
  • Advertisement
Sign in to follow this  
Joshnathan

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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)

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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;

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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!