Random angles of a bouncing ball
angle, java
Solution
Am I calculating the angle of the ball correctly?
Your drawing is not to scale. The 26 degrees is measured from a line perpendicular to the wall.
How can I add variability to the bounce based on where it hits on the wall?
You already suggested a random angle. You can also adjust the angle of reflection based on the distance from the center of the wall.
Put your angle of reflection calculation into its own method. You can adjust the calculation until your calculations give you the "randomness" you're looking for.
Once I have the angle in degrees, how can I translate that back to coordinates?
Convert the degrees to radians, then calculate the SAS of the triangle. Just leave your angles in radians in the model, and convert to degrees in your display / diagnostic methods.
Problem
I'm trying to make a ball bounce around a window. Depending on how far away the ball hits the wall and at what angle will determine its reflection. You can see in the pic that the black trajectory hits the opposite wall on the inner half... and the gray trajectory represents if it were to reflect and hit the other half... which would decrease the angle of reflection. I'm not sure if I'm thinking about it correctly... I'm trying to put the coordinates in terms of degrees. So given the pic... You would take those deltas, then get degrees... degree = Math.atan2(opposite/adjacent) = (-4/-2) My code ``` public class Calculate { public Calculate() { System.out.println(getCalc(7,5,4,0)); } public double getCalc(int x1, int x2, int y1, int y2) { double deltaX = Math.abs(x2-x1); double deltaY = Math.abs(y2-y1); double degrees = Math.toDegrees((java.lang.Math.atan2(deltaX, deltaY))); return degrees; } ``` } Gives the output: `26.56505117707799` So now I know the ball would reflect off the wall at 26 degrees (since that's the angle of incidence). But I don't want the ball to necessarily reflect uniformly off each wall so it adds variability. My questions: - Am I calculating the angle of the ball correctly? - How can I add variability to the bounce based on where it hits on the wall? - Once I have the angle in degrees, how can I translate that back to coordinates? Thank you!