Ad

Time of flight computed deterministically from input values.

Code
Diff
  • import math
    import numpy as np
    g = 9.81
    
    def calc_time_of_impact(speed, angle, origin_y):
        angle = np.deg2rad(angle)
        if origin_y:
            return (speed * math.sin(angle) + math.sqrt((speed * math.sin(angle))**2 + 2 * g * origin_y)) / g
        return 2 * speed * math.sin(angle) / g 
    • import math
    • import numpy as np
    • g = 9.81
    • def calc_time_of_impact(speed, angle, origin_y):
    • x_value = 0
    • y_value = 0
    • time = 0.0001
    • def x_formula(t): return speed * t * \
    • math.cos(angle * math.pi / 180)
    • def y_formula(t): return speed * t * \
    • math.sin(angle * math.pi / 180) - 9.81/2*t*t
    • while y_value >= -origin_y:
    • x_value = x_formula(time)
    • y_value = y_formula(time)
    • time += 0.0001
    • return time
    • angle = np.deg2rad(angle)
    • if origin_y:
    • return (speed * math.sin(angle) + math.sqrt((speed * math.sin(angle))**2 + 2 * g * origin_y)) / g
    • return 2 * speed * math.sin(angle) / g