从角度和角度获得较低的旋转值程度
public void TurnTowardsPoint(double DestinationX, double DestinationY)
{
int distX = (int) (DestinationX - x);
int distY = (int) (DestinationY - y);
double angleRadians = Math.atan2(distY, distX);
int angleDegrees = (int)Math.toDegrees(angleRadians);
//angleDegrees = Math.abs(angleDegrees);
Log.w("Angle", Integer.toString(angleDegrees));
Log.w("Degrees", Integer.toString((int) this._degrees));
if((this._degrees) - 360 != angleDegrees)
{
if(this._degrees >= 0)
{
RotateItem(true);
}
else
{
RotateItem(false);
}
}
}
RotateItem(true 是顺时针方向 false 是反方向,它永远不能减,否则它重置为 0,尽管这是可以改变的)。
我想要实现的是获得最短的旋转和旋转方式。当它指向角度原点时也完成。
我已经研究了几个小时了,现在我的问题是我从未在学校教过三角学,所以我正在努力解决这些概念和问题。了解何时使用什么。 (我一直在阅读它)。
我该怎么做?我在网上找不到任何东西,所以我最终不得不放弃,并在盯着一张纸两个小时但没有得到任何结果后向你们询问。
笔记: 船面向右,角度为 360/0D。 如果船舶位于该点正下方,则为 -90,在该点上方为 +90。如果船直接向右,则为 180D。
问题出在 2 个 if 语句上。第一个 if 语句需要在指向角度时停止旋转,第二个 if 语句应该决定哪条路最短。这2种说法都是错误的。
问题出在这一部分:
if((this._degrees) - 360 != angleDegrees)
{
if(this._degrees >= 0)
{
RotateItem(true);
}
else
{
RotateItem(false);
}
}
public void TurnTowardsPoint(double DestinationX, double DestinationY)
{
int distX = (int) (DestinationX - x);
int distY = (int) (DestinationY - y);
double angleRadians = Math.atan2(distY, distX);
int angleDegrees = (int)Math.toDegrees(angleRadians);
//angleDegrees = Math.abs(angleDegrees);
Log.w("Angle", Integer.toString(angleDegrees));
Log.w("Degrees", Integer.toString((int) this._degrees));
if((this._degrees) - 360 != angleDegrees)
{
if(this._degrees >= 0)
{
RotateItem(true);
}
else
{
RotateItem(false);
}
}
}
RotateItem(true is clockwise false is anti, it can never go minus otherwise it resets to 0, although this is changeable).
What I'm trying to achieve is to get the shortest way to rotate & also finish when it's pointing at the Angle point of origin.
I've been on this for a few hours now my problem is I never got taught Trigonometry at school so I'm struggling with the concepts & understanding what to use when. (I've been reading up on it).
How would I do this? I can't find anything online so I had to eventually give up and asked you guys after staring at a piece of paper for 2 hours and getting no where.
Notes:
Facing right the ship is 360/0D.
If the ship is directly under the point it is -90, above it is +90. If the ship is to the right directly it is 180D.
The problem lies with the 2 if statements. The First if statement needs to stop the rotation when it's pointing at the angle, the second one is suppose to decide which way is shortest. Those 2 statements are wrong.
The problem lies with this part:
if((this._degrees) - 360 != angleDegrees)
{
if(this._degrees >= 0)
{
RotateItem(true);
}
else
{
RotateItem(false);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,如果我没有理解错的话,你有
this._ Degrees
,它给出了你的对象当前指向的方向,以整数度为单位,标准化为 0(含)和 360(含)之间或独家?)。然后,您可以将旋转后对象指向的方向计算为angleDegrees
。如果我理解正确的话,你在计算中有一个符号错误,因为约定是逆时针转动为正方向(但如果你的 Y 轴有 0 以上 1,这将抵消,你必须检查并确定
angleDegrees
是否应该或必须被否定)。无论如何,
angleDegrees
给出了您想要转向的方向,范围从 -180 到 180 度。将其标准化,使方向在 0 到 360 范围内,现在,可以通过减法获得转动的量和方向,
该值位于 -360 到 360 度之间(更准确地说是在
-this._ Degrees 之间)
和angleDegrees
)。而且它可能还没有给出最短的转弯,如果差值小于-180度或大于180度,则超过半圈,而另一种方式会更短,因此我们再次归一化,现在转弯位于-180之间和 180 并给出到所需方向的最短转弯。如果
turn >= 0
,您想顺时针旋转,否则逆时针旋转,现在您只需旋转
abs(turn)
度,例如Okay, if I haven't misunderstood, you have
this._degrees
, which gives the direction your object is currently pointing in, in integer degrees, normalized to lie between 0 (inclusive) and 360 (in- or exclusive?). And you calculate the direction in which the object shall point after rotating asangleDegrees
.If I'm understanding correctly, you have a sign error in that calculation because the convention is for counterclockwise turns to be the positive direction (but if your Y-axis has 0 above 1, that would cancel out, you have to check and determine whether
angleDegrees
is as it should or has to be negated).Anyway,
angleDegrees
gives the direction you want to turn to, in the range from -180 to 180 degrees. Normalize it so that the direction lies in the range 0 to 360,Now, the amount and direction in which to turn can be obtained by subtraction,
That value lies between -360 and 360 degrees (more precisely between
-this._degrees
andangleDegrees
). And it may not give the shortest turn yet, if the difference is smaller than -180 degrees or larger than 180, it's more than a half turn and the other way would be shorter, therefore we normalize once more,Now turn lies between -180 and 180 and gives the shortest turn to the desired direction. If
turn >= 0
, you want to turn clockwise, else counterclockwiseand now you only have to rotate
abs(turn)
degrees, e.g.