| | |
Pong ball problem!!!! c++ & sdl
Thread Solved |
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
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!
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.
•
•
Join Date: Mar 2008
Posts: 1,427
Reputation:
Solved Threads: 115
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: Hope this helps.
if ( box.y < 0 ) {
yVel *= -1;
box.y = 0;
}
else if ( box.y > curLvlHeight - PUK_HEIGHT ) {
yVel *= -1;
box.y = curLvlHeight - PUK_HEIGHT;
} I need pageviews! most fun profile ever :)
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Billiard Ball Problem (Geeks' Lounge)
- Problem with Pong (Java)
- SDL library memory leak (C)
- Mixed problem- XP & file recovery.. (Windows NT / 2000 / XP)
- Simulated Cannon Ball Problem (C++)
- 1 do while with 2 delay (how u do that??) (C++)
Other Threads in the Game Development Forum
- Previous Thread: Guantanamo: The Game, The Controversy
- Next Thread: Okay, so I suck at math
| Thread Tools | Search this Thread |
3d advertising ai algorithm ban c++ cambridge camera censorship china competition console development engine fov fpx game gamer games gaming gauntanamo government idaho in-gameadvertisement intellectualproperty l-systems laracroft lindenmayer live manhunt math mathematics matrix mercenaries microsoft mmorpg modded msn naked news nintendo obama opengl palin physics pirate playstation politics projection ps3 rpg search selection software sony stephenhawking stocks studio technology terrorism tombraider uk videogame web wii world-of-warcraft xbox xbox-live xbox360






