Jump to content

Detecting a block in front of an entity


Alron

Recommended Posts

I'm stuck on trying to detect a block in front of an entity. I have some code written for it but its acting really wierd and I can't figure out the error. I have tried to find it for over a day now, its driving me nuts.

 

So here is my code:

	public boolean checkFrontForBlock(){
	int i = (int)Math.round(getRotationY() * 2 / Math.PI) % 4;
	boolean detected = false;
	switch(i){
	case 0:
		detected = !worldObj.isAirBlock((int)(posX), (int)(posY), (int)(posZ)-1);
		break;
	case 1:
		detected = !worldObj.isAirBlock((int)(posX)+1, (int)(posY), (int)(posZ));
		break;
	case 2:
		detected = !worldObj.isAirBlock((int)(posX), (int)(posY), (int)(posZ)+1);
		break;
	case 3:
		detected = !worldObj.isAirBlock((int)(posX)-1, (int)(posY), (int)(posZ));
		break;
	default:
		System.out.println("Got to default statement");
	}
	System.out.println("Block in front: " + detected + ", i is " + i);
	return detected;
}

 

i is properly calculated and works, just theisAirBlock doesn't. Wich is wierd because the following movement code works no problem:

	public void moveForward(int distance, float speed){
	targetX = (float) (Math.floor(posX)+0.5F);
	targetZ = (float) (Math.floor(posZ)+0.5F);
	hasTarget = true;
	int i = (int)Math.round(getRotationY() * 2 / Math.PI) % 4;
	switch(i){
	case 0:
		motionZ = -speed;
		targetZ -= distance;
		break;
	case 1:
		motionX = speed;
		targetX += distance;
		break;
	case 2:
		motionZ = speed;
		targetZ += distance;
		break;
	case 3:
		motionX = -speed;
		targetX -= distance;
		break;
	}
}

 

A screenshot of the problem to show you how it's not working:

4jt1j7.png

 

The first and the last are detecting a block in front, the two in the middle don't.

The first one is facing north, the next one is rotated 90°(east) and so on.

So i is 0, 1, 2, 3 from left to right.

 

I hope somebody can help me, because Ijust can't figure it out. >.<

Link to comment
Share on other sites

Hi

 

It's not obvious to me what's wrong from looking at your code, but I would suggest that you add a couple of diagnostics to help figure out what's going wrong.  For example, instead of calling isAirBlock, call

 

blockID = worldObj.getBlockId(..)

 

System.out.println("Block in front: " + blockID + ", i is " + i);

 

then put a variety of different block types in the vicinity and see whether they are correctly detected.

 

Alternatively, change your code to use worldObj.setBlock(...) to change the block "in front" of the entity.  You'll see pretty quickly whether the code is looking in the right place.

 

-TGG

 

 

Link to comment
Share on other sites

Thanks a lot for the suggestions, I could fianlly figure it out. Looks like the positioning was off and I had to subtract 1 from posX and posZ to make it work. Don't know how that would make sense but it works now so I'm happy  :D

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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