943,948 Members | Top Members by Rank

Ad:
Jun 1st, 2009
1

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

Expand Post »
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.
Similar Threads
Reputation Points: 30
Solved Threads: 0
Newbie Poster
Xephys is offline Offline
5 posts
since May 2009
Jun 1st, 2009
0

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

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.
Reputation Points: 30
Solved Threads: 0
Newbie Poster
Xephys is offline Offline
5 posts
since May 2009
Jun 4th, 2009
0

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

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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jun 4th, 2009
1

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

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.
Reputation Points: 30
Solved Threads: 0
Newbie Poster
Xephys is offline Offline
5 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Game Development Forum Timeline: Guantanamo: The Game, The Controversy
Next Thread in Game Development Forum Timeline: Okay, so I suck at math





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC