I'm making a Pong game, and I've come across a problem. When the ball (a rectangle) collides with the racket (or the bat) below or above the racket, I get a strange bug where the ball moves into the rectangle and goes left-right-left-right reaching high speeds(because I added acceleration) and jumps out at the opposite side. I know why this bug is happening:
if (ballrec.Intersects(player1rec)
&& ball.x <= 20
&& ball.y + 20 >= player.y
&& ball.y <= player.y + 100) //checks the front rebound-here's the bug
{
ball.vx *= -1; //changes x-direction
if (ball.vx < 0)
ball.vx -= 1; //increases x-velocity
effect.Play();
if (R.Next(4) == 0)
{
if (ball.vy < 0) ball.vy--;
else ball.vy++; //increases y-velocity on a special occasion
}
}
else
{
if (ballrec.Intersects(player1rec))
{
ball.vy *= -1;
effect.Play();
}
}
ball.vy=velocity y-axis: I multiply it with -1 to change the direction
effect=sound
The bug: To make the ball rebound at any given location on the front of the racket, it says that the ball's lower side (that +20) mustn't be higher than the racket's upper side and the ball's upper side mustn't be lower than the racket's lower side. However because of the x coordinates (ball.x<=20, 20=the width of the racket), the front rebound effect consumes the top and the bottom side of the racket, and then the rebound there can't work.
When I try to solve it, my best non-complicated solution (because next year I'm starting middle school (14-18 in my country) and don't know a lot of fancy maths), I don't get a good solution (check below).
My solution (which I'm not happy with): I lower the area required for the front rebound to ball.y>=player.y and ball.y+20<=player.y+100(the length) and the up and down rebound work, but if the ball hits a corner of the racket, the same bug appears only in this case the ball moves up-down-up-down.
My question: How to fix the bug? Thank you for your time! Hope that wasn't too long!
Current solution (not perfect):
if (ballrec.Intersects(player1rec)
&& ball.x <= 20
&& ball.y >= player.y
&& ball.y + 20 <= player.y + 100)
{
ball.vx *= -1;
if (ball.vx < 0)
ball.vx -= 1;
effect.Play();
if (R.Next(4) == 0)
{
if (ball.vy < 0) ball.vy--;
else ball.vy++;
}
}
else
{
if (ballrec.Intersects(player1rec))
{
ball.vy *= -1;
effect.Play();
}
}
I'm making a Pong game, and I've come across a problem. When the ball (a rectangle) collides with the racket (or the bat) below or above the racket, I get a strange bug where the ball moves into the rectangle and goes left-right-left-right reaching high speeds(because I added acceleration) and jumps out at the opposite side. I know why this bug is happening:
if (ballrec.Intersects(player1rec)
&& ball.x <= 20
&& ball.y + 20 >= player.y
&& ball.y <= player.y + 100) //checks the front rebound-here's the bug
{
ball.vx *= -1; //changes x-direction
if (ball.vx < 0)
ball.vx -= 1; //increases x-velocity
effect.Play();
if (R.Next(4) == 0)
{
if (ball.vy < 0) ball.vy--;
else ball.vy++; //increases y-velocity on a special occasion
}
}
else
{
if (ballrec.Intersects(player1rec))
{
ball.vy *= -1;
effect.Play();
}
}
ball.vy=velocity y-axis: I multiply it with -1 to change the direction
effect=sound
The bug: To make the ball rebound at any given location on the front of the racket, it says that the ball's lower side (that +20) mustn't be higher than the racket's upper side and the ball's upper side mustn't be lower than the racket's lower side. However because of the x coordinates (ball.x<=20, 20=the width of the racket), the front rebound effect consumes the top and the bottom side of the racket, and then the rebound there can't work.
When I try to solve it, my best non-complicated solution (because next year I'm starting middle school (14-18 in my country) and don't know a lot of fancy maths), I don't get a good solution (check below).
My solution (which I'm not happy with): I lower the area required for the front rebound to ball.y>=player.y and ball.y+20<=player.y+100(the length) and the up and down rebound work, but if the ball hits a corner of the racket, the same bug appears only in this case the ball moves up-down-up-down.
My question: How to fix the bug? Thank you for your time! Hope that wasn't too long!
Current solution (not perfect):
if (ballrec.Intersects(player1rec)
&& ball.x <= 20
&& ball.y >= player.y
&& ball.y + 20 <= player.y + 100)
{
ball.vx *= -1;
if (ball.vx < 0)
ball.vx -= 1;
effect.Play();
if (R.Next(4) == 0)
{
if (ball.vy < 0) ball.vy--;
else ball.vy++;
}
}
else
{
if (ballrec.Intersects(player1rec))
{
ball.vy *= -1;
effect.Play();
}
}
0 commentaires:
Enregistrer un commentaire