站立时面向前方的两个点之间的旋转面向前方(图片)

时间:2016-04-19 16:08:44

标签: c++11

通过这段小代码,我可以将“怪物”旋转到化身上,但如果化身位于“怪物”后面,那么“怪物”就会远离化身。 (图片如下)

注意:白色数字是m_RotationAngle

的值
DOUBLE2 mousePos = GAME_ENGINE->GetMousePosition();

double xDiff = m_ActPtr->GetPosition().x - mousePos.x;
double yDiff = m_ActPtr->GetPosition().y - mousePos.y;
m_RotationAngle = atan(yDiff, xDiff);
m_ActPtr->SetAngle(m_RotationAngle);

Avatar standing behind monsters Avatar standing infront of monsters

我试图用以下方法修复它:

if (diff.x < 0)
{
    m_RotationAngle = -atan(diff.y / diff.x);
    //also tried following but gave and error:
    //m_RotationAngle = tan(diff.y / diff.x);
}
else
{
    m_RotationAngle = atan(diff.y / diff.x);
}

但这给出了以下输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可能正在寻找atan2(yDiff, xDiff);,它使用参数的符号计算yDiff / xDiff的反正切,以确定正确的象限,而不是atan(也只需要一个参数)。< / p>

请注意,结果在[-π; +π]弧度,而不是度数。