I am trying to matemathically predict how long will projectile fly until it reach the target. I am trying to implement this formula:

t = [V * sin(α) + √(V * sin(α))² + 2 * g * h)] / g

where I know V (velocity of projectile) , α (angle of launch) , h (height of launch) The problem is it shows some weird results. I am not sure what is wrong (using ue4):

float Time = Velocity * FMath::Sin(AngleDeg) + FMath::Sqrt(FMath::Square(Velocity * FMath::Sin(AngleDeg)) + (2.f * Gravity * Height));
Time /= Gravity;

For example this test inputs: Gravity = 9.8; Height = 10; Velocity = 10; AngleDeg = 10; shows 0.97 seconds but theresult should be 1.61 according this calculator: https://www.omnicalculator.com/physics/projectile-motion ... When I try different inputs the result is always wrong... I also got the formula from this website so not sure what's wrong. Thanks for any advice

  • \$\begingroup\$ Where in this formula does your target 's position or offser from the firing point occur? \$\endgroup\$ – DMGregory Jun 11 at 1:17
  • \$\begingroup\$ does FMath:Sin accept degrees or radians? \$\endgroup\$ – trollingchar Jun 11 at 8:06

Given that you have velocity and angle, you can calculate \$v_x\$ and \$v_y\$.
You dont need \$v_x\$ though.

$$v_y = v \cdot sin \space \alpha $$

Make sure that you use the right units. If Sin accept radians, don't pass degrees there.

Then, using \$v_y\$, \$h\$ and \$a\$, you calculate flight time. You need to solve the equation:

$${at^2 \over 2} + v_yt + h = 0$$

Check your \$Y\$ axis. I assume that it goes up. So acceleration is negative. If you have \$g > 0\$, then you should have \$a=-g\$.

$$t_1, t_2 = {-v_y \pm \sqrt {{v_y^2} - 2ah} \over a}$$

Omit the negative root and you get

$$t = {-v_y - \sqrt {{v_y^2} - 2ah} \over a} = {v_y + \sqrt {{v_y^2} + 2gh} \over g}$$

It works:

enter image description here enter image description here

If you fire from negative heights, be ready to get 2 positive roots, 2 negative roots or no roots at all.

  • \$\begingroup\$ It appears there is some problem with sin(a) . For example FMath::Sin(10); returns -0.54 but should 0.73 . Might be problem with FMath library ? \$\endgroup\$ – juzo4321 Jun 11 at 8:04
  • \$\begingroup\$ @juzo4321 convert degrees to radians and try again. \$\endgroup\$ – trollingchar Jun 11 at 8:09
  • \$\begingroup\$ @juzo4321 also, 0.73 is somewhere close to 45 degrees. 10 degrees will give an answer close to 0. \$\endgroup\$ – trollingchar Jun 11 at 8:12
  • \$\begingroup\$ Yes it require Radians so now it gives proper result but problem is with negative angles for example if it fires from 100 height with -20 angle and 10 velocity I get negative results \$\endgroup\$ – juzo4321 Jun 11 at 8:25
  • \$\begingroup\$ @juzo4321 the formula cannot give a negative result for positive height. Maybe there is a wrong sign somewhere in your implementation. Compare your implementation with my formula. \$\endgroup\$ – trollingchar Jun 11 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.