旋转360度

时间:2012-09-12 19:43:48

标签: math geometry angle

我有一个多边形,我想要旋转360度以上,但仍然跟踪整个回合。

我将手指的角度定义为可变角度,我知道我正在旋转的怪物的角度,但是每转完一圈之后角度就会重置,因为触摸点和暴徒之间的角度从360变为0或者副反之亦然。

我怎样才能解决这个问题?

当前代码:

double rotation = angle - lastAngle;
selectedMob.rotate((float) (rotation));

其中angle是触摸点和mob之间的角度,lastAngle是此前的角度。

方法rotate只是将旋转添加到当前角度,如下所示:

public void rotate(float angle) {
    this.angle += angle;
}

角度定义为

                double rads = Math.atan2(dy, dx);
                double angle = Math.toDegrees(rads);

然后我调整角度,使其从0开始向上,但我确保它从0到360

调试:

09-13 00:03:53.708: V/GameActivity(23389): updating frame
09-13 00:03:53.708: V/GameActivity(23389): checked for all mobs
09-13 00:03:53.825: V/GameActivity(23389): Angle of finger is 360.0
09-13 00:03:53.825: V/GameActivity(23389): Angle of mob is 344.94818
09-13 00:03:53.825: V/GameActivity(23389): Last angle is 359.56921278299137
09-13 00:03:53.825: V/GameActivity(23389): Rotation will be 0.43078721700862843
09-13 00:03:53.825: V/GameActivity(23389): ****************
09-13 00:03:53.825: V/GameActivity(23389): updating frame
09-13 00:03:53.825: V/GameActivity(23389): checked for all mobs
09-13 00:03:54.137: V/GameActivity(23389): Angle of finger is 0.4275725068334077
09-13 00:03:54.137: V/GameActivity(23389): Angle of mob is 345.37897
09-13 00:03:54.137: V/GameActivity(23389): Last angle is 360.0
09-13 00:03:54.137: V/GameActivity(23389): Rotation will be -359.5724274931666
09-13 00:03:54.137: V/GameActivity(23389): ****************

1 个答案:

答案 0 :(得分:1)

您可以将当前角度修改为与lastAngle相同的范围:

while(Math.Abs(angle - lastAngle) > 180)
    angle += (angle > lastAngle ? -360 : 360);