精灵批处理旋转不正确



我试图让旋转的Texture2D正确适应/填充旋转Polygon(我自己的类)的边界,但它拒绝正常工作。我使用SpriteBatch方法是:

spriteBatch.Draw(texture, new Rectangle((int)Position.X, (int)Position.Y, Width, Height), null, color, Rotation, Vector2.Zero, SpriteEffects.None, 1.0f);

但是,其中唯一重要的位是Rectangle和原点,目前设置为Vector2.Zero.当运行上述内容时,它会生成此图像,其中Texture2D(填充的红色正方形)与Polygon(石灰线框)偏移值(texture.Width / 2, texture.Height / 2)。但是,旋转是正确的,因为两种形状都有平行的边。

我试过:

spriteBatch.Draw(texture, new Rectangle((int)Position.X, (int)Position.Y, Width, Height), null, color, Rotation, new Vector2(Width / 2, Height / 2), SpriteEffects.None, 1.0f);

此调用的唯一区别是我将原点(Texture2D应围绕的点)更改为new Vector2(Width / 2, Height / 2),这导致此图像,其中Texture2DPolygon偏移值为(-Width, -Height),但它仍然随Polygon旋转。

发生的另一个错误是,当使用与第一个宽度和高度不同的不同Texture2D时 - 尽管它应该产生相同的结果,因为destinationRectangle字段不会更改 - 它在程序中是不同的,如下图所示。同样,这使用与前一个完全相同的调用,只是具有不同的图像(具有不同的维度)。

对这两个问题中的任何一个的任何帮助将不胜感激。谢谢!

http://www.monogame.net/documentation/?page=M_Microsoft_Xna_Framework_Graphics_SpriteBatch_Draw

为了正确旋转,您需要确保origin是正确的,

如果是规范化值,要么0.5f, 0.5f,要么width / 2.0f, height / 2.0f.

或任何其他合适的角落,可以在您的机箱中旋转。

原点根据源矩形调整旋转的中心。(当像您的情况一样null传递时,源矩形是整个纹理。

请记住,在平移、旋转和缩放方面,顺序很重要。

旋转应用于源矩形的平移原点,允许旋转精灵表的各个帧。

以下代码应生成预期的输出:

spriteBatch.Draw(texture, new Rectangle((int)Position.Center.X, (int)Position.Center.Y, Width, Height), null, color, Rotation, new Vector2(texture.Width / 2, texture.Height / 2), SpriteEffects.None, 1.0f);

我的两个问题的答案都在于一个错误:

精灵批处理在应用比例平移之前应用围绕原点的旋转。

为了解释这一点,这里有一个例子:

您有一个大小为(16, 16)Texture2D,并希望它在围绕原点旋转(destinationRectangle.Width / 2, destinationRectangle.Height / 2)(等于(24, 24))时填充(48, 48)大小destinationRectangle。因此,您希望最终得到一个围绕其中心点旋转的正方形。

首先,SpriteBatch将围绕点(24, 24)旋转Texture2D,由于Texture2D尚未缩放,因此大小为(16, 16),将导致不正确和意外的结果。在此之后,它将被缩放,使其成为旋转不良的正方形的更大版本。

若要解决此问题,请使用(texture.Width / 2, texture.Height / 2)而不是(destinationRectangle.Width / 2, destinationRectangle.Height / 2)作为原点。

示例:spriteBatch.Draw(texture, new Rectangle((int)Position.X, (int)Position.Y, Width, Height), null, color, Rotation, new Vector2(texture.Width / 2, texture.Height / 2), SpriteEffects.None, 0f);

进一步的解释可以在这里和这里找到。

最新更新