1

I made a get_angle function inside my Vector3 class but I have a problem with it.

The Y angle is perfectly fine.

The pitch angle (X) it returns is slightly over my destination position when the base vector is over it (and when under the opposite is happening).

The amount of it being wrong is dependant on the height difference.

Angle get_angle(const Vector3f& v) const { 
    return Angle(
        math::rad_to_deg(atan2(get_distance(v), v.z - z)) - 90.0f, 
        math::rad_to_deg(atan2(v.y - y, v.x - x)), 
        0.0f); 
}

It's probably my maths that are bad here.

Not Szwagi
  • 81
  • 2
  • 10

2 Answers2

1

I found a solution to my problem:

Angle get_angle(const Vector3f& v) const { 
    return Angle(
    math::rad_to_deg(
        atan2(
        sqrt(pow(X - v.X, 2) + pow(Y - v.Y, 2)), // the problem was here, 
                                                 // the distance between the vectors should not include the Z position distance in it
        v.Z - Z)) - 90.0f,

    math::rad_to_deg(atan2(v.Y - Y, v.X - X)), // this worked correctly

    0.0f // roll angle should always be 0.0f
    ); 
}
Not Szwagi
  • 81
  • 2
  • 10
0

What exactly are you trying to calculate? What does your "Angle" class represent? I guess you either want to:

  1. Calculate the angle between the two vectors, i.e. a single scalar value. The formula can be found here as cos(theta) == dot(*this, v) / (norm() * v.norm()). https://math.stackexchange.com/questions/974178/how-to-calculate-the-angle-between-2-vectors-in-3d-space-given-a-preset-function

  2. Or, convert both vectors to spherical coordinates (phi, theta), and calculate a delta for each phi and theta, i.e. you calculate two angles. The conversion formula from cartesian to spherical coordinates can be found here: https://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates

Community
  • 1
  • 1
mh8020
  • 1,784
  • 1
  • 11
  • 14
  • My Angle class represents the 3 angles from 3d space: pitch, yaw and roll. I fixed my problem though. Posted the problem it had in another post. – Not Szwagi Sep 01 '16 at 21:57