How is this code even working? Pitch and Yaw

Viewed 160

I was recently looking up how to create simple bot to the game and there are 2 methods to do that 1 is to calculate pitch and yaw using atan2 and sin for example

but there's also another method how peoples calculate it and my question is how it even work ? This method I fully understand

code:

void CalcAngle(float *src, float *dst, float *angles)
{
    double delta[3] = {(src[0] - dst[0]), (src[1] - dst[1]), (src[2] - dst[2])};

    int x = 0;
    int y = 1;
    int z = 2;

    float hyp = delta[x] * delta[x] + delta[y] * delta[y];
    angles[0] = asinf(delta[z] / hyp) * 180 / pi;
    angles[1] = atanf(delta[y] / delta[x]) * 180 / pi;

    if(angles[0] > 0.0)
    {
        angles[1] += 180;
    }
}

and my imagination how peoples are doing that Second which I don't understand

If someone knows what's going on with this code, can you explain to me how it calculates valid values. For example, it can't calculate 3rd and 2nd quadrant angles, so how it even works for others? and why they are calculating the adjacent of triangle with opposite instead of opposite with hypotenuse ? I wouldn't ask if this code would be a single case, but I already saw it in many cases.

For be more clear

1 Answers

The code indeed seems to be wrong. You should use atan for pitch and atan2 for yaw.

double hyp = sqrt(delta[x] * delta[x] + delta[y] * delta[y]);
angles[0] = atan(delta[z] / hyp) * 180 / pi; // Pitch
angles[1] = atan2(delta[y] / delta[x]) * 180 / pi;  // Yaw
if(angles[1] < 0.0)
    angles[1] += 180;
Related