# Grid Based Block Placement

This topic is 794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm working on this 2D game that uses a .png file to generate the level.

I basically scan through the pixels (512x512) of the .png file, and if it corresponds to a specific set of RGB colors, it renders the object to the screen.

i.e - if(red == 255 && green == 0 && blue == 0) {

}

That's not an actual block of code, it's just an example.

I use that method to render my blocks to the game - basically the ground the player walks on.

The way I actually render them beside eachother is with a for loop which increments the x and y variables by 32 (the size of the blocks) then renders them in the appropriate area corresponding to the .png file.

In the game, the player can place down blocks by pressing 'K'. The issue is, the blocks can intersect with eachother and they can be placed anywhere. Think about it like this: You can't place blocks on a specific area on the ground in Minecraft, there's a grid box where the block goes. I want to have that in my game, but I can't seem to figure out how.

Okay, so while typing this out I decided to try something which ended up working.

Basically, when the player presses 'K', I have two integers which both equal the x and y coordinates of the player, they're called 'temp' for x, and 'temp2' for y.

I have this block of code:

while(temp % 32 != 0) {

temp++;

}

while(temp2 & 32 != 0) {

temp2++;

}

And then I add a new block with temp and temp2 as the coordinates rather than the player coordinates.

I'm kind of in a rush right now, so it may be a little hard to understand what I'm trying to explain, or even what I was originally trying to ask.

I hope this helped somebody.

Edited by Debz

##### Share on other sites

I have this block of code:

while(temp % 32 != 0) {

temp++;

}

while(temp2 & 32 != 0) {

temp2++;

}

There is a more efficient way to calculate what you are after, you basically want the closest larger multiple of the block size to the current number:

blockPositionX = BLOCK_SIZE * ((playerPositionX / BLOCK_SIZE) + 1);
blockPositionY = BLOCK_SIZE * ((playerPositionY / BLOCK_SIZE) + 1);


If you remove the "+1", you would get the closest smaller multiple.

Note that I used sensible variable names and constants to make the code more readable and maintainable, this is a very useful habit to get into, even if you are doing a one-man project.

##### Share on other sites
There is a more efficient way to calculate what you are after, you basically want the closest larger multiple of the block size to the current number:

The code you posted does not give the same results in all cases, e.g. playerPositionX = 0.

In the original case, blockPositionX would be 0, in your case it's BLOCK_SIZE. You always round up, even if it's already correct.

The following works, though:

blockPositionX = BLOCK_SIZE * ((playerPositionX + (BLOCK_SIZE - 1)) / BLOCK_SIZE);
blockPositionY = BLOCK_SIZE * ((playerPositionY + (BLOCK_SIZE - 1)) / BLOCK_SIZE);
Edited by Lactose!

##### Share on other sites

You're right of course, thanks for the correction.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633654
• Total Posts
3013168
×