nalgebra
nalgebra copied to clipboard
How to get a signed angle ?
Hi,
I want to calculate the orientation of a Vector2. The sign of the angle matters, but vector2.angle(Vector::x()) doesn't seem to keep that information.
example:
// assertion fails
assert_eq!(
na::Vector2::new(0f32, -1f32).angle(&na::Vector2::x()),
-std::f32::consts::FRAC_PI_2
);
Is there a way to do it with nalgebra, maybe with Rotation ? Or should I just do it by hand ?
@midnightexigent You were right when you said you need to use Rotation but you could use UnitComplex which is what the Rotation2 method would use if you chose to go that route
UnitComplex::rotation_between(&Vector2::x(), &Vector2::new(0f32,-1f32)).angle()
this gives out -1.5707964
this function is not commutative. Switch the order and the sign gets flipped
links:
- https://github.com/dimforge/nalgebra/blob/dev/src/geometry/unit_complex_construction.rs#L301
- https://github.com/dimforge/nalgebra/blob/dev/src/geometry/rotation_specialization.rs#L136
- https://math.stackexchange.com/questions/317874/calculate-the-angle-between-two-vectors
Thank you !