XNA 4.0 2D摄像头,光标处于活动状态

时间:2014-03-28 18:44:46

标签: c# xna camera cursor 2d

我是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));
        }

    }


}
}

还有Cursor Class

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

我仍然无法完全理解World和View Matrices及其数学的概念。

1 个答案:

答案 0 :(得分:0)

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