How to Calculate Deflection Angles In Programming

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.).

Learnify Hub © www.0685.com All Rights Reserved