Hello Codeproject,
I am currently working on a game of mine. It's a Minecraft type clone, I was just adding in the placing block function when I stumbled upon an odd error. It's not exactly an error, but it just acts up odd when I select a negative cooardinate block. Here is some of my code:
public void RemoveBlockFromScreen(int ClickWindowX, int ClickWindowY)
{
int X = Player.PositionBlockX;
int Y = Player.PositionBlockY;
X -= EntityWorld.ViewInBlocksWidth / 2;
Y -= EntityWorld.ViewInBlocksHeight / 2;
X += ClickWindowX / EntityWorld.BlockSize;
Y += ClickWindowY / EntityWorld.BlockSize;
Block b = EntityWorld.GetBlock(X, Y);
b.Material = BlockMaterial.Material_Sand;
}
If you would like to know some of the variabless:
The player position is 0, 0.
The Blocks in Width is 20.
The blocks in Height is 20.
The position where we clicked can be anywhere between 1024 and 768.
Now, the error could be in this piece of code, although I am not sure about that:
public Block GetBlock(int X, int Y)
{
int ChunkX = (int)Math.Floor((double)(X / ChunkWidth));
int ChunkY = (int)Math.Floor((double)(Y / ChunkHeight));
int BlockX = Math.Abs(X % ChunkWidth);
int BlockY = Math.Abs(Y % ChunkHeight);
foreach (Chunk Chunk in LoadedChunks)
{
if (Chunk.ChunkX == ChunkX && Chunk.ChunkY == ChunkY)
{
return Chunk.GetBlock(BlockX, BlockY);
}
}
return null;
}