title: Random angles of a bouncing ball
body: <p>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.</p>
  
  <p>I'm not sure if I'm thinking about it correctly... I'm trying to put the coordinates in terms of degrees.</p>
  
  <p>So given the pic... You would take those deltas, then get degrees...</p>
  
  <p>degree = Math.atan2(opposite/adjacent) = (-4/-2) </p>
  
  <p><img src="https://i.stack.imgur.com/HGznv.jpg" alt="enter image description here"></p>
  
  <p>My code</p>
  
  <pre><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;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Gives the output: <code>26.56505117707799</code></p>
  
  <p>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. </p>
  
  <p>My questions: </p>
  
  <ul>
  <li>Am I calculating the angle of the ball correctly?</li>
  <li>How can I add variability to the bounce based on where it hits on the wall?</li>
  <li>Once I have the angle in degrees, how can I translate that back to coordinates?</li>
  </ul>
  
  <p>Thank you!</p>