I am just coding my game and I stuck with getting the cell position.
I need to get cell position where the cursor is, and everything is drawing relative to camera matrix.
I’ve tried many things - doing many stuff with Vector2 positions, matrix itself, and I got nowhere.
Any help is appreciated.
Code that tries to get grid cell position:
// 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, null, null, null, viewMatrix);
debugRectangle = new Rectangle((int)minVector.X, (int)minVector.Y, (int)maxVector.X, (int)maxVector.Y);
spriteBatch.End();
}
}
}
Drawing code:
// Getting view matrix
var viewMatrix = cam.GetTransform();
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied,
null, null, null, null, viewMatrix);
// Drawing grid
for (float x = 0; x < 80; x++)
{
int length = 64 * 79;
Rectangle rectangle = new Rectangle((int)(64 * x), 0, 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, rectangle, 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, new Vector2(x, y), Color.White);
break;
case Block.BlockType.NOR:
spriteBatch.Draw(nor, new Vector2(x, y), Color.White);
break;
case Block.BlockType.NOT:
spriteBatch.Draw(not, new Vector2(x, y), Color.White);
break;
case Block.BlockType.NXOR:
spriteBatch.Draw(nxor, new Vector2(x, y), Color.White);
break;
case Block.BlockType.OR:
spriteBatch.Draw(or, new Vector2(x, y), 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, y + 17, 33, 33), Color.Red);
break;
case Block.BlockType.XOR:
spriteBatch.Draw(xor, new Vector2(x, y), Color.White);
break;
}
}
}
spriteBatch.End();
Camera:
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;
}
}