nalgebra icon indicating copy to clipboard operation
nalgebra copied to clipboard

How to get a signed angle ?

Open midnightexigent opened this issue 3 years ago • 2 comments

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 avatar Jan 13 '22 14:01 midnightexigent

@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:

  1. https://github.com/dimforge/nalgebra/blob/dev/src/geometry/unit_complex_construction.rs#L301
  2. https://github.com/dimforge/nalgebra/blob/dev/src/geometry/rotation_specialization.rs#L136
  3. https://math.stackexchange.com/questions/317874/calculate-the-angle-between-two-vectors

metric-space avatar Jan 14 '22 00:01 metric-space

Thank you !

midnightexigent avatar Jan 14 '22 07:01 midnightexigent