Facepunch / sbox-issues

173 stars 11 forks source link

Transform.Rotation.Forward outputs weird numbers at certain angles #5708

Open TheOnlyThing opened 2 months ago

TheOnlyThing commented 2 months ago

Describe the bug

when your object is at a certain angle, Transform.Rotation.Forward outputs numbers like 5.9604645E-08

To Reproduce

put code Log.Info(Transform.Rotation.Forward.x); in a object rotate Yaw to 90 degrees

Expected behavior

output like 0 or something idk

Media/Files

https://github.com/Facepunch/sbox-issues/assets/90431059/fa32d036-7c84-4070-be18-1b95ba139834

Additional context

I tested with Z aswell and that one goes weird at pitch 180 degrees

MD485 commented 2 months ago

There might not be a bug here, often times tiny rotations like 5.9604645E-08 or 0.000000059604645 are just imprecision. If you do Log.Info( (Transform.Rotation.Forward.x / 0.0001f).Floor() * 0.0001f );, you'll get 0 as you expect. The reason I'm rounding using 0.0001f is because that's the default facepunch uses when you call .AlmostEqual() on a float.

You can verify this by checking the value 90.001, which returns -0.000017523766. And the value 89.999, which returns 0.000017404556.

While not identical, they're almost equally far from 0. 0.001 is the smallest degree you can change the axis to have a visible change in both directions for the forward x result. Although 89.9999 does produce a different result, the same isn't true for 90.0001 which just returns the same value as 90.

Logically 90 degrees should always return 0 on the forward though, maybe some double checking on the order of operations or something could get rid of the tiny values on the x axis.