Reputation: 21
I'm coding a game of Pong in C# and there seems to be something wrong with a few lines of code. Those lines are in the code to make sure the ball bounces back the opposite direction. But it doesn't really work. It bounces, but it goes through the edges of the paddle. Besides there is a problem where my ball goes in the right wall about half the way before bouncing the right direction.
if (ball.Location.X > player1.Location.X
&& ball.Location.X + ball.width < player1.Location.X + player1.Width
&& ball.Location.Y + ball.Height > player1.Location.Y)
This should make sure the ball bounces back from the paddle, but as said before; it does not bounce on the entire paddle. The ball will go right through the edge of the paddle, due to some wrong coding in this sentence, but I have on clue how to fix this. It has to do something with the right overlapping code, where the ball bounces on the full length of the paddle.
if (ball.Location.X > player2.Location.X
&& ball.Location.X + ball.Width < player2.Location.X + player2.Width
&& ball.Location.Y < player2.Location.Y + player2.Height)
It is the exact same thing with this sentence. It goes through the edges which means there something wrong with the overlapping. What should come due to some X
and Y
(including some Width
and Height
somewhere).
Last but not least there is a point something where my ball goes halfway in the left wall. But I don't understand why is interacts like that.
if (ball.Location.X + ball.Width > this.Width)
This should be making sure the left location of the ball (including its Width
) should not go further than the entire Width of the screen where the game is played in...
Thanks anyway for reading this. Hopefully there is someone able to help me with this problem.
This is basically my entire code
namespace Pong_2
{
public partial class Form1 : Form
{
int playerSpeed = 7;
int p1Vel;
int p2Vel;
int bVelX = 1;
int bVelY = 1;
int scorePlayer1Int;
int scorePlayer2Int;
bool pause = false;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!pause)
{
ball.Location = new Point(ball.Location.X + bVelX, ball.Location.Y + bVelY);
player1.Location = new Point(player1.Location.X + p1Vel, player1.Location.Y);
player2.Location = new Point(player2.Location.X + p2Vel, player2.Location.Y);
}
if (ball.Location.Y < 0)
{
scorePlayer1Int++;
ball.Location = new Point(this.Width / 2, this.Height / 2);
}
if (ball.Location.Y > this.Height)
{
scorePlayer2Int++;
ball.Location = new Point(this.Width / 2, this.Height / 2);
}
if (ball.Location.X > player1.Location.X && ball.Location.X + ball.Width < player1.Location.X + player1.Width && ball.Location.Y + ball.Height > player1.Location.Y)
{
bVelY *= -1;
}
if (ball.Location.X > player2.Location.X && ball.Location.X + ball.Width < player2.Location.X + player2.Width && ball.Location.Y < player2.Location.Y + player2.Height)
{
bVelY *= -1;
}
if (ball.Location.X < 0)
{
bVelX *= -1;
}
if (ball.Location.X + ball.Width > 984)
{
bVelX *= -1;
}
scorePlayer1.Text = scorePlayer1Int.ToString();
scorePlayer2.Text = scorePlayer2Int.ToString();
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
p1Vel = 0;
}
else if (e.KeyCode == Keys.Left)
{
p1Vel = 0;
}
else if (e.KeyCode == Keys.D)
{
p2Vel = 0;
}
else if (e.KeyCode == Keys.A)
{
p2Vel = 0;
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
p1Vel = playerSpeed;
}
else if (e.KeyCode == Keys.Left)
{
p1Vel = -playerSpeed;
}
else if (e.KeyCode == Keys.D)
{
p2Vel = playerSpeed;
}
else if (e.KeyCode == Keys.A)
{
p2Vel = -playerSpeed;
}
else if (e.KeyCode == Keys.P)
{
if (!pause)
{
pause = true;
}
else if (pause)
{
pause = false;
}
}
}
}
}
Upvotes: 2
Views: 494
Reputation: 16991
The most likely cause of this problem is due to the distance the ball is moving between frames. If your ball is moving at a high enough speed each frame, the odds are that the collision will only be detected once the ball is well inside the other object.
If your ball is moving at 100 pixels per second, and you are drawing 10 frames each second, the ball will jump 10 pixels at a time. If the left wall is at 0
and the ball's previous X
was at 5
, the next frame will have the ball at -5
. At high enough speeds, and low enough frame rates, it is even possible for objects to pass right through each other without colliding.
Solving this is tricky, it involves some complex math to calculate the exact point of collision between the last and current frames. If you have a lot of objects, this can get expensive to calculate, and slow your game down. Many games will use a simple collision detection, using bounding boxes, to trigger the more expensive calculation as an optimization.
Unfortunately I'm in a location that has a filter preventing me from looking up game related web-sites, (cough)work(cough), but this type of collision detection is usually used for things like bullets. You should be able to find something on the internet by searching around those types of terms. I'll update this answer once I get someplace with open internet.
Update
I'm not actually able to verify this link from my present location, but this should contain the math and theory you need for accurate inter-frame collision detection.
The answer to this question may also be helpful.
Upvotes: 1