Jump to content

Setting bounding boxes on a per-block basis...


voidzm

Recommended Posts

I'm trying to create a block that changes the way that it renders based on whether blocks of the same ID are directly next to it. I've implemented an ISimpleBlockRenderingHandler that takes care of the rendering itself, and that works perfectly, but I'm having trouble getting the bounding box of the blocks to appropriately update. My renderWorldBlock function calls back to the source block to update its bounds with this code:

 

public void updateBoundingBox(IBlockAccess world, int x, int y, int z) {
	float lowX = 0.25F;
	float lowY = 0.25F;
	float lowZ = 0.25F;
	float highX = 0.75F;
	float highY = 0.75F;
	float highZ = 0.75F;
	if(world.getBlockId(x+1, y, z) == this.blockID) {
		highX = 1.0F;
	}
	if(world.getBlockId(x-1, y, z) == this.blockID) {
		lowX = 0.0F;
	}
	if(world.getBlockId(x, y+1, z) == this.blockID) {
		highY = 1.0F;
	}
	if(world.getBlockId(x, y-1, z) == this.blockID) {
		lowY = 0.0F;
	}
	if(world.getBlockId(x, y, z+1) == this.blockID) {
		highZ = 1.0F;
	}
	if(world.getBlockId(x, y, z-1) == this.blockID) {
		lowZ = 0.0F;
	}
	this.setBlockBounds(lowX, lowY, lowZ, highX, highY, highZ);
}

 

Each time I place a new instance of the block, every single instance of the block in the world changes its bounding box as well, as if this is modifying the global setting for the block. I've also tried implementing this function which I saw in BlockFence:

 

public AxisAlignedBB getCollisionBoundingBoxFromPool(IBlockAccess par1World, int x, int y, int z) {
	float lowX = 0.25F;
	float lowY = 0.25F;
	float lowZ = 0.25F;
	float highX = 0.75F;
	float highY = 0.75F;
	float highZ = 0.75F;
	if(par1World.getBlockId(x+1, y, z) == this.blockID) {
		highX = 1.0F;
	}
	if(par1World.getBlockId(x-1, y, z) == this.blockID) {
		lowX = 0.0F;
	}
	if(par1World.getBlockId(x, y+1, z) == this.blockID) {
		highY = 1.0F;
	}
	if(par1World.getBlockId(x, y-1, z) == this.blockID) {
		lowY = 0.0F;
	}
	if(par1World.getBlockId(x, y, z+1) == this.blockID) {
		highZ = 1.0F;
	}
	if(par1World.getBlockId(x, y, z-1) == this.blockID) {
		lowZ = 0.0F;
	}
	return AxisAlignedBB.getAABBPool().addOrModifyAABBInPool((double)((float)x + lowX), (double)((float)y + lowY), (double)((float)z + lowZ), (double)((float)x + highX), (double)((float)y + highY), (double)((float)z + highZ));
}

 

This doesn't help at all. I'm rather confused with this, since I've basically copied what BlockFence does for its bounding, but for some reason it won't work on the per-block level.

 

Link to comment
Share on other sites

Well, after much fruitless Googling, I found what my problem was. Apparently my "updateBoundingBox" method needed to be called the same thing that it is called in  BlockFence and BlockWall, which is "setBoundingBoxBasedOnState". For some reason, Minecraft calls that particular function and then the "setBlockBounds" call applies only to the block in question. It doesn't look like the AABB function does much except for entity raytracing and pathfinding, so that had nothing to do with it.

 

Hope someone else finds this useful!

Link to comment
Share on other sites

To clarify, there are two different notions of bounding boxes. One of them is kept in a set of static variables in the Block class, and the game calls the block's setBoundingBoxBasedOnState method immediately before doing anything that uses it. Because it's static, if you don't update it at the right times you get weird interactions between blocks. This bounding box is used for drawing the wireframe around the block you're looking at.

 

The other kind of bounding boxes are used for collision detection with entities; this is what getCollisionBoundingBoxFromPool is for. It's also possible for a block to have multiple collision boxes by overriding addCollidingBlockToList instead and adding more than one block to the list; stairs do this, for example.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.