我正在做一個(可能很簡單)的任務,其中我想使繪製對象移動到用戶控制(繪製太)。我所有的是球員X和Ycoördinate,分別定義爲Xp和Yp。必須移動(在觸發器之後,不包括在代碼中)到'player-object'的對象在this.X和this.Y中定義了它的coördinates。C#2D矢量運動 - 速度和方向
int xDirection = Xp - this.X;
int yDirection = Yp - this.Y;
int angleInDegrees = (int)Math.Atan2(xDirection, yDirection);
double radians = (Math.PI/180) * angleInDegrees;
double xTmp = 3 * Math.Cos(radians);
int xSpeed = (int)xTmp;
double yTmp = 3 * Math.Sin(radians);
int ySpeed = (int)yTmp;
Console.WriteLine(xDirection);
Console.WriteLine(yDirection);
Console.WriteLine(xSpeed);
Console.WriteLine(ySpeed);
Console.ReadLine();
這不給我正確的數字,所以我想知道什麼可能是錯的。
關於這個最棘手的問題可能是必須移動到playerobject的對象可能從所有邊(360度)接近,但沒有可用的方法角度。
我希望能完成我的問題, 添
只是可能導致問題的一些事情。您的XY座標,速度和角度_應該被視爲雙打。通過使用整數,會引入相當多的蠕變和舍入誤差(特別是在處理弧度時)。您還可以考慮引入明確的Degrees/Radians類以避免可能的混合兩者的情況。另外,考慮利用極座標來表示速度(角度+速度)。這裏既是樣本:http://stackoverflow.com/questions/12126907/when-should-i-define-a-explicit-or-implicit-conversion-operator-in-c/12232002#12232002 –