这里我正在计算演员和触摸点之间的度数,并将此度数传递给演员作为旋转角度。我希望演员应该指向手指触摸或手指拖动方向。(考虑我通过手指触摸拉动公交车在道路上拖动)
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或者不断地在上面的公式中需要改变。
答案 0 :(得分:3)
第一个参数必须是y,第二个参数是x。
double atan2(double y, double x)
Atan2实际上是指定y / x
的反正切函数因为通常第一个参数是y。
此外,当您交换x和y时,您可以获得适当的学位,因为在某些边界中,如x <0和y <0,它不会交换或不交换。
答案 1 :(得分:0)
结合问题和答案帮助我快速解决问题。只是为了澄清一点解决方案:
double degrees = Math.atan2(
towards.getY() - pointer.getY(),
towards.getX() - pointer.getX()
) * 180.0d / Math.PI;
pointer.setRotation((float) degrees);
另请注意,要围绕中间旋转,您需要执行以下操作:
pointer.setOrigin(Align.center);