将对象转向给定坐标
我目前有一个物体在太空中飞行,我想让该物体转向给定的坐标。
由于某种原因,物体不停地旋转而不是自我调整。
我的三角学几乎不存在,到目前为止,我一直在使用猜测工作,所以请提供基本的解释:-/
public void TurnTowardsDestination(double DestinationX, double DestinationY)
{
//Current Co-Ordinate of the object.
double positionX = x;
double positionY = y;
//My failed attempt at understanding atan2.
float spriteToDestAngle = (float) Math.toDegrees(Math.atan2((positionX - DestinationX),(positionY - DestinationY)));
//The Rotate, true is clockwise false anti-clockwise.
if(spriteToDestAngle > 0.0){
RotateItem(true);
}else{
RotateItem(false);
}
Log.w("direction", Integer.toString((int) spriteToDestAngle));
}
有人可以给我指出正确的教程,或者至少解释一下我如何让物体朝正确的方向移动。
我使用了一个有效的修复程序:(对于其他坚持这个问题的人) 从: http://sinepost.wordpress.com/ 2012/02/16/他们有阿坦你想要阿坦2/
int distX = (int) (DestinationX - x);
int distY = (int) (DestinationY - y);
double angleRadians = Math.atan2(distY, distX);
int angleDegrees = (int)Math.toDegrees(angleRadians);
//setRotation(angleDegrees);
I currently have an object flying though space, I want to make the object turn towards a given co-ordinate.
For some reason the object is spinning round and round instead of adjusting itself.
My trigganometry is almost non-existent and so far I've been using guess work so basic explanation please :-/
public void TurnTowardsDestination(double DestinationX, double DestinationY)
{
//Current Co-Ordinate of the object.
double positionX = x;
double positionY = y;
//My failed attempt at understanding atan2.
float spriteToDestAngle = (float) Math.toDegrees(Math.atan2((positionX - DestinationX),(positionY - DestinationY)));
//The Rotate, true is clockwise false anti-clockwise.
if(spriteToDestAngle > 0.0){
RotateItem(true);
}else{
RotateItem(false);
}
Log.w("direction", Integer.toString((int) spriteToDestAngle));
}
Could someone point me to the right tutorial on this or at least explain how I would get the object to move in the right direction.
I used a fix that works: (For anyone else stuck on this)
From:
http://sinepost.wordpress.com/2012/02/16/theyve-got-atan-you-want-atan2/
int distX = (int) (DestinationX - x);
int distY = (int) (DestinationY - y);
double angleRadians = Math.atan2(distY, distX);
int angleDegrees = (int)Math.toDegrees(angleRadians);
//setRotation(angleDegrees);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Neil Brown 有一篇关于此的综合文章作者< /a>. Neil 正在使用 Greenfoot 进行编写,它基于 Java,因此移植示例代码应该非常简单。
There's a comprehensive article on this by Neil Brown. Neil is writing in Greenfoot, which is based on Java so it should be quite straight forward to port the example code.
您可能会发现
Line2D.relativeCCW(Point2D)
对于确定一个点相对于给定向量是顺时针还是逆时针非常有用。You might find
Line2D.relativeCCW(Point2D)
to be very useful in determining whether a point is clockwise or counterclockwise from a given vector.我只想指出,你的三角函数实际上非常合理,只是你的逻辑不正确。例如,如果您的目的地位于当前位置的正上方,则 spriteToDestAngle 将为 90。即使您直指它,您的代码也表明它应该旋转。您应该存储当前方向并与之进行比较。
编辑:
澄清一下,你从来没有考虑过你当前的方向。您仅根据位置进行轮换。
I would just like to point out that your trig here is actually pretty sound, just your logic is incorrect. For example, if your destination is right above the current position, then the spriteToDestAngle will be 90. Even if you are pointing straight at it, your code states it should rotate. You should store the current direction and compare to that.
EDIT:
To clarify, you never take into account your current direction. You rotate based solely on position.