Java – Calculate the degree of a bouncing ball

Calculate the degree of a bouncing ball… here is a solution to the problem.

Calculate the degree of a bouncing ball

I’m playing a simple breakout game and I’m having some problems calculating the angle at which the ball hits the top border. When the ball moves up at an angle of 180 degrees, it bounces back down at an angle of 0 degrees. But when the ball moves up at an angle of 170 degrees, it should bounce back down at a mirrored angle, say 10 degrees. I can calculate 180-170 = 10 degrees like this, but what if the ball moves up at an angle of 190 degrees!? Then it should spring down at an angle of 350 degrees, but I don’t know how to calculate this !?

Is there an easy way to calculate or mirror the angle value of the ball moving up? Preciate some help because I’m not good at math! Thanks!

Edit:
I move the ball like this :

xPos += speed * Math.sin(Math.toRadians(direction));
yPos += speed * Math.cos(Math.toRadians(direction));

Solution

Answer questions about angles and reflections:

  1. Determine your angle measurement system. You say that the angle of a ball moving up is 180°, so I guess 0° points down and the angle increases counterclockwise (90° to the right, etc.). It’s important to be consistent. Let d be the angle at which the ball moves in that system.

  2. Define the angle of the border method vector. If the border at the top is horizontal, its vector is perpendicular to it and has an angle of 0°. (in the measurement system defined in point 1). Let n be that angle. The vertical border will have n = 90°

  3. Exit corner o-ball by:

    o = 2*n - d - 180°

    Note that you may need to standardize this angle, i.e. you add/subtract 360° to/from o until 0° <= o < 360°

Your example d = 190°, n = 0° :

o = 2*0° - 190° - 180° = -370°

This causes o = 350° after normalization as expected.

Related Problems and Solutions