gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:44:04 25 4
gpt4 key购买 nike

四处移动相机时,我无法在控件上获得正确的鼠标位置。 Controller 的宽度为 800 像素,高度为 600 像素。

让我们只看绘图方法:在这里,我唯一想做的事情就是从屏幕中心到鼠标位置画一条线。问题是当相机移动时,结果与相机在位置 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 Matrix GetTransformationMatrix()
{
return Matrix.CreateScale(new Vector3(1, 1, 0)) *
Matrix.CreateTranslation(new Vector3(-View.X, -View.Y, 0));
}

为了移动我应用的相机:

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

结果:

Camera hasn't moved
Camera has moved to the right

我尝试过反转矩阵以及使用 PointToClientPointToScreen 但没有成功。

最佳答案

一段时间后,我终于开始根据这篇文章开始工作 ( 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;
}

然后...(绘制方法)

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

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

关于C#-Monogame.Forms : Question about getting mouse coordinates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55316811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com