godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
88.81k stars 20.14k forks source link

.rotated() of a Vector2/3 returns a result with floating point error #95855

Open AiksiLotl opened 3 weeks ago

AiksiLotl commented 3 weeks ago

Tested versions

System information

Godot v4.2.stable - Windows 10.0.19045 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3060 Laptop GPU (NVIDIA; 32.0.15.5599) - AMD Ryzen 5 5600H with Radeon Graphics (12 Threads)

Issue description

When rotating a Vector2 / Vector3 that has only the integer parts, by 90, 180 or 270 degrees, the result of Vector2.rotated() or Vector3.rotated() returns a value with a floating point error. When printing the result into the console, the result appears as if the floating point error isn't there (though there is a clue in a form of -0 showing up as one of the vector components sometimes, depending on the vector you'd be rotating). This results in unexpected, game breaking behaviour for projects that rely on precise integer grid calculations, which was the case for me.

Steps to reproduce

Open and start the MRP

Minimal reproduction project (MRP)

Vector.rotated() Bug showcase.zip

AThousandShips commented 3 weeks ago

This is to be expected, the method doesn't do any special treatment for specific angles (that'd be expensive)

For 2D, if you need these angles specifically I'd suggest just using the standard rotation formulas, like so (ccw rotation):

For 3D this is even more expected as you're dealing with axes as well as angles

Some notes could be added to the documentation though, but generally one should assume things have floating point errors