0
\$\begingroup\$

I am having trouble with making a Breakout Clone using JavaFX. I have almost everything working, but I have a problem with the physics of the ball after it bounces off of the paddle. I used this question to get the physics working with the paddle, but only slightly. Instead of working nice and clean after switching around x and y values, the ball will sometimes end up getting stuck inside the paddle and jittering back and forth very rapidly instead of bouncing off. I have tried getting a "future version" of the ball, by checking where it would be on the next frame, but it still ends up doing the same thing. My code is here for anyone who is interested (it is all one class file, so I figured I would just use a pastebin).

Thanks in advance for any help!

EDIT: If anyone needs a video of what is happening, I will post one.

\$\endgroup\$
1
\$\begingroup\$

The ball bounces "off" the paddle but because it's still inside the paddle it bounces again back into, and again, and again.

This is what causes the jittering.

The solution is to check the direction and only bounce the ball when it's moving toward the paddle.

There's a few other solutions which may at times be adequate:

  • Rewind the ball back to before contact so it's not inside the paddle after a bounce. Caveat: This still causes issues when the ball can hit the sides of the paddle and the paddle can move under player control into the ball.
  • Force the ball out of the paddle height. Caveat: causes an instant motion that doesn't look quite right.
  • Disable bouncing the ball off the paddle for a short time after hitting the paddle. Caveat: May not be possible if your game feature enemies or bricks that can get very close to the paddle.
\$\endgroup\$
  • \$\begingroup\$ Thanks! I don't know why, but checking the direction of the ball never even occurred to me. It works perfectly now! \$\endgroup\$ – MattChris Apr 30 '18 at 1:48
1
\$\begingroup\$

In your code you have

if (dot.getCenterY() <= brick.getY() || dot.getCenterY() >= brick.getY()+brick.getHeight())
                        dotVelY *= -1;

This makes it so that the ball changes it's Y velocity anytime it hits the paddle. We don't want that.

To fix your issue just make it so that the ball's velocity is reversed ONLY when the velocity.y is greater than zero (the ball is moving downwards)

The code should look something like this:

if ((dot.getCenterY() <= brick.getY() || dot.getCenterY() >= brick.getY()+brick.getHeight()) && dotVelY > 0)
                        dotVelY *= -1;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.