简体   繁体   中英

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.

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.

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() :

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.

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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