计算异常 2D 中点的角度的函数 space

Function to calculate angle to a point in unusual 2D space

我正在寻找一个强大的函数来计算对象和点之间的差异(增量)。

例如,如果在 A 点有一个方向为 1.2 Rad 的物体,为了面向 B 点,物体转动所需的角度是多少。

此外,我在一个奇怪的坐标系中工作,其中北(0 Rad)面向 +X,下图显示了这一点。

我了解基本原理,但我正在努力制作一些可靠的东西。

我的 C++ 函数模板是这样的,

    float Robot::getDeltaHeading(float _x1, float _y1, float _x2, float _y2, float _currentHeading) {

    //TODO:

    return xxxxxxx;
}

如有任何帮助,我们将不胜感激。

提前干杯。

答案在这里。

float Robot::getDeltaHeading(float _x1, float _y1, float _x2, float _y2, float _currentHeading) {

    _currentHeading -= 90;

    double Ux = 0.0, Uy = 0.0, Vx = 0.0, Vy = 0.0, d = 0.0;

    d = sqrtf(powf(abs(_x1 - _x2), 2) + powf(abs(_y1 - _x2), 2));

    Ux = (_x2 - _x1) / d;
    Uy = (_y2 - _y1) / d;

    Vx = cos(_currentHeading * (3.14159f / 180.0));
    Vy = sin(_currentHeading * (3.14159f / 180.0));

    auto ans = 90 + (atan2(((Ux * Vy) - (Uy * Vx)), ((Ux * Vx) + (Uy * Vy))) * (180.0 / 3.14159f));

    while (ans > 180) ans -= 360;
    while (ans < -180) ans += 360;

    return ans;
}