我目前有一个物体在空间中飞行,我想让物体转向给定的坐标。将对象转向给定的坐标
由于某些原因,对象旋转而不是自我调整。
我trigganometry几乎是不存在的,到目前为止,我一直在使用的猜测工作,所以基本的解释,请: -/
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/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);
试图做任何形式的计算机图形没有基本的几何和三角学知识将是非常不积极的。我强烈建议你学习一些! – 2012-02-19 17:08:27
你能推荐一些很好的教程网站吗? – 2012-02-19 17:11:47
我建议购买例如这是一本高中数学教科书,并且通过练习来完成。 – 2012-02-19 17:53:05