Sign in to follow this  
chosenkill6

Pong Ball not bouncing

Recommended Posts

I am working on a Pong Clone as my first big game in SDL and i am so close yet so far. I cant seem to get the ball to bounce back and forth between the two paddles yet the code i have writtem looks like it should do just that. I have been stuck on this for about a week :/

Maybe some of the more experianced programmers could help me out. Here is the function that i call in my game loop:
[code]
void move_ball()
{
int left_collision = 0,right_collision = 0;
float left_top, left_middle,left_bottom,right_top,right_middle,right_bottom;
bool left_up,left_down,right_up,right_down;

left_collision = SDL_BoundingCollide(pong_ball.image,pong_ball.x,pong_ball.y,left_paddle.image,left_paddle.x,left_paddle.y);
right_collision = SDL_BoundingCollide(pong_ball.image,pong_ball.x,pong_ball.y,right_paddle.image,right_paddle.x,right_paddle.y);

left_top = left_paddle.y;
left_middle = left_top + 50;
left_bottom = left_middle + 50;

right_top = right_paddle.y;
right_middle = right_top + 50;
right_bottom = right_middle + 50;

if(left_collision == 1)
{
if(pong_ball.y > left_top && pong_ball.y < left_middle)
{
right_up = true;
right_down = false; left_up = false; left_down = false;
}
if(pong_ball.y > left_middle && pong_ball.y < left_bottom)
{
right_down = true;
right_up = false; left_up = false; left_down = false;
}
}
if(right_collision == 1)
{
if(pong_ball.y > right_top && pong_ball.y < right_middle)
{
left_up = true;
left_down = false; right_up = false; right_down = false;
}
if(pong_ball.y > right_middle && pong_ball.y < right_bottom)
{
left_down = true;
left_up = false; right_up = false; right_down = false;
}
}

if(left_up == true || left_down == true || right_down == true || right_up == true)
{
if(left_collision == 0 && right_collision == 0)
{
if(left_up == true)
{
pong_ball.x -= pong_ball.xVel;
pong_ball.y -= pong_ball.yVel;
}
if(left_down == true)
{
pong_ball.x -= pong_ball.xVel;
pong_ball.y += pong_ball.yVel;
}
if(right_up == true)
{
pong_ball.x += pong_ball.xVel;
pong_ball.y -= pong_ball.yVel;
}
if(right_down == true)
{
pong_ball.x += pong_ball.xVel;
pong_ball.y += pong_ball.yVel;
}
}
}
}
[/code]
I would really appreciate help. Usually i dont like to post lots of code as i prefer to solve things by myself, but i need to get this game done, so please advise as to what i should do.
Thanks

Share this post


Link to post
Share on other sites
Well it looks like what you have here is something like this

if(leftCollision == 1){ do stuff here}
if(rightCollision == 1){do stuff here}
if(anything done == true){
if(leftCollision == 0 && rightCollision == 0){
Move Ball here
}
}

It looks like your problem is that you never get to that point.
For example in order to make left_up = true leftCollision would have to be 1.
In order to move the ball leftCollision would have to be 0.
Hence you can never reach the part of the code where it should be moving. This should give you a clue on how to proceed.

Also as a little side note you might want to start putting indents after every if statement, your current code is really hard to read and tell what is happening.

Share this post


Link to post
Share on other sites
[quote name='chosenkill6' timestamp='1317595075' post='4868410']
if(left_collision == 0 && right_collision == 0)
[/quote]

This.

It looks to me like your ball doesn't 'move' unless a collision flag is set. The collision flag is only set the moment you're colliding.

Rather than all that complicated stuff with right_up and so on, why not just modify the velocity? Kinda like the pseudocode...

[source lang="cpp"]
move_ball() {
//compute collisions here
...
//collide with paddle
if (paddle_collision) {
ball.x = -ball.x;
if (ball.y < paddle_midpoint_y)
ball.y = -(abs(ball.y)); //always go -ve
else
ball.y = +(abs(ball.y)); //always go +ve
}
//collide with top/bottom of screen
if (screentop_collision)
ball.y = -(abs(ball.y));
if (screenbottom_collision)
ball.y = +... //etc

//always compute motion regardless of collisions!
ball.x += vel.x;
ball.y += vel.y;
}
[/source]

Share this post


