我目前有一個物體在空間中飛行,我想讓物體轉向給定的座標。將對象轉向給定的座標
由於某些原因,對象旋轉而不是自我調整。
我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