Public Group

# Stuck the ball in the board (pingpong game)

This topic is 825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

this game isn't completed yet, i've created the part where the ball hits the board, but there's problem in playing and that's the ball stucking in the board. For a better comprehension and understaning i've attached a video down below , plz watch it and lemme know of your take on this bug.

  public partial class PingPong : Form
{
public PingPong()
{
InitializeComponent();
timer1.Enabled = true;
timer1.Interval = 30;
}

int mx = 5;
int my = 5;
private void timer1_Tick(object sender, EventArgs e)
{

if (ball.Bounds.IntersectsWith(boardCenter.Bounds))
{
mx = mx * -1;
my = my * 1;
}
else if (ball.Location.Y >= this.ClientSize.Height - ball.Height)
{
mx = mx * 1;
my = my * -1;
}
else if (ball.Location.Y <= 0)
{
mx = mx * 1;
my = my * -1;
}

else if (ball.Location.X >= this.ClientSize.Width - ball.Width)
{
mx = mx * -1;
my = my * 1;
}

ball.Location = new Point(ball.Location.X + mx, ball.Location.Y + my);

}

private void PingPong_KeyDown(object sender, KeyEventArgs e)
{

if (e.KeyCode == Keys.Up)
{
board.Location = new Point(board.Location.X, board.Location.Y - 4);

}
else if (e.KeyCode == Keys.Down)
{
board.Location = new Point(board.Location.X, board.Location.Y + 4);
}

}

}


##### Share on other sites

Make the paddle ignore the ball if its traveling away from it.

Currently the ball goes a bit inside it, its directions is reversed, its still inside it, so it gets reversed again (which prevents it from ever leaving the paddle)

To do this, ensure the direction the pad points, and the direction of ball velocity, point away from each other, before reversing ball direction (dot product is how you do this with vectors - but you can just use a simple conditional if you want to)

##### Share on other sites

That's the problem with naive collisions. When you assume that the ball can only impact in one direction, you only flip for that direction.

The proper collision method would be to test if the ball at any point intersects the planes of a box. Each plane will have a normal ( a vector that is perpendicular to the plane or line).

Should the ball intersect or (collide) with the plane (line) you reflect the ball about the normal.

The formula for this is...

N = Normal of the plane (line) of a box
V = Vector of the ball. Vector being the directional speed of the ball. Example X velocity = 5, Y velocity = 5. This equates to a ball moving at a 45 degree angle to the right with a speed of 5.

-2* DotProduct(N*V) - V

EDIT:

A note. As long as the normals are facing in the correct direction, This equation will work for any arbitrary surface. Even if the surface is curved, then taking an integration of the surface's form to produce a tangent will give you the means to create a normal.

If you want the ball's direction to be influence by the motion of the paddle, then you can manipulate the paddles normal.

Edited by Tangletail

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631751
• Total Posts
3002084
×