XNA 4.0 2D相机与光标活动

本文关键字:光标 活动 相机 2D XNA | 更新日期: 2023-09-27 17:54:49

我对XNA框架很陌生,现在我可以做绘画,动画等,但是我遇到了一个问题,相机显示光标

我想实现一个2D游戏,你使用光标作为主要控制器(选择,在地图上移动等)。我想通过鼠标右键和拖动来控制相机。

我已经创建了非常简单的逻辑移动它,但它不工作。每当我按下RButton并拖动它时,相机都工作得很好。

再次执行时,问题弹出。整个视图重置到初始位置。我猜问题出在鼠标相对于世界的位置。

我正在添加相机类

class TCCamera : IDrawable
{
    public Matrix transformation;
    Viewport viewport;
    Vector2 centre;
    public TCCamera(Viewport vport)
    {
        viewport = vport;
        this.centre = new Vector2();
    }
    public void LoadContent(Microsoft.Xna.Framework.Content.ContentManager Content)
    {
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        throw new NotImplementedException();
    }
    public Matrix Translate
    {
        get
        {
            return this.transformation;
        }
    }
    public void Update(GameTime gameTime)
    {
        MouseState mstat = Mouse.GetState();

        if (mstat.RightButton == ButtonState.Pressed)
        {
            this.centre.X = mstat.X;
            this.centre.Y = mstat.Y;
            transformation =  Matrix.CreateTranslation(new Vector3(-centre.X, -centre.Y, 0));
        }
    }

}
}

还有光标类

class TCCursor : IDrawable
{
    Texture2D tex;
    public Vector2 pos;
    Rectangle bBox;
    public TCCursor()
    {
        this.pos = new Vector2();
        this.bBox = new Rectangle(0, 0, 50, 50);
    }
    public void LoadContent(ContentManager Content)
    {
        tex = Content.Load<Texture2D>("cursor");
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(tex, bBox, Color.White);
    }
    public void Update(GameTime gameTime)
    {
        MouseState value = Mouse.GetState();
        this.pos.X = value.X;
        this.pos.Y = value.Y;
        this.bBox.X = value.X;
        this.bBox.Y = value.Y;
    }
}

我还画了

spriteBatch.Begin(SpriteSortMode.Immediate, 
            BlendState.AlphaBlend,
            null, null, null, null, camera.Translate);

我仍然不能完全理解世界和视图矩阵的概念和他们的数学。

XNA 4.0 2D相机与光标活动

过了一会儿,我找到了使用逆矩阵的解决方案。我发现这个非常有用:教程XNA相机