2010-08-02 58 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(); 
    }