Ball doesn't bind to the boundry correctly

I am programming my game and have hit another snag. I am trying to get the ball to bounce from left to right. I managed to get that working, but when the ball hits the left boundary it intersects it as if you do a print screen you will see half the ball within the screen and half of it is outside the screen. What’s strange is this behaviour isn’t exhibited by the right boundary checking code. To make matters worse the code for the ball’s boundary checking is is almost the same as the paddle’s boundary checking code, yet that code works fine. Any assistance in the matter would be greatly appreciated. You can download the source code here

Sincerely,

8BitOoze

You logic is good for detecting the right size. I suspect that the width of your applet may be bigger then what you see on your screen. Try drawing a line on the right side and see where disappears from the screen.

I believe you have two errors. First is that during your x intersection test, it should be x + vx < … not x - vx < … if your intent was to predict the objects next location. This change breaks the paddle movement, but I think there was another mistake there. You are using the paddles vx as a speed (i.e. the absolute value of the velocity). In the paddle movement code it should be using -paddle.getVX() to test against the left boundary.

x + vx < 0 - offset

don’t seem logical to me as your moving the object to the left by subtracting from the x position. Could you care to explain?

Why are we negating the paddles x velocity just to check for the left boundary? Again this doesn’t seem logically to me. If you would mind explaining it would be very helpful. Any assistence in this matter would be greatly appreciated.

Sincerely,

8BitOoze

To predict where an object will be you add its velocity (times the amount of time forward you want to predict) to its position. This is why you should always be adding the velocity, never subtracting. Think about how the objects position will be updated in the next frame.

When the paddle is moving left and needs to be tested against the left boundary, it has a negative velocity. Its x position is decreasing. In your code you are using your paddle velocity different than your ball velocity. You are adding or subtracting the velocity depending on which direction it should be moving. That is why you need to flip the sign for the left boundary test.

I would recommend you rewrite your paddle movement so that it uses negative velocity to move left. This would also fix your problem.

Thanks for the explanation, that makes a lot of sense. I made the adjustments you suggested and it worked!

Sincerely,

8BitOoze