C# directx 精灵起源

发布于 2024-09-12 16:35:26 字数 492 浏览 3 评论 0原文

当我的 Sprite 旋转原点固定在窗口的左上角时,我遇到这个问题(与 sprite.Drawsprite.Draw2D 相同) 不管怎样,如果我改变旋转中心,它仍然在左上角。我需要精灵绕其 Z 轴旋转。

编辑: 我已经尝试过这个:

    hereMatrix pm = Matrix.Translation(_playerPos.X + 8, _playerPos.Y + 8, 0);
    sprite.Transform = Matrix.RotationZ(_angle) * pm;
    sprite.Draw(playerTexture, textureSize, new Vector3(8, 8, 0), new Vector3(_playerPos.X, _playerPos.Y, 0), Color.White);

但它似乎效果不佳......

i have this problem when my Sprite rotation origin is fixed at top left corner of window (same with sprite.Draw and sprite.Draw2D)
Either way if i change rotation center it's still at top left. I need sprite to rotate around its Z axis.

Edit:
I have tried this:

    hereMatrix pm = Matrix.Translation(_playerPos.X + 8, _playerPos.Y + 8, 0);
    sprite.Transform = Matrix.RotationZ(_angle) * pm;
    sprite.Draw(playerTexture, textureSize, new Vector3(8, 8, 0), new Vector3(_playerPos.X, _playerPos.Y, 0), Color.White);

But it does not seem to works well...

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

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

发布评论

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

评论(2

寂寞美少年 2024-09-19 16:35:26

当你画它的时候,它在正确的位置吗?

我相信乘法顺序是相反的,并且您不应该根据变换中玩家的位置进行变换。

// shift centre to (0,0)
sprite.Transform = Matrix.Translation(-textureSize.Width / 2, -textureSize.Height / 2, 0);

// rotate about (0,0)
sprite.Transform *= Matrix.RotationZ(_angle); 


sprite.Draw(playerTexture, textureSize, Vector3.Zero,
            new Vector3(_playerPos.X, _playerPos.Y, 0), Color.White);

编辑

您还可以使用Matrix.Transformation方法一步获得矩阵。

When you draw it, is it in the correct place?

I believe that the multiplication order is reversed, and that you shouldn't be transforming by the players position in the transform.

// shift centre to (0,0)
sprite.Transform = Matrix.Translation(-textureSize.Width / 2, -textureSize.Height / 2, 0);

// rotate about (0,0)
sprite.Transform *= Matrix.RotationZ(_angle); 


sprite.Draw(playerTexture, textureSize, Vector3.Zero,
            new Vector3(_playerPos.X, _playerPos.Y, 0), Color.White);

Edit

You could also use the Matrix.Transformation method to get the matrix in one step.

韬韬不绝 2024-09-19 16:35:26

我已经为您找到了解决方案,这是一个简单的方法,您可以在每次想要绘制精灵时使用。
通过这种方法,您将能够以所需的旋转中心旋转精灵。

public void drawSprite(Sprite sprite, Texture texture, Point dimension, Point rotationCenter, float rotationAngle, Point position)
    {
        sprite.Begin(SpriteFlags.AlphaBlend);

        //First draw the sprite in position 0,0 and set your desired rotationCenter (dimension.X and dimension.Y represent the pixel dimension of the texture)
        sprite.Draw(texture, new Rectangle(0, 0, dimension.X, dimension.Y), new Vector3(rotationCenter.X, rotationCenter.Y, 0), new Vector3(0, 0, 0), Color.White);

        //Then rotate the sprite and then translate it in your desired position
        sprite.Transform = Matrix.RotationZ(rotationAngle) * Matrix.Translation(position.X, position.Y, 0);

        sprite.End();   
    }

I've got the solution for you, it's a simple method that you can use everytime you want to draw a sprite.
With this method you will be able to rotate the sprite with your desired rotation center.

public void drawSprite(Sprite sprite, Texture texture, Point dimension, Point rotationCenter, float rotationAngle, Point position)
    {
        sprite.Begin(SpriteFlags.AlphaBlend);

        //First draw the sprite in position 0,0 and set your desired rotationCenter (dimension.X and dimension.Y represent the pixel dimension of the texture)
        sprite.Draw(texture, new Rectangle(0, 0, dimension.X, dimension.Y), new Vector3(rotationCenter.X, rotationCenter.Y, 0), new Vector3(0, 0, 0), Color.White);

        //Then rotate the sprite and then translate it in your desired position
        sprite.Transform = Matrix.RotationZ(rotationAngle) * Matrix.Translation(position.X, position.Y, 0);

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