2016-10-07 170 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