godot-rust / gdnative

Rust bindings for Godot 3
https://godot-rust.github.io
MIT License
3.61k stars 210 forks source link

Error in Vector2::rotated(f32) #951

Closed carmel4a closed 2 years ago

carmel4a commented 2 years ago

Hi, there are 2 options. I'm tired because it's late, or there's error in Vector2 rotating formula. If I'm correct, the formula is: cos(a) * x1 + sin(a) * y1; sin(a) * x1 + cos(a) * y1 but should be: cos(a) * x1 - sin(a) * y1; sin(a) * x1 + cos(a) * y1 ____________^ Example:

use gdnative::prelude::*;
fn foo() {
    let v1: Vector2 = Vector::new(100.0, 100.0);
    let v1: Vector2 = v1.normalized(); // Vector2 { x: 0.7071068, y: 0.7071068 }
    let v1: Vector2 = v1.rotated(PI / 2.0); // Vector2 { x: 0.70710677, y: 0.70710677 }
}

Tested on gdnative = "0.10.1" Fix should be easy, but we should check if in similar methods there aren't any similar errors. Thanks.

Bromeon commented 2 years ago

That's quite a silly bug. In the future we should probably delegate more of these methods to the underlying glam, now that there's also glam::Vec2::rotate().

Thanks a lot for the report! 🙂 I'll see in the next days if I find other similar issues.