问题描述
我只是在编写我的游戏,我一直在获取单元格位置。 我需要获取光标所在的单元格位置,并且所有内容都是相对于相机矩阵绘制的。
我尝试了很多事情 - 用 Vector2 位置、矩阵本身做了很多事情,但我一无所获。
感谢任何帮助。
尝试获取网格单元格位置的代码:
// Getting view matrix
var viewMatrix = cam.GetTransform();
Vector2 worldPosition = Vector2.Transform(mouse.Position.ToVector2(),Matrix.Invert(viewMatrix));
// Getting the grid cell position where cursor is
int width = 80;
int height = 80;
int size = 64;
for (int forX = 0; forX < width; forX++)
{
for (int forY = 0; forY < height; forY++)
{
int mouseXmin = (size * forX) + 1;
int mouseXmax = mouseXmin + 63;
int mouseYmin = (size * forY) + 1;
int mouseYmax = mouseYmin + 63;
Vector2 minVector = Vector2.Transform(new Vector2(mouseXmin,mouseYmin),Matrix.Invert(viewMatrix));
Vector2 maxVector = Vector2.Transform(new Vector2(mouseXmax,mouseYmax),Matrix.Invert(viewMatrix));
if (worldPosition.X > minVector.X && worldPosition.X < maxVector.X
&& worldPosition.Y > minVector.Y && worldPosition.Y < maxVector.Y)
{
// Some debug stuff,this is where I got that it does not work beacuse of matrix magic
spriteBatch.Begin(SpriteSortMode.Deferred,BlendState.NonPremultiplied,null,viewMatrix);
debugRectangle = new Rectangle((int)minVector.X,(int)minVector.Y,(int)maxVector.X,(int)maxVector.Y);
spriteBatch.End();
}
}
}
绘图代码:
// Getting view matrix
var viewMatrix = cam.GetTransform();
spriteBatch.Begin(SpriteSortMode.Deferred,viewMatrix);
// Drawing grid
for (float x = 0; x < 80; x++)
{
int length = 64 * 79;
Rectangle rectangle = new Rectangle((int)(64 * x),1,length);
spriteBatch.Draw(px,rectangle,Color.White);
}
for (float y = 0; y < 80; y++)
{
int length = 64 * 79;
Rectangle rectangle = new Rectangle(0,(int)(64 * y),length,1);
spriteBatch.Draw(px,Color.White);
}
// Drawing blocks
for (int forX = 0; forX < 80; forX++)
{
for (int forY = 0; forY < 80; forY++)
{
Block block = blocks[forX,forY];
if (block == null) continue;
int x = (64 * forX) + (1 * (forX + 1));
int y = (64 * forY) + (1 * (forX + 1));
switch (block.type)
{
case Block.BlockType.AND:
spriteBatch.Draw(and,new Vector2(x,y),Color.White);
break;
case Block.BlockType.NAND:
spriteBatch.Draw(nand,Color.White);
break;
case Block.BlockType.NOR:
spriteBatch.Draw(nor,Color.White);
break;
case Block.BlockType.NOT:
spriteBatch.Draw(not,Color.White);
break;
case Block.BlockType.NXOR:
spriteBatch.Draw(nxor,Color.White);
break;
case Block.BlockType.OR:
spriteBatch.Draw(or,Color.White);
break;
case Block.BlockType.WIRE:
DrawRectangle(new Rectangle(x + 15,y + 15,35,35),Color.Black);
if (block.active) DrawFilledRectangle(new Rectangle(x + 17,y + 17,31,31),Color.Green);
else DrawFilledRectangle(new Rectangle(x + 17,33,33),Color.Red);
break;
case Block.BlockType.XOR:
spriteBatch.Draw(xor,Color.White);
break;
}
}
}
spriteBatch.End();
相机:
public class Camera
{
public Camera()
{
Zoom = 1;
Position = Vector2.Zero;
Rotation = 0;
Origin = Vector2.Zero;
Position = Vector2.Zero;
}
public float Zoom { get; set; }
public Vector2 Position { get; set; }
public float Rotation { get; set; }
public Vector2 Origin { get; set; }
public void Move(Vector2 direction)
{
Position += direction;
}
public Matrix GetTransform()
{
var translationMatrix = Matrix.CreateTranslation(new Vector3(Position.X,Position.Y,0));
var rotationMatrix = Matrix.CreateRotationZ(Rotation);
var scaleMatrix = Matrix.CreateScale(new Vector3(Zoom,Zoom,1));
var originMatrix = Matrix.CreateTranslation(new Vector3(Origin.X,Origin.Y,0));
return translationMatrix * rotationMatrix * scaleMatrix * originMatrix;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)