Pong ball problem!!!! c++ & sdl

Thread Solved

Join Date: May 2009
Posts: 3
Reputation: Xephys is an unknown quantity at this point 
Solved Threads: 0
Xephys Xephys is offline Offline
Newbie Poster

Pong ball problem!!!! c++ & sdl

 
1
  #1
Jun 1st, 2009
Hi there,

This is my first post and I am quite new to developing games.
I have only made tic tac toe and currently making a pong clone,
using c++ and SDL.

I seem to be having a problem with the animation of my ball (or puk as I call it). The animation works fine...except for when it has a second encounter with either the top or bottom of the screen. I have coded so that when the puk encounters the y=0 axis(top) it bounces back on to the screen and also similar code for when it encounters with the bottom of the screen.

The problem I am having however is that if a second encounter with the top or bottom of the screen occurs instead of bouncing back the puk simply vanishes and a point goes to the appropriate player. I have tried to enclose that part of the code in a loop but it just froze the application.

I would appreciate any thoughts/suggestions on the matter. I have enclosed the function from my puk class that deals with the puks motion below.

Thanks!

void Puk::move(){
    //Move the Puk left or right********************************
    box.x += xVel;
    
    //If the Puk went too far to the left reset puk at player 2 position
    if(box.x < 0){
             
        box.x -= xVel;
        init((curLvlWidth - 6*PUK_WIDTH), (curLvlHeight - PUK_HEIGHT)/2, curLvlWidth, curLvlHeight);
        xVel = 0;
        yVel = 0;
        status = PUK_LEFT;
        score_p2++;
        //re-initialise  npc bat
        npc_bat.init( (curLvlWidth- 4*BAT_WIDTH), (curLvlHeight - BAT_HEIGHT)/2, curLvlWidth, curLvlHeight);
        npc_bat.set_vel(0, 0);
    }
    //If the Puk went too far to the right reset puk at player 1 position
    if( (box.x + PUK_WIDTH) > curLvlWidth  ){
 
        box.x -= xVel;
        init(6*PUK_WIDTH, (curLvlHeight - PUK_HEIGHT)/2, curLvlWidth, curLvlHeight);
        xVel = 0;
        yVel = 0;
        status = PUK_RIGHT;
        score_p1++;
        //re-initialise  npc bat
        npc_bat.init( (curLvlWidth- 4*BAT_WIDTH), (curLvlHeight - BAT_HEIGHT)/2, curLvlWidth, curLvlHeight);
        npc_bat.set_vel(0, 0);
    }
    
    //Checks if collision with player 2 bat occured and if true, then bounce puk back 
    if(Check_Collision(npc_bat, my_puk) == true){
                               Mix_PlayChannel( -1, collision, 0 ); 
                               box.x -= xVel;
                               xVel = 0;
                               yVel = random_yvel + npc_bat.get_yvel() ;
                               xVel -= PUK_HEIGHT;
                               box.x += xVel;
                               status = PUK_LEFT;
    }

    //Checks if collision with player 1 bat occured and if true, then bounce puk back 
    if(Check_Collision(player1_bat, my_puk) == true ){
                               Mix_PlayChannel( -1, collision, 0 );      
                               box.x -= xVel;
                               xVel = 0;
                               yVel = random_yvel + player1_bat.get_yvel() ;
                               xVel += PUK_HEIGHT;
                               box.x += xVel;
                               //box.y += yVel;
                               status = PUK_RIGHT;
    }

    //Move the Puk up or down*********************************
    box.y -= yVel;
    //If the Puk went too far up or down
    if(box.y < 0){
        //move back
        yVel -= PUK_HEIGHT;
        box.y -= yVel;
    }
    if((box.y + PUK_HEIGHT) > curLvlHeight){
        //move back
        yVel += PUK_HEIGHT;
        box.y -= yVel;
    }
}
Last edited by Xephys; Jun 1st, 2009 at 3:53 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: Xephys is an unknown quantity at this point 
Solved Threads: 0
Xephys Xephys is offline Offline
Newbie Poster

Re: Pong ball problem!!!! c++ & sdl

 
0
  #2
Jun 1st, 2009
sorry found a mistake in my code.
line 56 should read box.y += yVel; instead
of box.y -= yVel; .

I was just testing something while making this thread and didnt realise when I copied the code over.
The same problem still persists.
Last edited by Xephys; Jun 1st, 2009 at 4:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,427
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 115
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Pong ball problem!!!! c++ & sdl

 
0
  #3
Jun 4th, 2009
So, are you trying to make the puk bounce when it hits the top or the bottom? If so, simply invert the Y-Velocity like this:
if ( box.y < 0 ) {
  yVel *= -1;
  box.y = 0;
}

else if ( box.y > curLvlHeight - PUK_HEIGHT ) {
  yVel *= -1;
  box.y = curLvlHeight - PUK_HEIGHT;
}
Hope this helps.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: Xephys is an unknown quantity at this point 
Solved Threads: 0
Xephys Xephys is offline Offline
Newbie Poster

Re: Pong ball problem!!!! c++ & sdl

 
1
  #4
Jun 4th, 2009
Hey, thanks for your help...it works!!!!!!!

Thank you, yeh basically I just wanted the puk bounce back whenever it hit the top or bottom but my code seemed to only allow the puk to bounce off the top or bottom once. But thanks to you, the puk can now do multiple bounces. And with that the game is finished. It was the last bug that I was trying to fix.

Cheers for that.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC