2010-08-02 57 views
2

如果我改变旋转中心,我有,当我的雪碧旋转原点被固定在窗口的左上角这个问题(与相同sprite.Drawsprite.Draw2D) 无论哪种方式,它仍然在左上方。我需要精灵围绕它的Z轴旋转。C#的DirectX精灵起源

编辑: 我已经试过这样:

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); 

但它似乎没有好作品不...

+0

请张贴一些示例代码,以便我们可以看到你在做什么。 – Tchami 2010-08-03 06:58:30

回答

1

当你画它,它是在正确的位置?

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

// 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方法来获得矩阵中的一个步骤。

1

我有你的解决方案,这是一个简单的方法,你可以使用每次你想绘制一个精灵。 使用此方法,您将能够以所需的旋转中心旋转精灵。

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(); 
    }