Jump to content

[1.8]Create Explosion at look position


Recommended Posts

I have been googling this for the last 30 minutes and for the life of me I can't figure out what I did wrong.  When i use this it creates an explosion somewhere that seems to be related to where I am standing and the block I'm looking at but not actually AT the block.  I'm sure it's a really simple thing I overlooked but I'm just not seeing it.


	public void execute(ICommandSender sender, String[] args) throws CommandException {
    	BlockPos blk = sender.getEntityWorld().rayTraceBlocks(sender.getPositionVector(), sender.getCommandSenderEntity().getLookVec()).getBlockPos();
    	sender.getEntityWorld().createExplosion(sender.getCommandSenderEntity(),blk.getX(),blk.getY(),blk.getZ(),5, false);

Link to comment
Share on other sites

The look vector for an entity is not a position in the world. It's just an angle, basically. You're treating it as a position. (Look vector values are only between -1 and 1).


To find the position you are looking at, you can a function found here: http://jabelarminecraft.blogspot.com/p/minecraft-modding-extending-reach-of.html


That function only works client side, so a universal version is like so:


public static MovingObjectPosition getMouseOverExtendedUniversal(Entity theRenderViewEntity, float dist) {

	AxisAlignedBB theViewBoundingBox = new AxisAlignedBB(
			theRenderViewEntity.posX - 0.5D,
			theRenderViewEntity.posY - 0.0D,
			theRenderViewEntity.posZ - 0.5D,
			theRenderViewEntity.posX + 0.5D,
			theRenderViewEntity.posY + 1.5D,
			theRenderViewEntity.posZ + 0.5D

	MovingObjectPosition returnMOP = null;
	if (theRenderViewEntity.worldObj != null)
		double var2 = dist;
		returnMOP = theRenderViewEntity.rayTrace( var2, 0 );
		double calcdist = var2;
		Vec3 pos = getPositionEyes( theRenderViewEntity, 0 );
		var2 = calcdist;
		if (returnMOP != null)
			calcdist = returnMOP.hitVec.distanceTo( pos );

		Vec3 lookvec = theRenderViewEntity.getLook( 0 );
		Vec3 var8 = pos.addVector( lookvec.xCoord * var2,

				lookvec.yCoord * var2,

				lookvec.zCoord * var2 );
		Entity pointedEntity = null;
		float var9 = 1.0F;
		List<Entity> list = theRenderViewEntity.worldObj



								lookvec.xCoord * var2,

								lookvec.yCoord * var2,

								lookvec.zCoord * var2 ).expand( var9, var9,
								var9 ) );
		double d = calcdist;

		for (Entity entity : list)
			if (entity.canBeCollidedWith())
				float bordersize = entity.getCollisionBorderSize();
				AxisAlignedBB aabb = new AxisAlignedBB(

						entity.posX - entity.width / 2,


						entity.posZ - entity.width / 2,

						entity.posX + entity.width / 2,

						entity.posY + entity.height,

						entity.posZ + entity.width / 2 );
				aabb.expand( bordersize, bordersize, bordersize );
				MovingObjectPosition mop0 = aabb.calculateIntercept( pos,
						var8 );

				if (aabb.isVecInside( pos ))
					if (0.0D < d || d == 0.0D)
						pointedEntity = entity;
						d = 0.0D;
				} else if (mop0 != null)
					double d1 = pos.distanceTo( mop0.hitVec );

					if (d1 < d || d == 0.0D)
						pointedEntity = entity;
						d = d1;

		if (pointedEntity != null && (d < calcdist || returnMOP == null))
			returnMOP = new MovingObjectPosition( pointedEntity );

	return returnMOP;



The parameter dist is the max distance to check for a position. The moving object position returned is where you want to spawn the explosion.


With all due respect, sir: I do, what I do, the way I do it. ~ MacGyver

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.

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.


  • Create New...

Important Information

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