Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
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:

C#
/* Removes a block from the view. */
public void RemoveBlockFromScreen(int ClickWindowX, int ClickWindowY)
{
    /* Set the block position. */
    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:


C#
/* Get a block from the loaded chunks. */
public Block GetBlock(int X, int Y)
{
    /* Calculate the positions */
    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);

    /* Return if the chunk is loaded. */
    foreach (Chunk Chunk in LoadedChunks)
    {
        if (Chunk.ChunkX == ChunkX && Chunk.ChunkY == ChunkY)
        {
           return Chunk.GetBlock(BlockX, BlockY);

        }
    }

    /* Return null. */
    return null;
}
Posted
Updated 30-Apr-13 22:53pm
v2
Comments
Yvar Birx 30-Apr-13 18:07pm    
Edit: The error I am having is that whenever I click on a location or a chunk that has a minus cooardinate, it simply won't place the block correctly, however when these values are not in the minus, it does everything I want it to.
TnTinMn 30-Apr-13 19:03pm    
Just a guess here, but maybe it is due to the Floor function. Perhaps use Ceiling if negative?

Floor(2.7) = 2 whereas Floor(-2.7) = -3
Yvar Birx 30-Apr-13 19:07pm    
Well, I thought that as-well. But, the chunks have a cooardinate that be negative, however the blocks should be more than 0 and less then the size of the chunk.
Yvar Birx 30-Apr-13 19:26pm    
Interesting answer though. I tried it, and I feel like it's something like that causing the bug. Because whenever I select it on the left side(- side) it puts it on the right side but in the exact opposite way.
Yvar Birx 1-May-13 4:52am    
Anyone else? :L

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900