18

I have problem but I don't know how to describe it so I have drawn it.

Pong Physics Graphic

As you can see ball bounces when collision is detected. Nevermind the angle - I have just drawn it that way. But sometimes ball goes through the paddle leaving it other way.

Why that happens ? Becase when I detect the collision I change vx to -vx. What should I do in that case ? Change also y ? But how to check which side of the paddle ball hit?

Maybe other ideas?

skaffman
  • 398,947
  • 96
  • 818
  • 769
zarcel
  • 1,211
  • 2
  • 16
  • 35

5 Answers5

29

This is quite a common problem for people writing pong clones. The solution is to, upon detecting a collision, not only reverse the velocity but also alter the ball position so that it doesn't intersect the paddle.

Otherwise it is possible for the ball to become stuck inside the paddle, constantly negating vx = -vx = vx = -vx each frame. It won't leave the paddle until its y momentum moves it far enough up or down to escape.

Jeff Linahan
  • 3,775
  • 5
  • 37
  • 56
  • Could you explain a little to me ? :> – zarcel Apr 02 '11 at 22:08
  • 1
    You need to set the ball's x position when you detect a collision; changing the velocity is not enough. The leftmost pixel of the ball should appear to the right of the rightmost position of the paddle. – Jeff Linahan Apr 02 '11 at 22:11
  • 1
    basically, when you detect a collision, the ball might either be tangent to the paddle, or actually intersecting it. so your first step must be to not just reverse the velocity, but then nudge the ball along the new vector till it does not intersect the paddle. you need to do this in an "atomic" step *before* control is returned to your regular ball movement loop. – Martin DeMello Apr 03 '11 at 12:54
10

A number of things could be causing the problem.

Notably, if the ball can travel more than one pixel per "tick" or "frame", it may intersect the paddle by more several pixels by the time the collision is detected.

You then reverse bounce the ball off the paddle by altering its velocity, but depending on the new angle, it may take several frames for the ball to completely leave the paddle. So, on the next tick, it's still intersecting and you're reversing the velocity again. A freak occurrence of this may well lead to the ball eventually leaving the paddle on the other side, appearing to fly straight through.

You may want to put a "do not collide for a while" flag on that paddle-ball combination, when the intersection is first detected.

As a related issue, if the ball is going fast enough (particularly when its x-component is highest, like when the ball is travelling almost entirely horizontally, so that there is the least of the paddle for it to get through), there may in fact be no frames where the ball is physically intersecting the paddle.

This all depends on your code, which we can't see. Hopefully the above should give you some ideas, though.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 4
    That "do not collide for a while" flag can be handled as a simple state machine. When a collision is detected, if in the normal state, apply the bounce and also transition to the "colliding" state. While in the colliding state, don't apply any bouncing - just stay in the colliding state until the collision is no longer detected, then transition back to the normal state. The effect - bounces can only repeat after at least one frame of non-collision. –  Apr 02 '11 at 22:10
  • @Steve314: +1 Yep, that's how I'd deal with it. – Lightness Races in Orbit Apr 02 '11 at 22:11
  • 3
    In theory the ball can never hit the same paddle/wall twice consecutively, right? So if you save the last object it hit, you can ignore subsequent hits to the same object. – André Paramés Apr 02 '11 at 23:07
  • @André: That's another good approach, not too far off Steve's. – Lightness Races in Orbit Apr 02 '11 at 23:09
  • One criticism of my approach - it may be confused by collisions with two separate objects in two successive frames - e.g. the bat then a top/bottom wall. To handle that needs a more sophisticated state. One possibility is exactly what André describes. –  Apr 03 '11 at 19:03
0

A solution I found when making pong was to use vx = abs(vx) when a collision with the left paddle is detected and vx = -abs(vx) when a collision with the right paddle is detected. this ensures the ball bounces only once,eliminating the travel inside the paddle caused by multiple bounces.

0

Your awesome drawing shows me that you're not detecting a vertical collision. You should do the same thing you do for the x coordinates for the y coordinates.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • Actually, I do. I set velocity to 0 when collision is detected and it stopped in both cases from the drawing. – zarcel Apr 02 '11 at 21:45
0

This is usually caused by too fast movement of a ball. Where one frame it is traveling to a wall and next frame it is already placed behind the wall. Rhe collision just does not happens. You can negate that by looking further along balls trajectory and checking for obstacles.

Kromster
  • 7,181
  • 7
  • 63
  • 111