Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random angles of a bouncing ball

Tags:

java

angle

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)

enter image description here

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!

like image 875
user3871 Avatar asked Oct 04 '22 10:10

user3871


2 Answers

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.

like image 168
Gilbert Le Blanc Avatar answered Oct 07 '22 22:10

Gilbert Le Blanc


I think that the distance of the ball from the surface doesn't really have an effect on the angle. The angle of the ball before hitting the surface should be the same (mirror reflected) when it leaves the surface for it to be natural.

You can add some variability by thinking what happens to a rubber ball, since it changes a little on impact depending on the force etc., the reflection is not exactly the same every time. You could simply add or remove a degree or two randomly and see how it goes.

Once you have an angle, its once again down to trigonometry. You have an angle, and you know the hypotenuse (I presume depending on your frame-rate and ball speed the ball would have travelled a certain amount from the surface). So from that you need to get the adjacent and opposite lines of the triangle.

For example:

sin(angle) * hypothenuse = opposite (so Y offset from the surface).

cos(angle) * hypothenuse = adjacent (so X offset from the point of contact).

Just add or remove (depending on the direction) the adjacent and opposite values from the coordinates of the contact point.

like image 26
jbx Avatar answered Oct 08 '22 00:10

jbx