PDA

View Full Version : I need help with a motion system for a ball in c++(please help!)



stinkee2
June 21st, 2009, 15:33
ok ive been making this pong game for a few days now (im a new programmer and this is my first game) and im almost done,Ive got a working menu screen, paddles move, collision detection, everythings fine...
BUT when I tried to make a motion system it just hits the paddle and stops and if you move the paddle the ball will continue to go off the screen and restart the game?
I know what is causing this problem but it will be easier if I show you the source code first.

if(active == 1)//if X is pressed
{
bx = move_left(bx,speed)
if(bx == 451)
{
if(CollisionDetection(bx,by,10,11,451,by,10,53) == 1)
{
bx = move_left(bx,speed);
}
else
{
bx = move_right(bx,speed);
}
}
if(bx == 12)
{
if(CollisionDetection(bx,by,10,11,12,py,10,53) == 1)
{
bx = move_left(bx,speed);
}
else
{
bx = move_right(bx,speed);
}
}
}//this is all in a while loop

bx is an int and it is responsible for the balls x position
speed is the speed of the ball
bx = move_right() and bx = move_left() make the ball move left or right and they work perfectly
CollisionDetection() does exactly what it sounds like and perfectly.
=================================
my problem is every time you press x to start the game the ball moves in the direction it should, but when it collides it just stops and if you move the paddle while the ball is stopped, right after the paddle isnt colliding with the ball it continues to go past me and I lose (its like playint pong with Chuck Norris)
this happens because the first thing there makes the ball move left and since that is repeated continuously in the while loop it will always move left until it hits the paddle and then after you move the paddle it will move left again.
how would I get the ball to move in a direction and then bounce back?
Please can somebody help me??
(right now Im only working on the x position not the y)
I might solve this on my own after I post this but im not sure...

stinkee2
June 21st, 2009, 16:05
Nevermind, I got it... see hehehe