How to Calculate Deflection Angles In Programming

How to Calculate Deflection Angles In Programming thumbnail
Program an online billiards game with angular momentum equations.

Game programs require that the programmer include calculations for the deflection angle, or the angle at which a moving object will deflect after it strikes another object. Without the code to precisely calculate the deflection angle, games that involve collisions would not appear realistic. For beginners, calculating the deflection angle of a ball when it hits a stationary edge, such as the edge of billiards table, is one of the easier deflection angles to calculate. Calculating it with code, however, requires that you define the variables and the mathematical formulas needed.

Instructions

    • 1

      Declare in code a variable called "xposition" to store the x coordinate of the moving ball and a variable called "yposition" to store the y coordinate of the moving ball at an arbitrary point in time. Declare a variable called "xposition2" to store the x coordinate of the moving ball and a variable called "yposition2" to store the y coordinate of the moving ball at a later point in time.

    • 2

      Declare a variable called "deltaY" to store the change in y-coordinates of the moving ball, a variable called "deltaX" to store the change in x-coordinates of the moving ball, a variable called "slope" to store the slope of the line that the moving ball follows and a variable called "angle" to store the deflection angle of the moving ball after it hits the stationary object (the edge of the pool table).

    • 3

      Code the statements necessary to retrieve and store in the variables xposition, yposition, xposition2 and yposition2 at two different times.

    • 4

      Program the code to calculate deltaY using the equation:
      deltaY = ypostiion2-yposition

    • 5

      Program the code to calculate deltaX using the equation:
      deltaY = xpostiion2-xposition

    • 6

      Program the code to calculate the slope of the line of the moving ball using the equation:
      Slope = deltaY/deltaX

    • 7

      Program the code to calculate the angle of the moving ball using the equation

      Angle = arctangent(slope)

    • 8

      Program the code to calculate the angle of deflection using the equation.

      Angle = 180 - Angle

      The equation takes the value of the angle calculated with the arctangent statement and subtracts it from 180 degrees to obtain the angle of deflection (of the moving ball with reference to the edge of the pool table.).

Tips & Warnings

  • The times that you sample the coordinates of the moving ball is not critical if the ball that is moving follows a straight line. Balls that move on a curved path require that you use two points in time that are as close as possible to each other and occur at a point in time that is nearest the point in time when the ball hits the table's edge.

  • The direction of the slope of the line may not necessarily be reflected in the results of the arctangent function. The angle may have to modified by plus or minus 90 degrees, depending on whether the slope is positive or negative.

Related Searches:

References

  • Photo Credit Jupiterimages, Brand X Pictures/Brand X Pictures/Getty Images

Comments

Related Ads

Featured