2016-10-07 174 views
1

我想圍繞原點旋轉4點。當我不旋轉點時,它們出現在正確的位置。我做的是這樣的:圍繞原點旋轉點

let origin = this.transform.position; 
for (let i in this._pointsOrig) { 
    let point = this._pointsOrig[i]; 
    this._points[i] = new Vector2(
     point.x + origin.x, 
     point.y + origin.y 
    ); 
} 

,給了我這樣的結果:

注:藍十字爲原點,和綠色的矩形輪廓點

Collider not rotated

當我嘗試旋轉矩形時,如下所示:

// degrees is between 0 & 360, so convert to radians 
let rotation = this.transform.rotation.degrees * (Math.PI/180); 
let cos = Math.cos(rotation); 
let sin = Math.sin(rotation); 
let origin = this.transform.position; 

for (let i in this._pointsOrig) { 
    let point = this._pointsOrig[i]; 
    this._points[i] = new Vector2(
     (cos * (point.x - origin.x)) + (sin * (point.y - origin.y)) + origin.x, 
     (cos * (point.y - origin.y)) - (sin * (point.x - origin.x)) + origin.y 
    ); 
} 

我得到這個矩形現在在左下角的結果。

Collider Rotated

我不知道我需要做這樣的點圍繞原點旋轉。

回答

2

我會嘗試使用

(cos * point.x) + (sin * point.y) + origin.x, 
(cos * point.y) - (sin * point.x) + origin.y 

它看起來像point.x和point.y由(origin.x,origin.y)轉移之前的值。所以在旋轉之前移動座標具有不希望的效果。

您可能會發現旋轉是在錯誤的方向

(cos * point.x) - (sin * point.y) + origin.x, 
(cos * point.y) + (sin * point.x) + origin.y 

所以改變的跡象可以工作。

如果旋轉常是90°,我們知道的比COS(90°)= 0,罪(90°)= 1,以便將其簡化

this.points[i] = new Vector2(
    - point.y + origin.x, 
     point.x + origin.y); 

如果問題仍然存在,可能幫助包括一些實際的值爲origin,_pointsOrig[i]和結果。

+0

非常感謝您!這似乎解決了這個問題!它現在以'y'度旋轉的方式旋轉'x'邊數! –

0

您必須在旋轉前移動_pointsOrig以使它們居中約座標原點

然後旋轉,然後通過你的產地轉移

point.x = point.x - left + width/2 
point.y = point.y - top + height/2 
this._points[i] = new Vector2(
    (cos * point.x + sin * point.y) + origin.x, 
    (cos * point.y - sin * point.x) + origin.y