简体   繁体   English

C#-Monogame.Forms:有关获取鼠标坐标的问题

[英]C# - Monogame.Forms: Question about getting mouse coordinates

I'm having a problem getting the correct mouse position on the control when moving the camera around. 移动相机时,在控件上获得正确的鼠标位置时出现问题。 The controler has 800px width and 600px height. 控制器的宽度为800px,高度为600px。

Lets only look at drawing method: In here, the only thing I'm trying to do is to draw a line from the center of the screen to the mouse position. 让我们只看一下绘制方法:在这里,我唯一要做的就是从屏幕中心到鼠标位置画一条线。 The problem is that when the camera moves, the result is not the same as when the camera is at position x: 0, y: 0. 问题在于,当摄像机移动时,结果与摄像机在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();
}

So, I draw the control by using Camera.GetTransformationMatrix() : 因此,我使用Camera.GetTransformationMatrix()绘制了控件:

public Matrix GetTransformationMatrix()
{
     return Matrix.CreateScale(new Vector3(1, 1, 0)) *
         Matrix.CreateTranslation(new Vector3(-View.X, -View.Y, 0));
}

For moving the camera I apply: 对于移动相机,我适用:

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

Draw line method: 画线方法:

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

Result: 结果:

Camera hasn't moved 相机未移动
Camera has moved to the right 相机已移至右侧

I've tried to invert the matrix as well as using PointToClient or PointToScreen but with no success. 我试图反转矩阵以及使用PointToClientPointToScreen但没有成功。

After some time i finnaly got to work based on this post ( https://gamedev.stackexchange.com/questions/85836/how-do-i-get-the-mouse-coordinates-relative-to-my-whole-scene-with-monogame ) All i had to do was adding the camera position to the mouse position: (Camera.cs) 一段时间后,我最终根据这篇文章开始工作( https://gamedev.stackexchange.com/questions/85836/how-do-i-get-the-mouse-coordinates-relative-to-my-whole-scene -with-monogame )我要做的就是将摄像头位置添加到鼠标位置:(Camera.cs)

public Vector2 GetMouse(Vector2 mouse) 
{
    Vector2 outVect = new Vector2(Position.X + mouse.X, Position.Y + mouse.Y);

    return outVect;
}

and then...(draw method) 然后...(绘制方法)

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM