单个对象的 Monogame XNA 变换矩阵?

Monogame XNA transform matrix for a single object?

我已经阅读了一些解释 XNA/Monogame 变换矩阵的教程。问题是这些矩阵应用于

SpriteBatch.Begin(...matrix);

这意味着所有的Draw代码都会被转换。 如何将转换矩阵应用于单个可绘制对象?就我而言,我想转换滚动背景,使其自动换行。

SpriteBatch.Draw(.. this has no transform matrix parameter?);

如果您只想对某些绘图调用使用特定的 spritebatch begin 调用,您可以根据需要启动一个新的调用。

例如

SpriteBatch.Begin(...matrix);

//Draw stuff with matrix

SpriteBatch.End();

SpriteBatch.Begin();

//do the rest of the drawing

SpriteBatch.End();

这通常用于在适当的位置、比例和旋转下使用 "camera" 矩阵绘制一堆对象,然后调用另一个 spritebatch.Begin 来绘制平面、静态 UI 在上面等等

我遇到了同样的问题,但使用 SpriteBatch.Begin()SpriteBatch.End() 对我的情况不起作用。

您可以像这样转换单个可绘制对象(此代码假定对象正在绘制到目的地 Rectangle):

static Point Transform(Point point, Matrix matrix)
{
    var vector = point.ToVector2();
    var transformedVector = Vector2.Transform(vector, Matrix.Invert(matrix));
    return transformedVector.ToPoint();
}

// matrix below is the same as the matrix you used in SpriteBatch.Begin(...matrix).

var destinationRectangle = new Rectangle(
    Transform(bounds.Location, matrix),
    Transform(bounds.Size, matrix)
);

// Draw the object to the destination rectangle!