【发布时间】:2015-03-06 23:12:27
【问题描述】:
比如我现在的角度位置是170,我点击物体旋转到-170度,这里的问题是178、179、180之后,下一个数字是-180、-179、-178和等等……
即使在数字上很难 170 度也远非 -170 度,但在视觉上它们看起来很近,一个物体正在旋转最长的方式以达到该数字,例如:
if(currentAngle < targetAngle)
{
currentAngle += 1;
}
if(currentAngle > targetAngle)
{
currentAngle -= 1;
}
这样我可以达到目标角度,但是又如何穿越180到-180之间的这个障碍,也许有一个公式?
更新:
onclick() {
double angle = Math.atan2(dy, dx);
targetAngle = (int)Math.toDegrees(angle); //180 to //-179
}
onFrame() {
//here happens the animation
if(currentAngle < targetAngle)
{
currentAngle +=1;
}
if(currentAngle > targetAngle)
{
currentAngle -= 1;
}
}
现在,如果我当前处于 -179 度角,并且我点击了 160 度角,它应该向左旋转以尽可能快地达到该角度,但在我的情况下它正在向右旋转(更长),那是因为它被限制在 -180 到 179 之间,因此如何打破限制并从 179 到 -180、-181、-182...如果你明白我的意思
1) 我的点击处理程序:
onClick() {
double angle = Math.atan2(dy, dx);
angle = (int)Math.toDegrees(angle);
Log.d("test", "angler:" + angle);
if(angle < 0)
angle += 360;
}
所以在这里我使用角度 += 360 将度数转换为正数,然后:
2) 我的 onFrame 处理程序:
onFrame() {
if(currentAngle != angle)
{
if(angle < currentAngle)
{
currentAngle -= 5;
}
else
{
currentAngle += 5;
}
int diff = Math.abs(angle - currentAngle);
if(diff <= 5)
{
currentAngle = angle; // if its near we put it exact to that angle
}
invalidate(); //update the view
}
}
这就是我的全部
【问题讨论】:
-
你只需要一种编程语言的答案。
-
@StephenC,不,因为在许多编程语言中都会遇到这种情况,我什至不知道这可能指的是哪种编程语言,这就是为什么我用 java、c# 和 actionscript 标记它的原因跨度>
-
在这种情况下,你需要一个算法;即没有特定语言的解决方案。
-
你计算当前角度和目标角度之间的距离,然后选择较小的那个。
-
@user4642850 - 够了“我是菜鸟”的东西。这只需要您>>考虑一下