LibGDX top grass tiles

if(isAlive){
			if(body.isActive()){
				
				for(Chunk chunk : gameScreen.chunks){ <-- all game chunks
					
					Chunk checkCheck = chunk;
					
					for(Block blocks : checkCheck.getBlocks()){ <-- all chunks blocks
						
						Block checkBlock = blocks;
					
						if(checkBlock != this){ <-- make sure its not the block we are currently checking
							
							if(checkBlock.blockFace_UP.overlaps(boxRectangle)){ if the if the checkbox'es rectangle above it overlaps the checking rectangle then we set it to dirt
								setType(BlockType.DIRT);
							}else{
								setType(BlockType.GRASS_TOP); else we set it to grass top
							}
							
						}
						
						
					}
					
				}
				
			}
		}

it just makes everything grass tiles

remove:


else{
    setType(BlockType.GRASS_TOP); else we set it to grass top
}

i want to be able to set all blocks underneath to dirt and all blocks that is not intersecting with anything from top to be dirt

Is it a side scroller? What kind of game is it. it’s impossible to help without knowing.

so checkBlock.blockFace_UP.overlaps(boxRectangle) is always returning false?

yeah its a side scroller, but why is it false doh here are the rectangles
https://gyazo.com/4d59e4bfe47dae153391c918bbb6afd0

the once at top is suppose to be the grass tiles coos there rects are not intersecting with any block

So the little rectangles are grass? Are your blocks in an array or list?

is blockFace_UP an adjacent block?

in a list

but not even this works :confused:

		boxRectangle = new Rectangle(block_pos.x - Assets.blockSize / 2, block_pos.x - Assets.blockSize / 2, 0.3f, 0.3f);
	
		blockFace_UP = new Rectangle(block_pos.x, block_pos.x , 0.3f, 0.3f);
		
//		if(isAlive){
//			if(body.isActive()){
				if(boxRectangle.contains(blockFace_UP)){
					setType(BlockType.GRASS_TOP);
				}
//			}
//		}

tried blockFace_UP = new Rectangle(block_pos.x - Assets.blockSize / 2, block_pos.x - Assets.blockSize / 2, 0.3f, 0.3f); as well

and just check my y was x but correcting it did not work

Is that image what it should look like? it’s confusing. I can’t tell whats grass.

No y coord?


boxRectangle = new Rectangle(block_pos.x - Assets.blockSize / 2, block_pos.x - Assets.blockSize / 2, 0.3f, 0.3f);
   
blockFace_UP = new Rectangle(block_pos.x, block_pos.x , 0.3f, 0.3f);

to:


boxRectangle = new Rectangle(block_pos.x - Assets.blockSize / 2, block_pos.y - Assets.blockSize / 2, 0.3f, 0.3f);
   
blockFace_UP = new Rectangle(block_pos.x, block_pos.y , 0.3f, 0.3f);

i edited those boxes, is where the boxRectangle is and where blockFace_UP is the small once is all the blocks blockface up the bigger once are the blocks

I have no idea what you mean anymore.

checking the block with it self like this

		boxRectangle = new Rectangle(block_pos.x , block_pos.y , 0.3f , 0.3f);
	
		blockFace_UP = new Rectangle(block_pos.x , block_pos.y, 0.3f , 0.3f);

and check if they overlap etch other works

but when i do this

		boxRectangle = new Rectangle(block_pos.x , block_pos.y , 0.3f , 0.3f);
	
		blockFace_UP = new Rectangle(block_pos.x , block_pos.y + 0.4f, 0.3f , 0.3f);
		
		if(isAlive){
			if(body.isActive()){
				
				for(Chunk chunk : gameScreen.chunks){
					
					for(Block block : chunk.getBlocks()){
						if(block != this){
							if(!block.boxRectangle.overlaps(this.blockFace_UP)){
								setType(BlockType.DIRT);
							}else{
								setType(BlockType.GRASS_TOP);
							}
						}
	
						break;
					}
				}
			}
		}

it does not work

and if you wonder why i add 0.4f to blockface its so the rectangle check moves up one block

Honestly I don’t get your code.

if you use am array for the chunks you can do something like this:
Keeping it as similar as your original method.


for (Chunk chunk : chunks) {
    for (int y = 0; y < rows; y++) {
        for (int x = 0; x < columns; x++) {
            if (chunk.blocks[x][y + 1] != null) {
                //has block on top
            } else {
                //is top block    
            }
        }
    }
}

i’am using arraylist btw =P just so you know can you convert that?

can you? convert that code with arraylist instead?

This is what I gather from what a compilation of your posts have said about this chunking system: Each chunk is 8x8, the world is comprised of chunks stretching left and right procedurally (un-bounded), and fixed vertically. The chunks are stored in a List, and the block inside of chunks are stored in individual Lists. Correct?

Assuming this (and keeping the same system), I would store the blocks in arrays (either 1D or 2D, it doesn’t matter) inside each chunk instead of the Lists. The chunks themselves can continue to be stored in List format. That way, you can implement what KudoDEV suggested, which would be much easier than searching the List for each block.

well so i did but get error here

			for(int x = 0; x < chunkBlockList.length;x++){
				for(int y = 0; y < chunkBlockList.length;y++){
					if(chunkBlockList != null){
						chunkBlockList[x][y].draw(batch);
					}
				}
			}

and here is where i add blocks to chunk

		chunkBlockList = new Block[8][8];
		
		for(int x = 0; x < chunkBlockList.length;x++){
			for(int y = 0; y < chunkBlockList.length;y++){

				
				int col = map.getPixel(region.getRegionX() + x, region.getRegionY() + y);

				switch(col & 0xFFFFFFFF){
					
					case 0xFFFFFFFF:
						chunkBlockList[x][y] = new Block(
								this,
								gameScreen,
								chunk_pos.x + x * Assets.blockSize, 
								chunk_pos.y + y * Assets.blockSize, BlockType.DIRT);
						System.out.println("ADDED: NEW DIRT BLOCK: "+x+","+y + " TYPE: "+chunkBlockList[x][y].getBlockType());
						
						
					break;
					
				
				}
			}	
		}

it gives me the msg and it says it adds blocks so why do i get NullPointerException

EDIT: Got Rid off the error

You’re using


chunkBlockList.length

Edit: Oh you fixed it.

https://gyazo.com/e11f7eff60d2814e4d80d55642b2aa97

was not what i expected there is tons off dirt blocks at top that is still dirt and some chunks are just grass O,o

current code:

			    for (int y = 0; y < 7; y++) {
				        for (int x = 0; x < 8; x++) {
				            if (chunk.chunkBlockList[x][y + 1] != null) {
				                //has block on top
				            	setType(BlockType.DIRT);
				            } else {
				                //is top block    
				            	setType(BlockType.GRASS_TOP);
				            }
				        }
				    }

You need to learn to write test code. Design a universe with just 4 blocks and execute the method. What happens? Run it again using a debugger.

https://gyazo.com/623a1dc1b8f4fb4dd69abd8e306abc0b

well i kinda solve it but its still alot off dirt that is dirt at top