Skip to content

Conversation

kostasvs
Copy link

TimeOfFlight currently returns NaN in some cases, particularly when firing angle is low and the target Y offset is negative.

The function has to solve a quadradic and it gets the solution for the positive root, but sometimes the correct solution is the one for negative root.

Example for testing:

var durationTest = ProjectileMath.TimeOfFlight(5, 5 * Mathf.Deg2Rad, -1.5f, 9.8);
Debug.Log($"angle: 5°, speed: 5m/s, offsetY: -1.5m, gravity: 9.8m/s², duration: {durationTest:F2}s");

Using current implementation, the TimeOfFlight returns NaN and the output is:

angle: 5°, speed: 5m/s, offsetY: -1.5m, gravity: 9,8m/s², duration: NaNs

Using this PR's implementation, TimeOfFlight returns correct result, and the output becomes:

angle: 5°, speed: 5m/s, offsetY: -1.5m, gravity: 9,8m/s², duration: 0,60s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant