[LibGDX]Trying to make Pong, weird ball behaviour, slides on wall?

Here’s the project on GitHub: http://github.com/ericblobb/PongHelp

Whenever I run the game and the ball hits a wall, instead of bouncing off like it’s supposed to do, it sticks to the wall and slides on it. And that’s only for the top wall. For the bottom wall, the ball slides for less than second then drops off the window. I have no idea at what is happening so could any of you guys help?

Thanks.

You never change isGoingRight or isGoingLeft in bouncewall

edit: the issue probably has to do with ball.overlaps

That’s because there’s no right or left wall :wink:

@OP Using booleans to represent the direction of the ball, and then translating it to x & y seems really clunky. Instead (since you are using LibGDx), use a Vector2 to represents the ball direction. When a ball hits either the top or bottom wall, just invert the y component of the vector. Really, now that I think about it, just use an dx and dy value to represent the velocity if you don’t want to use vectors.

Yeah, for what ever reason I was thinking of breakout

edit:

Maybe I should elaborate. Basically if you overlap the top wall with the ball and the ball moves over the top wall more before you check to see if it overlaps then you’ll change direction, but will still be overlapping the wall.

This will in turn make the ball “ping-pong” back and forth along the wall forever, constantly changing direction but never reaching a point where the ball no longer overlaps the wall. I suggest you put breakpoints in your if statements in bouncewall() to see if it keeps alternating, then go from there.

This sounds like a good idea but I’ve never used vectors before Do you know any good tutorials or references that I can look into? Also thanks for the other replies everyone. I’ll try to take into account what you all said.

I was just suggesting vectors for a container for the x & y values of the velocity. For aligned objects like your walls, simply reflecting the y (y = -y) will give you the reflection you need. Like danisaur said, your collision detection might be running more than once, which is causing it to “jitter” like you described. Read his post for more info.

It is because the ball is overlapping and staying overlapped.

So therefore the code that resolves the collision is getting called repeatedly.

Have a search for Minimum Translation Distance or Minimum Translation Vector, this is what you will be looking for to do proper collision resolving using an easy algorithm.

EDIT: For more complex shapes, you can look up Separating Axis Theorom.