title: XNA: Scrolling A Linearly Wrapped, Rotating Texture, Up, Down, Left, And Right:
body: <p>I'm working on a simple XNA based game for the PC where I have a game screen window size of 1024 X 768 and I want to scroll, in the direction in which I move my mouse, a linearly wrapped, rotating texture, which is 1280 X 1280 in size. </p>
  
  <p>The origin of this texture has been set to it's center point(640, 640) and I then place this texture onscreen in the middle of my game window at (512, 384). </p>
  
  <p>The code I have so far (pasted in it's entirety below) works perfectly until I introduce rotation to the mix.</p>
  
  <pre><code>public class Game1 : Microsoft.Xna.Framework.Game
  {
  
      GraphicsDeviceManager graphics;
  
      SpriteBatch spriteBatch;
  
      int x;
      int y;
  
      float z = 250f;
  
      Texture2D Overlay;
  
      Texture2D RotatingBackground;
  
      Rectangle? sourceRectangle;
  
      Color color;
  
      float rotation;
  
      Vector2 ScreenCenter;
  
      Vector2 Origin;
  
      Vector2 scale;
  
      Vector2 Direction;
  
      SpriteEffects effects;
  
      float layerDepth;
  
      public Game1()
      {
  
          graphics = new GraphicsDeviceManager(this);
  
          Content.RootDirectory = "Content";
  
      }
  
      protected override void Initialize()
      {
  
          graphics.PreferredBackBufferWidth = 1024;
  
          graphics.PreferredBackBufferHeight = 768;
  
          graphics.ApplyChanges();
  
          Direction = Vector2.Zero;    
  
          IsMouseVisible = true;
  
          ScreenCenter = new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2);
  
          Mouse.SetPosition((int)graphics.PreferredBackBufferWidth / 2, (int)graphics.PreferredBackBufferHeight / 2);
  
          sourceRectangle = null;
  
          color = Color.White;
  
          rotation = 0.0f;
  
          scale = new Vector2(1.0f, 1.0f);
  
          effects = SpriteEffects.None;
  
          layerDepth = 1.0f;
  
          base.Initialize();
  
      }
  
      protected override void LoadContent()
      {
  
          spriteBatch = new SpriteBatch(GraphicsDevice);
  
          Overlay = Content.Load&lt;Texture2D&gt;("Overlay");
  
          RotatingBackground = Content.Load&lt;Texture2D&gt;("Background");
  
          Origin = new Vector2((int)RotatingBackground.Width / 2, (int)RotatingBackground.Height / 2);
  
      }
  
  
      protected override void UnloadContent()
      {
  
      }
  
      protected override void Update(GameTime gameTime)
      {
  
          float timePassed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  
          MouseState ms = Mouse.GetState();
  
          Vector2 MousePosition = new Vector2(ms.X, ms.Y);
  
          Direction = ScreenCenter - MousePosition;
  
          if (Direction != Vector2.Zero)
          {
  
              Direction.Normalize();
  
          }
  
          x += (int)(Direction.X * z * timePassed);
  
          y += (int)(Direction.Y * z * timePassed);
  
  
          //No rotation = texture scrolls as intended, With rotation = texture no longer scrolls in the direction of the mouse. My update method needs to somehow compensate for this.         
          //rotation += 0.01f;  
  
          base.Update(gameTime);
  
      }
  
      protected override void Draw(GameTime gameTime)
      {
  
          spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  
          spriteBatch.Draw(RotatingBackground, ScreenCenter, new Rectangle(x, y, RotatingBackground.Width, RotatingBackground.Height), color, rotation, Origin, scale, effects, layerDepth);
  
          spriteBatch.Draw(Overlay, Vector2.Zero, Color.White);
  
          spriteBatch.End();
  
          base.Draw(gameTime);
  
      }
  }  
  </code></pre>
  
  <p>For example if I were to rotate the texture to the right then moving the mouse straight up from the screen's center causes the texture to scroll diagonally upwards and to the right - this is not my intent - If I move my mouse in any direction I want my texture to scroll in that direction just the same as if I had never introduced rotation to the mix.       </p>
  
  <p>How do I go about altering my Update() method to achieve this?</p>
  
  <p>Thanks for reading.....            </p>