Jump to content

how to get the side of the block the player is currently looking at?


Recommended Posts

Posted

I've been exploring code for the last 4 hours and I can't find a way to make that work. 

 

Also tried the search on the forums but couldn't come up with better keywords, I'm afraid.

 

Is it possible to get the "NORTH; EAST; WEST; UP; DOWN; SOUTH" side  of a block corresponding to the player crosshair?

 

I want to call it inside of onItemUse, or onItemUseFirst.

Posted

Pulled this from my code, use/edit freely

 

/**
 * If [4] is -1 , there was no block within reach.
 * @return [0]= BlockId, [1]= BlockX, [2]= BlockY, [3]= BlockZ, [4]= SideHit, [5]= BlockMeta
 */
public static int[] getBlockInfront(World theWorld, EntityPlayer thePlayer, double reach)
{
	int[] blockInfo = { 0, 0, 0, 0, -1, 0 };

	MovingObjectPosition movingobjectposition = getMovingObjectPositionFromPlayer(theWorld, thePlayer, true, reach);
	if (movingobjectposition != null) {
		if (movingobjectposition.typeOfHit == EnumMovingObjectType.TILE) {
			blockInfo[1] = movingobjectposition.blockX;
			blockInfo[2] = movingobjectposition.blockY;
			blockInfo[3] = movingobjectposition.blockZ;
			blockInfo[4] = movingobjectposition.sideHit;
			blockInfo[5] = theWorld.getBlockMetadata(blockInfo[1], blockInfo[2], blockInfo[3]);
			blockInfo[0] = theWorld.getBlockId(blockInfo[1], blockInfo[2], blockInfo[3]);
		}
	}
	return blockInfo;
}

private static MovingObjectPosition getMovingObjectPositionFromPlayer(World world, EntityPlayer entityplayer, boolean flag, double reach)
{
	float f = 1.0F;
	float playerPitch = entityplayer.prevRotationPitch + (entityplayer.rotationPitch - entityplayer.prevRotationPitch) * f;
	float playerYaw = entityplayer.prevRotationYaw + (entityplayer.rotationYaw - entityplayer.prevRotationYaw) * f;
	double playerPosX = entityplayer.prevPosX + (entityplayer.posX - entityplayer.prevPosX) * f;
	double playerPosY = (entityplayer.prevPosY + (entityplayer.posY - entityplayer.prevPosY) * f + 1.6200000000000001D) - entityplayer.yOffset;
	double playerPosZ = entityplayer.prevPosZ + (entityplayer.posZ - entityplayer.prevPosZ) * f;
	Vec3 vecPlayer = Vec3.createVectorHelper(playerPosX, playerPosY, playerPosZ);
	float cosYaw = MathHelper.cos(-playerYaw * 0.01745329F - 3.141593F);
	float sinYaw = MathHelper.sin(-playerYaw * 0.01745329F - 3.141593F);
	float cosPitch = -MathHelper.cos(-playerPitch * 0.01745329F);
	float sinPitch = MathHelper.sin(-playerPitch * 0.01745329F);
	float pointX = sinYaw * cosPitch;
	float pointY = sinPitch;
	float pointZ = cosYaw * cosPitch;
	Vec3 vecPoint = vecPlayer.addVector(pointX * reach, pointY * reach, pointZ * reach);
	MovingObjectPosition movingobjectposition = world.rayTraceBlocks_do_do(vecPlayer, vecPoint, flag, !flag);
	return movingobjectposition;
}

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.