首页 > 解决方案 > 使用 ArcTan2 在 +- 180 度边界上旋转线

问题描述

我有一条线以设定的速度(每次计算的 x 度 = 旋转角度)旋转到当前鼠标位置。

我通过使用鼠标光标计算线的当前角度和目标角度来做到这一点。

比我根据这个逻辑设置新的当前角度:

            if (this.currentAangle < this.targetAngle)
            {
                if((this.currentAangle + this.rotatingAngle)> this.targetAngle)
                {
                    this.currentAangle = this.targetAngle;
                }
                else
                {
                    this.currentAangle += this.rotatingAngle;
                }
            } 
            else
            {
                if ((this.currentAangle - this.rotatingAngle) < this.targetAngle)
                {
                    this.currentAangle = this.targetAngle;
                }
                else
                {
                    this.currentAangle -= this.rotatingAngle;
                }
            }

所以如果当前角度和目标角度的差距大于转速,就加上转速。如果较小,则将当前角度设置为目标角度。

这种方法几乎适用于所有角度。除了当前和目标角度在左侧的情况(一只蜜蜂在 -180 左右,另一只蜜蜂在 180 左右)

所以我相应地更改了我的代码:

            if (Math.Sign(this.targetAngle) != Math.Sign(this.currentAangle) && Math.Abs(this.targetAngle)>2)
            {
                Signum = -1;
            }

            if (this.currentAangle < this.targetAngle * Signum)
            {
                if((this.currentAangle + this.rotatingAngle)> this.targetAngle)
                {
                    this.currentAangle = this.targetAngle;
                }
                else
                {
                    this.currentAangle += this.rotatingAngle;
                }
            } 
            else
            {
                if ((this.currentAangle - this.rotatingAngle) < this.targetAngle)
                {

                    this.currentAangle = this.targetAngle;
                }
                else
                {
                    this.currentAangle -= this.rotatingAngle;
                }
            }

但这只能解决一部分问题。如果我将鼠标“缓慢”移动到“9 点钟”行。它完美地工作。但是,如果我快速移动鼠标,它会立即“跳”到目标。

我觉得这是由于计算错误造成的。我想我可以用很多 if-else 子句来解决这个问题。

有没有办法更聪明地解决这个问题?有一些数学?我的感觉告诉我,在某处添加一些 2*PI 可能会有所帮助,但我无法真正让它发挥作用

标签: c#mathrotationgeometry

解决方案


推荐阅读