我正在爲一臺簡單的基於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()方法來實現這個?
感謝您的閱讀.....
感謝您的回覆,Elideb。我所有的'相機'組成的是一個矩形,它顯示了我的1280 X 1280線性包裹紋理的一部分。我可能是錯的,但我不相信我需要一個轉換矩陣來做我想做的事情,因爲我發佈的代碼工作得很好,直到我在其原點周圍引入了紋理旋轉。我需要以某種方式調整x和y值以補償旋轉運動。例如 - 如果我將鼠標水平移動到左側,我希望紋理在繞其原點旋轉的同時向左滾動。 –