Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MotionCollision get_angle() returns NAN #76857

Closed
mschwarz1 opened this issue May 8, 2023 · 1 comment · Fixed by #76906
Closed

MotionCollision get_angle() returns NAN #76857

mschwarz1 opened this issue May 8, 2023 · 1 comment · Fixed by #76906

Comments

@mschwarz1
Copy link

Godot version

master (e684d12)

System information

Windows 10

Issue description

MotionCollision's get_angle() function can return NAN. According to the docs it should always return a positive value.

I believe I'm running into the same scenario as #8111 as applying a CLAMP within the acos() seems to fix the issue.

Steps to reproduce

In my case I had a platform that could rotate along the z axis and an independent character controller who's gravity would automatically adjust to the down vector of the rotating platform and the character controller would rotate towards the gravity so it's down vector also aligned with the platform's.

Using get_angle against the ground from the player's up direction would occasionally result in NAN at various platform rotations despite the vectors being perpendicular and normalized.

It would also seemingly result in weird behavior in move_and_slide() where the velocity would continuously have gravity added to it despite the character controller not moving.

In the minimal reproduction project there's a single scene with a platform that's named something along the lines of rotate me. If you run that project and slowly rotate the z axis from the remote tree eventually you should see an instance where "nan!" gets printed indicating it ran into the issue. It's very finicky and the character controller can fall through the platform so it might take a few tries.

Minimal reproduction project

GetAngle.zip

@lawnjelly
Copy link
Member

lawnjelly commented May 9, 2023

Ah, this has come up before, imo we should be using a safe_acos() function in the majority of cases in the engine (with input clamped). It's very easy for float error to get outside the -1 to 1 range and cause Nan, and clamping is pretty cheap outside bottleneck code.

EDIT:
Yeah this bug is everywhere. In Godot case I think it might be better to make the default function safe, and add an extra unsafe_acos function for the rare occasions where it isn't needed. Same applies for asin, not sure if there is a similar conditions for other math funcs (maybe safe_sqrt), but I've most commonly seen it occur for acos.

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

Successfully merging a pull request may close this issue.

4 participants