Sign in to follow this  
Joshnathan

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


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


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


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this