How to calculate position in matrix based on screen position
So I am doing a pacman remake and I am using XNA and .NET to create it.
I set the screen size to 448x576 and the matrix to 28x36, where each position in the matrix is ββ16px by 16px
448/16 = 28 576/16 = 36
This is how I calculate the screen position based on the position in the matrix:
public Vector2 GetPositionAt(int col, int row)
{
int X = ((col) * 16) + 8;
int Y = ((row) * 16) + 8;
return new Vector2(X, Y);
}
reverse code:
float col = ((position.X - 8) / 16);
float row = ((position.Y - 8) / 16);
return new MatrixPosition((int)col, (int)row);
but that doesn't work because the position is only updated when pacman's position is in the center of the position in the matrix. I want to do this when it is close to a new point.
Let me explain with pictures (Sorry, can't post pictures): http://imgur.com/a/CYekd
source to share
float col = ((position.X - 8) / 16);
float row = ((position.Y - 8) / 16);
If you replace position.X = 8 and position.Y = 18:
col = ((8 - 8) / 16) = 0 / 16 = 0 row = ((18 - 8) / 16) = 10 / 16 = 0
Integers don't work the way you want them to. I understand why you are using -8 to find the center of the square, but you cannot just split this method correctly.
col = position.X / 16
row = position.Y / 16
But you still have to consider that you are in this cell. Don't reread it.
source to share