Using XNA I'm working on my first, relatively simple, videogame for the PC.

At the moment my game window is 1024 X 768 and I have a 'Starfield' linearly wrapped background texture 1280 X 1280 in size whose origin has been set to its center point (width / 2, height / 2).

This texture is drawn onscreen using (graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2) to place the origin in the center of the window.

I want to be able to use the horizontal movement of the mouse to rotate my texture left or right and use the arrow keys to scroll the texture in four directions.

From my own related coding experiments I have found that once I rotate the texture it no longer scrolls in the direction I want, it's as if somehow the XNA framework's 'sense of direction' has been 'rotated' along with the texture.

As an example of what I've described above lets say I rotate the texture 45 degrees to the right, then pressing the up arrow key results in the texture scrolling diagonally from top-right to bottom-left. This is not what I want, regardless of the degree or direction of rotation I want my texture to scroll straight up, straight down, or to the left or right depending on which arrow key was pressed.

How do I go about accomplishing this? Any help or guidance is appreciated.

To finish up there are two points I'd like to clarify:

[1] The reason I'm using linear wrapping on my starfield texture is that it gives a nice impression of an endless starfield.

[2] Using a texture at least 1280 X 1280 in conjunction with a game window of 1024 X 768 means that at no point in it's rotation will the edges of the texture become visible.

Thanks for reading.....

Update # 1 - as requested by RCIX:

The code below is what I was referring to earlier when I mentioned 'related coding experiments'. As you can see I am scrolling a linearly wrapped texture in the direction I've moved the mouse relative to the center of the screen. This works perfectly if I don't rotate the texture, but once I do rotate it the direction of the scrolling gets messed up for some reason.

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;


        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;



    protected override void LoadContent()

        spriteBatch = new SpriteBatch(GraphicsDevice);

        Overlay = Content.Load<Texture2D>("Overlay");

        RotatingBackground = Content.Load<Texture2D>("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)



        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;  



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



  • \$\begingroup\$ Gonna need more info about how you're calculating the position and rotation to draw the texture... \$\endgroup\$
    – RCIX
    Commented Jul 15, 2011 at 23:27
  • \$\begingroup\$ Thanks, RCIX. Updated as you requested with my 'related coding experiment' \$\endgroup\$
    – The Thing
    Commented Jul 16, 2011 at 11:43

1 Answer 1


Yes, the "sense of direction" goes get messed up, but only because of how you are thinking about it.

Take your mouse, rotate it 90* to the left, and then get your cursor to move to the top of the screen. Notice how you need to move your mouse to your left to get the cursor to move up? That is because your mouse doesn't change what "UP" is when you rotate it. You can do the same thing by leaving your mouse alone and rotating yourself around your desk. Move to the side. see how your mouse is rotated, but it isn't, right? you moved :)

I guess an easier way to think about it is two people are standing facing each other. The one says "Move Left" so the other one does. "No, not your left, my left".

OK, so how do you code around it? Well, unless there is some API I haven't heard about all you need to do is calculate the desired movement vector based off the current rotation.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .