2

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

AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
Awynon X
  • 39
  • 3
  • 1
    The math is indeed just wrong. I googled the code you posted: https://www.unknowncheats.me/forum/counterstrike-global-offensive/170226-explaining-calcangle.html it seems some hacking community and they are not good at math but good at copy-pasting wrong code: `good job op spoon-feeding these newbs who didn't pay attention to high school maths classes.` – dewaffled May 26 '21 at 23:18
  • I can't read the handwriting. I managed to decypher "atan²" but what is "aɔim"? – JDługosz May 27 '21 at 15:05
  • Use atan2. Always use atan2. Do you want to use atan or asin? Stop, think again. The only reverse trigonometric function you want is atan2, no exceptions. – n. m. could be an AI May 30 '21 at 07:52

1 Answers1

1

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;
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52