使用旋轉矩陣。此代碼將從一個點(X,Y)由THETA弧度移動到新的點(PX,像素)
Point Rotate(x, y, theta)
int px = (x * Math.Cos(theta)) - (y * Math.Sin(theta));
int py = (y * Math.Cos(theta)) + (x * Math.Sin(theta));
return new Point(px, py);
end
上面所用的矩陣,
[cosθ - sinθ][x]
[cosθ + sinθ][y]
可否移動周圍的圓上的點使用圖形座標時順時針旋轉
上週我做了同樣的事情。您可以通過查找您希望移動的全部theta,然後將其除以幀數(或步數)來設置動畫。現在,在某個任意點(例如(0,半徑))處開始每一步,然後增加一些計數器totalSteps,並始終從該初始點開始移動。如果你簡單地移動點本身,每一幀你都會累積一些錯誤,但是如果你總是從當前增量開始移動,停止增量== totalTheta時,它會是完美的。讓我知道這是否合理。
也許我應該多說明一點。比方說,你有一個方法「BeginMove」:
double totalTheta = 0;
double increment = 0;
double currentTheta = 0;
bool moving = false;
void BeginMove()
{
totalTheta = (2 * Math.PI)/numObjects;
increment = totalTheta/steps;
currentTheta = 0;
moving = true;
}
現在你有哪些更新,此舉每一幀的方法:
void Update
{
if (!moving) return;
// do a min/max to ensure that you never pass totalTheta when incrementing.
// there will be more error handling, but this is the basic idea.
currentTheta += increment;
SomeObject.Location = Rotate(0, radius, currentTheta);
moving = (currentTheta < totalTheta);
}
有顯然會更加這裏的邏輯根據您的具體情況,但這個想法是:
- 找到總共移動的theta。
- 找到增量(totalTheta/steps)
- 維持一個已經移動的總數。
- 在每次移動之前將跑步總數增加角度增量。
- 從圓上的相同(任意)點開始每個移動,並按運行總數旋轉。
- 重複,直到運行總數==總θ。
你需要告訴我們哪些角度你正在使用的三角形。從左到騎?從下到上? – 2010-06-17 17:15:52
你使用度數還是弧度? – luke 2010-06-17 17:35:16
「degress」,從0到360,或者我有一個0(北),90(東),180(南)和-90(西)的方法。這些值我傳遞(我猜誤導參數)「角」 – jaysonragasa 2010-06-17 17:44:16