在移动相机时,在控件上获得正确的鼠标位置时遇到问题。控制器的宽度为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 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
我尝试使用PointToClient
或PointToScreen
求矩阵求逆,但没有成功。
答案 0 :(得分:0)
一段时间后,我终于根据这篇文章开始工作(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);
}