3

在这里,我正在计算演员和触摸点之间的度数,并将这个度数作为旋转角度传递给演员。我希望演员应该指向手指触摸或手指拖动方向。(考虑我通过手指触摸和拖动在路上拉公共汽车)

float degrees = (float) ((Math.atan2(touchPoint.x - crocodile.position.x, -(touchPoint.y - crocodile.position.y)) * 180.0d / Math.PI));

其计算度数正确。只是有时在两者之间它给出了不同的程度。所以当结果真的与预期不同时,我的演员就会波动。

我打印的结果是:

块引用

    degree is :: 141.93233
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 158.61426
    degree is :: 90.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 90.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93057
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93306
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93134
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93134
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93306
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 141.93134
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 141.93134
    degree is :: 180.0
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93008
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93306
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93257
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 141.93134
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0
    degree is :: 180.0

现在,180 和 141 为什么会随着这些值波动。考虑到我一直期待 141 或 180 我需要在上述公式中进行更改。

4

2 回答 2

5

第一个参数必须是 y,第二个是 x。

double atan2(double y, double x)

Atan2 实际上是指定 y/x 的反正切函数

因此,通常第一个参数是 y。

此外,有时当您交换 x 和 y 时,您可以获得适当的程度,因为在某些边界(如 x<0 和 y<0)中,交换与否无关紧要。

看这里以获得更多解释

于 2015-03-26T18:48:13.607 回答
1

结合问题和答案帮助我快速解决了我的问题。只是为了澄清一下解决方案:

double degrees = Math.atan2(
    towards.getY() - pointer.getY(),
    towards.getX() - pointer.getX()
) * 180.0d / Math.PI;
pointer.setRotation((float) degrees);

另请注意,为了围绕中间旋转,您需要执行以下操作:

pointer.setOrigin(Align.center);
于 2015-09-25T04:48:50.313 回答