Xna SpriteBatch Matrix.Decompose()

发布于 2024-08-23 18:26:54 字数 260 浏览 5 评论 0原文

我想要做的是能够将变换矩阵推送和弹出到 SpriteBatch 中。我有 2 个精灵,父级和子级,子级应该相对于父级进行缩放、旋转、平移。

我目前有一个使用纹理四边形的实现,但我认为使用内置的 SpriteBatch 类和使用 Matrix.Decompose() 应该可以实现。不过,我不确定如何将分解后的值传递给 SpriteBatch。

我了解如何保留矩阵堆栈,我只是在寻找将来自 Matrix.Decompose() 的值与 SpriteBatch 结合使用的示例。

What I am looking to do is to be able to push and pop transform matrices into SpriteBatch. I have 2 sprites, parent and child, and the child should be scaled, rotated, translated relative to the parent.

I currently have an implementation using textured quads but I think this should be possible using the built in SpriteBatch class and using Matrix.Decompose(). I'm not sure how to pass the decomposed values to SpriteBatch once I've decomposed them though.

I understand how to keep the matrix stack, I'm just looking for an example of using values coming from Matrix.Decompose() in conjunction with SpriteBatch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是一片海 2024-08-30 18:26:54

我自己终于弄清楚了这一点。大部分功劳都属于这个 博客文章

您可以使用此方法来分解您的矩阵:

private void DecomposeMatrix(ref Matrix matrix, out Vector2 position, out float rotation, out Vector2 scale)
{
    Vector3 position3, scale3;
    Quaternion rotationQ;

    matrix.Decompose(out scale3, out rotationQ, out position3);

    Vector2 direction = Vector2.Transform(Vector2.UnitX, rotationQ);
    rotation = (float)Math.Atan2((double)(direction.Y), (double)(direction.X));
    position = new Vector2(position3.X, position3.Y);
    scale = new Vector2(scale3.X, scale3.Y);
}

然后您可以为您的叶精灵构建一个变换矩阵,如下所示:

Matrix transform = 
    Matrix.CreateScale(new Vector3(this.Scale, 1.0f)) *
    Matrix.CreateRotationZ(this.Rotation) *
    Matrix.CreateTranslation(new Vector3(this.Position, 0));

对于您的父精灵,包括原点:

Matrix transform = 
    Matrix.CreateTranslation(new Vector3(-this.Origin, 0)) *
    Matrix.CreateScale(new Vector3(this.Scale, 1.0f)) *
    Matrix.CreateRotationZ(this.Rotation) *
    Matrix.CreateTranslation(new Vector3(this.position, 0));

以相反的顺序乘以堆栈中的所有矩阵。

Figured this one out myself finally. Most of the credit belongs to this blog post though.

You can use this method to decompose your matrix:

private void DecomposeMatrix(ref Matrix matrix, out Vector2 position, out float rotation, out Vector2 scale)
{
    Vector3 position3, scale3;
    Quaternion rotationQ;

    matrix.Decompose(out scale3, out rotationQ, out position3);

    Vector2 direction = Vector2.Transform(Vector2.UnitX, rotationQ);
    rotation = (float)Math.Atan2((double)(direction.Y), (double)(direction.X));
    position = new Vector2(position3.X, position3.Y);
    scale = new Vector2(scale3.X, scale3.Y);
}

You can then build a transform matrix your leaf sprites like so:

Matrix transform = 
    Matrix.CreateScale(new Vector3(this.Scale, 1.0f)) *
    Matrix.CreateRotationZ(this.Rotation) *
    Matrix.CreateTranslation(new Vector3(this.Position, 0));

For your parent sprites, include the origin:

Matrix transform = 
    Matrix.CreateTranslation(new Vector3(-this.Origin, 0)) *
    Matrix.CreateScale(new Vector3(this.Scale, 1.0f)) *
    Matrix.CreateRotationZ(this.Rotation) *
    Matrix.CreateTranslation(new Vector3(this.position, 0));

Multiply by all the matrices in the stack in reverse order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文