c# - 使用 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 可能会有所帮助,但我无法真正让它发挥作用
解决方案
推荐阅读
- python - 我可以使用 IPython embed() 来更改函数的状态吗?
- cassandra - 如何在 Cassandra 中建模天气数据?
- officer - 在官员中插入自定义目录的最佳方法
- html - 如何在焦点上删除textarea元素内的奇怪轮廓
- reactjs - Next.js 在 getServerSideProps 中使用“ReferenceError”导入函数错误
- filter - 如何使用 do 和 "by" 重写这个不推荐使用的表达式,以及 "groupby" (Julia)
- node.js - 对 BeagleBone 进行编程以打开 LED,出现 NodeJS 错误
- hbase - 在 HBase 中自动分发数据(无需预拆分)
- c - 如何解释字符串中的反斜杠?
- docker - 由于您的应用程序中发生未处理的异常,Docker Desktop 无法启动