Hi!
I'm programming a Paddle/Breakout game in C++ and I have just gotten to the ball movement...
When the ball hits the paddle,wall or a block how should the new angle be calculated??
Printable View
Hi!
I'm programming a Paddle/Breakout game in C++ and I have just gotten to the ball movement...
When the ball hits the paddle,wall or a block how should the new angle be calculated??
Usually if it hits the left or right wall, we just inverse the xspeed (xspeed = -xspeed), and if it hits the paddle or the top wall, we inverse the yspeed (yspeed = -yspeed). If you're using angles, you simply add 90 degrees to it each time it hits a wall.
I was using this code, so I just wanted to check...do you know why it wont run....I suspect that its the if statement that is wrong...
PHP Code:if(ball.x > (SCREEN_WIDTH - ball.width) )
{
xangle = -xangle;
ball.xv = xangle;
ball.yv = yangle;
Move_BOB(&ball);
}
if(ball.x <0)
{
xangle = xangle;
yangle = yangle;
ball.xv = xangle;
ball.yv = yangle;
Move_BOB(&ball);
}
if (ball.y < 0)
{
xangle = xangle;
yangle = yangle;
ball.xv = xangle;
ball.yv = yangle;
Move_BOB(&ball);
}
if(ball.x > SCREEN_HEIGHT)
{
xangle = -xangle;
yangle = -yangle;
ball.xv = xangle;
ball.yv = yangle;
Move_BOB(&ball);
}
I've fixed your code:PHP Code:if(ball.x > (SCREEN_WIDTH - ball.width) || ball.x < 0)
{
ball.xv = -ball.xv;
Move_BOB(&ball);
}
if(ball.y > (SCREEN_HEIGHT - ball.height) || ball.y < 0)
{
ball.yv = -ball.yv;
Move_BOB(&ball);
}
I figured that out while reading your reply ;)
thanks! :)
When I start the game the ball goes in the same direction even if I hit the ball with the corner of the paddle.....how would I make it go steeper and broader depending on how I hit the ball.....i have been trying butt off to do it but it won't work...any suggestions??
Solved it guys!
Glad you solved it. What did you end up doing?
I did the following, but it still doesn't work perfectly...when the ball hits the paddle on the far right the ball is smashed down instead of going up....
PHP Code://the ball has gone outside the screen
if(ball.x <= 0 | ball.x >= SCREEN_WIDTH - ball.width ){
xangle = -xangle;
}
// the ball has gone outside the screen
if(ball.y <= 0 |ball.y == SCREEN_HEIGHT -ball.height+ paddle.height ){
yangle = -yangle;
}
if(ball.y > SCREEN_HEIGHT){
yangle = -yangle;
}
// the ball has hit the paddle
if(Collision_BOBS(&ball,&paddle)){
yangle=-yangle; // change direction
// This collision detection determine where the ball hit the paddle and
// calculate a new direction
if(ball.x >= paddle.x && paddle.x <=19){
if(xangle >0){
xangle = 2;
}
else if(xangle <0){
xangle = -2;
}
}
if(ball.x > paddle.x +19 && paddle.x <=38){
if(xangle > 0){
xangle = 1;
}
else if(xangle <0){
xangle = -1;
}
}
if(ball.x >= paddle.x +38 && paddle.x <=57){
if(xangle > 0){
xangle = 0.5;
}
else if(xangle < 0){
xangle = -0.5;
}
}
if(ball.x > paddle.x +57 && paddle.x <=76){
if(xangle < 0){
xangle = -0.5;
}
else if(xangle > 0){
xangle = 0.5;
}
}
if(ball.x > paddle.x +76 && paddle.x <=95){
if(xangle <0){
xangle =-1;
}
else if(xangle >0){
xangle = 1;
}
}
if(ball.x > paddle.x +95 && paddle.x <=115){
yangle = -yangle;
if(xangle <0){
xangle = -2;
}
else if(xangle >0){
xangle = 2;
}
}
}