2011-07-11 89 views
0

我正在为一台简单的基于XNA的PC游戏工作,其中我有一个1024×768的游戏屏幕窗口大小,我希望按照我移动鼠标的方向滚动一个1280 X 1280的线性包裹旋转纹理。XNA:滚动线性包裹,旋转纹理,向上,向下,向左和向右:

这个纹理的起源已经被设置为中心点(640,640),然后我将这个纹理放置在我的游戏窗口的中间位置(512,384)。

我到目前为止的代码(粘贴在下面的完整部分)完美地工作,直到我将旋转引入混合。

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

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

    } 
} 

例如,如果我是纹理向右旋转,然后从屏幕的中心直线上升移动鼠标使纹理向上斜向滚动到正确的 - 这不是我的意图 - 如果我移动我的鼠标在任何方向上,我都希望我的纹理在这个方向上滚动,就像我从未将旋转引入混音一样。

我该如何改变我的Update()方法来实现这个?

感谢您的阅读.....

回答

0

从你的问题,据我了解,你想要的鼠标控制摄像头,旋转只影响质感。 对此,所需的更改会影响Draw方法。当调用Spritebatch.Begin时,应该使用x和y值传递一个变换矩阵作为参数。这使您可以在世界坐标中旋转纹理,同时独立于旋转移动相机。

检查此问题 - Spritebatch.Begin() Transform Matrix - 了解变换矩阵的工作原理。

+0

感谢您的回复,Elideb。我所有的'相机'组成的是一个矩形,它显示了我的1280 X 1280线性包裹纹理的一部分。我可能是错的,但我不相信我需要一个转换矩阵来做我想做的事情,因为我发布的代码工作得很好,直到我在其原点周围引入了纹理旋转。我需要以某种方式调整x和y值以补偿旋转运动。例如 - 如果我将鼠标水平移动到左侧,我希望纹理在绕其原点旋转的同时向左滚动。 –

0

这似乎工作。可能有一个更优雅的解决方案,但它确实是一个需要解决的问题。

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

      float angle = 0; 
      angle = (float)Math.Atan2(Direction.Y, Direction.X) - rotation; 
      Direction.X = (float)Math.Cos(angle); 
      Direction.Y = (float)Math.Sin(angle); 
      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 += .01f ; 
     if (rotation > MathHelper.Pi * 2) 
     { 
      rotation -= (MathHelper.Pi * 2); 
     } 
     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); 

    } 
} 
相关问题