C# xna精灵到鼠标位置

C# xna精灵到鼠标位置,c#,class,matrix,C#,Class,Matrix,在XNA4.0中,如何使精灵移动到鼠标坐标。我知道这样做是可能的: if (ms.LeftButton == ButtonState.Pressed) { Vector2 shipPos = new Vector2(ms.X,ms.Y); } if (ms.LeftButton == ButtonState.Pressed) { Vector2 shipPos = new Vector2( ms.X-screenWidth/2 +

在XNA4.0中,如何使精灵移动到鼠标坐标。我知道这样做是可能的:

if (ms.LeftButton == ButtonState.Pressed) 
{
    Vector2 shipPos = new Vector2(ms.X,ms.Y);
}
if (ms.LeftButton == ButtonState.Pressed) 
{
    Vector2 shipPos = new Vector2(
                      ms.X-screenWidth/2 + previousShipPosition.X,
                      ms.Y-screenHeight/2 + previousShipPosition.Y
                      );
}
但是因为我用的是跟踪飞船的摄像机,所以不能正常工作。原因是鼠标的位置是相对于屏幕的,如果当我点击窗口左上角时,飞船被移动到500500,那么飞船将返回到0,0,甚至很难,我希望飞船从飞船位置向上移动到角落。以下是我的矩阵的代码:

class Camera
{
    public Matrix transform;
    Viewport view;
    Vector2 centre;

    public Camera(Viewport newView)
    {
        view = newView;
    }
    public void Update(GameTime gameTime, Game1 ship)
    {
        int w = Game1.width;
        int h = Game1.height;
        centre = new Vector2(ship.FSpos.X - (w / 2 - 189/2), 
                             ship.FSpos.Y - (h / 2-128/2));
        transform = Matrix.CreateScale(new Vector3(1, 1, 0)) *
                    Matrix.CreateTranslation(new Vector3(-centre.X, -centre.Y, 0));
    }
}

你可以这样做:

if (ms.LeftButton == ButtonState.Pressed) 
{
    Vector2 shipPos = new Vector2(ms.X,ms.Y);
}
if (ms.LeftButton == ButtonState.Pressed) 
{
    Vector2 shipPos = new Vector2(
                      ms.X-screenWidth/2 + previousShipPosition.X,
                      ms.Y-screenHeight/2 + previousShipPosition.Y
                      );
}

糟糕的是,语言使用不当:D。我的意思是船应该从示例500500中的中心移动到相对于地图的位置,地图位于左上角。这些坐标是胡乱猜测的,但相对于世界的坐标可能是100740hmm。。。有人删除了他的评论:根据相机的定义,你不能只添加相机的偏移量吗?e、 g.shipPos=新矢量2Ms.X,ms.Y+camera.TopLeftCorner;