【问题标题】:Rotating over 360 degrees旋转超过 360 度
【发布时间】:2012-09-12 19:43:48
【问题描述】:

我有一个多边形,我想旋转 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): ****************

【问题讨论】:

  • 添加另一个变量来跟踪完整的旋转。
  • 重置了哪些角度? angle是怎么计算的?
  • @duffymo 我真的不想那样做,我真的希望角度从 360 到 361、362...
  • @NicoSchertler mobs 角度回到 0 左右,因为从 0 到 360 的旋转就像 -360,反之亦然
  • this.angle += angle; 看起来这不会将角度重置回 0,它应该已经做了你想要的。可能是显示错误?

标签: math geometry angle


【解决方案1】:

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

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

【讨论】:

  • 它有效!虽然循环在我眼中看起来很可怕 :e 非常感谢!
  • 理论上,if 就足够了。 while 适用于所有情况。
  • 嘘?模数怎么了?
  • 模数如何帮助我们?考虑到它时,我的大脑中出现了stackoverflow :(
猜你喜欢
  • 1970-01-01
  • 2013-02-01
  • 1970-01-01
  • 2012-12-06
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多