Link to post
Share on other sites
I made some changes to my function, what i did was if the collision was true, it would set the appropriate bool to true and then set the collision flag back to zero so that the movement of the ball is executed, but this somehow caused some really weird effects on the movement of the ball, to the point where im wondering how it can even be possible for that to happen. What happens is the ball only moves whenever i move my mouse in a certain direction (How is the mouse even affecting the game? i have no mouse events anywhere in my entire source :S ) or if i hold a key down...
Here is my new function:
[code]
void move_ball()
{
int left_collision = 0,right_collision = 0;
float left_top, left_middle,left_bottom,right_top,right_middle,right_bottom;
bool left_up,left_down,right_up,right_down;

left_collision = SDL_BoundingCollide(pong_ball.image,pong_ball.x,pong_ball.y,left_paddle.image,left_paddle.x,left_paddle.y);
right_collision = SDL_BoundingCollide(pong_ball.image,pong_ball.x,pong_ball.y,right_paddle.image,right_paddle.x,right_paddle.y);

left_top = left_paddle.y;
left_middle = left_top + 50;
left_bottom = left_middle + 50;

right_top = right_paddle.y;
right_middle = right_top + 50;
right_bottom = right_middle + 50;

if(left_collision == 1)
{
if(pong_ball.y > left_top && pong_ball.y < left_middle)
{
pong_ball.x += pong_ball.xVel;
pong_ball.y -= pong_ball.yVel;

right_up = true;
right_down = false; left_up = false; left_down = false;

left_collision = 0;
}
if(pong_ball.y > left_middle && pong_ball.y < left_bottom)
{
pong_ball.x += pong_ball.xVel;
pong_ball.y += pong_ball.yVel;

right_down = true;
right_up = false; left_up = false; left_down = false;

left_collision = 0;

}
}
if(right_collision == 1)
{
if(pong_ball.y > right_top && pong_ball.y < right_middle)
{
pong_ball.x -= pong_ball.xVel;
pong_ball.y -= pong_ball.yVel;

left_up = true;
left_down = false; right_up = false; right_down = false;

right_collision = 0;

}
if(pong_ball.y > right_middle && pong_ball.y < right_bottom)
{
pong_ball.x -= pong_ball.xVel;
pong_ball.y += pong_ball.yVel;

left_down = true;
left_up = false; right_up = false; right_down = false;

right_collision = 0;

}
}

if(left_up == true || left_down == true || right_down == true || right_up == true)
{
if(left_collision == 0 && right_collision == 0)
{
if(left_up == true)
{
pong_ball.x -= pong_ball.xVel;
pong_ball.y -= pong_ball.yVel;
}
if(left_down == true)
{
pong_ball.x -= pong_ball.xVel;
pong_ball.y += pong_ball.yVel;
}
if(right_up == true)
{
pong_ball.x += pong_ball.xVel;
pong_ball.y -= pong_ball.yVel;
}
if(right_down == true)
{
pong_ball.x += pong_ball.xVel;
pong_ball.y += pong_ball.yVel;
}
}
}
}
[/code]
Again i am sorry for posting so much code, but i dont know what portions to post as the problem could be anywhere

Share this post


Link to post
Share on other sites
[quote name='chosenkill6' timestamp='1317600075' post='4868438']What happens is the ball only moves whenever i move my mouse in a certain direction (How is the mouse even affecting the game? i have no mouse events anywhere in my entire source :S ) or if i hold a key down...[/quote]
It sounds almost like you are calling move_ball() each time you receive an event instead of once every frame. You don't use SDL_WaitEvent, do you?

Share this post


Link to post
Share on other sites
Nope, im calling move_ball() in my game loop
Here is the loop that the function is called from:
[code]
void game()
{
SDL_Event game_event;

blit_background(); //Blits the background
set_start_pos(); //Sets the starting positions of the paddles and the ball
SDL_Flip(screen);
bool game_quit = false;

while(game_quit != true)
{
if(SDL_PollEvent(&game_event))
{
if(game_event.type == SDL_QUIT)
{
menu_quit = true;
game_quit = true;
}
}

blit_background(); //Blits the background
handle_input(); //Gets the keypresses and moves the paddles
apply_paddles(); //Blits the paddles
move_ball(); //The function that is causing me so much trouble...
apply_ball(); //Blits the ball
SDL_Flip(screen);
SDL_Delay(2); //I like to put a small delay in my game loop for some reason, shouldnt cause any problems though right?
}
}[/code]

Share this post


Link to post
Share on other sites
Your game loop looks good but you probably want to change [i]if[/i] to [i]while[/i] when testing SDL_PollEvent so that you handle all events otherwise some events will get delayed if you have recived multiple events.

In move_ball() you don't initialize left_up etc. so when no collision has occured you can't be sure what direction the ball will move.

A rhetorical question: What happens when right_collision == 1 and pong_ball.y == right_middle?

Share this post


Link to post
Share on other sites
[quote name='chosenkill6' timestamp='1317600075' post='4868438']
Again i am sorry for posting so much code, but i dont know what portions to post as the problem could be anywhere
[/quote]

Part of the fun of programming is solving problems yourself.

There have been lots of great pointers above - but if you want everyone to just jump in and solve your next bug, then what are you really getting out of it? You'll learn more and develop stronger skills by solving these riddles yourself.

Share this post


Link to post
Share on other sites
[quote name='Wooh' timestamp='1317603078' post='4868449']
Your game loop looks good but you probably want to change [i]if[/i] to [i]while[/i] when testing SDL_PollEvent so that you handle all events otherwise some events will get delayed if you have recived multiple events.

In move_ball() you don't initialize left_up etc. so when no collision has occured you can't be sure what direction the ball will move.

A rhetorical question: What happens when right_collision == 1 and pong_ball.y == right_middle?
[/quote]
The reason that I don't initialize the direction bool variables is because everytime that function is called, so every time the loop executes, the value will be set back to the initial value. The only workaround to this that I can think of is making these variables global and then setting them before my game loop starts. Still I don't know how that will fix the issue of my ball moving only when the mouse is moved or a key is pressed
And as for your question, that is something I will take care of once I get most of my game working properly, after all this is only the beta stages of my game :P

Share this post


Link to post
Share on other sites
You do realize that not initializing variables mean they can have whatever garbage value that happened to be in memory? At the moment the function is not remembering the ball direction.

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