C#-单博弈。形式:关于获取鼠标坐标的问题

C#-单博弈。形式:关于获取鼠标坐标的问题,c#,forms,winforms,monogame,mouse-position,C#,Forms,Winforms,Monogame,Mouse Position,在移动相机时,我无法在控件上获得正确的鼠标位置。控制器的宽度为800px,高度为600px 让我们看看画法:在这里,我唯一想做的就是 就是从屏幕中心到鼠标位置画一条线。问题是,当摄影机移动时,结果与摄影机处于位置x:0、y:0时的结果不同 protected override void Draw() { GraphicsDevice.Clear(new Color(50, 50, 50)); SpriteBatch.Begin(SpriteSortMode.Deferred,Bl

在移动相机时,我无法在控件上获得正确的鼠标位置。控制器的宽度为800px,高度为600px

让我们看看画法:在这里,我唯一想做的就是 就是从屏幕中心到鼠标位置画一条线。问题是,当摄影机移动时,结果与摄影机处于位置x:0、y:0时的结果不同

protected override void Draw()
{
    GraphicsDevice.Clear(new Color(50, 50, 50));
    SpriteBatch.Begin(SpriteSortMode.Deferred,BlendState.AlphaBlend, null, null, null, null,
        Camera.GetTransformationMatrix());

    Map.Draw(SpriteBatch);
    //SelectionTool.Draw(SpriteBatch);

    if (isPanning)
    {
        var point = PointToClient(MousePosition);
        Vector2 mousePosition = new Vector2(point.X, point.Y);
        Console.WriteLine(mousePosition);

        DrawLine(SpriteBatch, Camera.CenterScreen, mousePosition, Color.White);
    }

    SpriteBatch.End();
}
因此,我使用
Camera.GetTransformationMatrix()
绘制控件:

对于移动相机,我应用:

public void Move(Vector2 distance)
{
    View.X += (int)(distance.X * panSpeed);
    View.Y += (int)(distance.Y * panSpeed);
}
划线法:

public void DrawLine(SpriteBatch spriteBatch, Vector2 from, Vector2 to, Color color, int width = 1)
{
    Rectangle rect = new Rectangle((int)from.X, (int)from.Y, (int)(to - from).Length() + width, width);
    Vector2 vector = Vector2.Normalize(from - to);
    float angle = (float)Math.Acos(Vector2.Dot(vector, -Vector2.UnitX));
    Vector2 origin = Vector2.Zero;

    if (from.Y > to.Y)
         angle = MathHelper.TwoPi - angle;

    SpriteBatch.Draw(lineTexture, rect, null, color, angle, origin, SpriteEffects.None, 0);
}
结果:



我曾尝试使用
PointToClient
PointToScreen
反转矩阵,但没有成功。

一段时间后,我终于根据这篇文章开始工作() 我所要做的就是将相机位置添加到鼠标位置:(camera.cs)

然后…(画法)

public void DrawLine(SpriteBatch spriteBatch, Vector2 from, Vector2 to, Color color, int width = 1)
{
    Rectangle rect = new Rectangle((int)from.X, (int)from.Y, (int)(to - from).Length() + width, width);
    Vector2 vector = Vector2.Normalize(from - to);
    float angle = (float)Math.Acos(Vector2.Dot(vector, -Vector2.UnitX));
    Vector2 origin = Vector2.Zero;

    if (from.Y > to.Y)
         angle = MathHelper.TwoPi - angle;

    SpriteBatch.Draw(lineTexture, rect, null, color, angle, origin, SpriteEffects.None, 0);
}
public Vector2 GetMouse(Vector2 mouse) 
{
    Vector2 outVect = new Vector2(Position.X + mouse.X, Position.Y + mouse.Y);

    return outVect;
}
    if (isPanning)
    {
        Vector2 mousePosition = Camera.GetMouse(currMousePos);
        Console.WriteLine(mousePosition);                

        DrawLine(SpriteBatch, Camera.CenterScreen, mousePosition, Color.White);
    }