Calculating degrees for bouncing ball

android, java

Solution

To answer your question regarding angles and reflection:

Decide on your system of angle measurement. You told that a ball that is moving upwards is at an angle of `180°`, so I guess `0°` is pointing downwards and the angle is increasing in counter-clockwise direction (`90°` points to the right etc.). It is important to be consistent. Let `d` be the angle of the ball's movement in that system.

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

The outgoing angle `o` of the ball is given by:

`o = 2*n - d - 180°`

Note that you might have to normalize this angle i.e. you add/subtract `360°` to/from o until `0° <= o < 360°`.

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

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

This will result in `o = 350°` after normalization, as expected.

Problem

I'm doing a simple breakout game and I have some problem how to calculate the angle when the ball hit the top border. When the ball moves upwards in an angle of 180 degrees, then it bouncing back downwards in an angle of 0 degrees. But when the ball is moving upwards in an angle of 170 degrees, then it should be bouncing back downwards in a mirrored angle, like 10 degrees. I can calculate this like `180-170 = 10` degrees, but what if the ball is moving upwards in an angle of 190 degrees!? Then it should be bouncing back downwards in an angle of 350 degrees, but I don't know how to calculate this!? Is there a simple way to calculate or mirror the value of the upwards moving angle of the ball? Preciate some help because I'm not good in math! Thanks! EDIT: I'm moving the ball like this: ``` xPos += speed * Math.sin(Math.toRadians(direction)); yPos += speed * Math.cos(Math.toRadians(direction)); ```

Original source