【发布时间】:2012-08-28 19:22:05
【问题描述】:
我试图让代表精灵的 4 个角的 4 个 Vector2 对象围绕精灵本身围绕其中心旋转。然而,使用下面的代码,Vector2 对象在客户端空间中围绕 0,0 旋转,而不是围绕对象的中心旋转。使用矩阵变换,有什么方法可以让 Vector2 对象围绕对象的中心而不是全局坐标 (0,0) 旋转?
这是目前为止的旋转功能:
public Vector2[] CheckCollision()
{
//Get the 4 corners of the sprite locally
//We can get all 4 corners from only 2 vectors
Vector2 topLeft = new Vector2(position.X - spriteSize.X, position.Y - spriteSize.Y);
//Not sure why position is representing the
//bottom right instead of the center here....
Vector2 bottomRight = position;
Vector2 bottomLeft = new Vector2(topLeft.X, bottomRight.Y);
Vector2 topRight = new Vector2(bottomRight.X, topLeft.Y);
//Create transformation matrix
Matrix transform = Matrix.CreateRotationZ(MathHelper.ToRadians(this.direction)) *
Matrix.CreateScale(this.scale);
//Transform the vectors
topLeft = Vector2.Transform(topLeft, transform);
bottomRight = Vector2.Transform(bottomRight, transform);
bottomLeft = Vector2.Transform(bottomLeft, transform);
topRight = Vector2.Transform(topRight, transform);
Vector2[] vectorArray = new Vector2[4];
vectorArray[0] = topLeft;
vectorArray[1] = bottomRight;
vectorArray[2] = bottomLeft;
vectorArray[3] = topRight;
return vectorArray;
}
【问题讨论】:
-
精灵的位置对应于它的右下角这一事实让我相信你在调用
SpriteBatch.Draw()时指定了错误的原点。 -
@ColeCampbell 我在
SpriteBatch.Draw()调用中将精灵的中心指定为原点。这允许 SpriteBatch 围绕对象的中心旋转精灵,就像我尝试对 Vector2 对象做的那样。我也需要解决位置问题,但它没有我的矩阵问题那么严重,我觉得我可以在以后解决它。