Jump to content

Getting the block a player is looking at?


sytone

Recommended Posts

I am trying to determine if a player is looking at a chest or not, I have looked through the Entity and the Look helper but I have not seen anything that helps. Is there anything that helps determine what a player is looking at in the code or do I have to get the player facing direction and eye height and mathematically walk the line of site and test for a block at each point until I hit !air or chest.

 

If so any examples as I am being lazy and my trig is rusty :)

Link to comment
Share on other sites

float f = 1.0F;
                        float f1 = p.prevRotationPitch + (p.rotationPitch - p.prevRotationPitch) * f;
                        float f2 = p.prevRotationYaw + (p.rotationYaw - p.prevRotationYaw) * f;
                        double d0 = p.prevPosX + (p.posX - p.prevPosX) * (double)f;
                        double d1 = p.prevPosY + (p.posY - p.prevPosY) * (double)f + 1.62D - (double)p.yOffset;
                        double d2 = p.prevPosZ + (p.posZ - p.prevPosZ) * (double)f;
                        Vec3 vec3 = world.getWorldVec3Pool().getVecFromPool(d0, d1, d2);
                        float f3 = MathHelper.cos(-f2 * 0.017453292F - (float)Math.PI);
                        float f4 = MathHelper.sin(-f2 * 0.017453292F - (float)Math.PI);
                        float f5 = -MathHelper.cos(-f1 * 0.017453292F);
                        float f6 = MathHelper.sin(-f1 * 0.017453292F);
                        float f7 = f4 * f5;
                        float f8 = f3 * f5;
                        double d3 = 5.0D;
                        Vec3 vec31 = vec3.addVector((double)f7 * d3, (double)f6 * d3, (double)f8 * d3);
                        MovingObjectPosition movingobjectposition = world.rayTraceBlocks_do_do(vec3, vec31, false, true);
                        if (movingobjectposition == null) {
                        	return;
                        }
                        if (movingobjectposition.typeOfHit == EnumMovingObjectType.TILE)
                        {
                            int ix = movingobjectposition.blockX;
                            int iy = movingobjectposition.blockY;
                            int iz = movingobjectposition.blockZ;
                            //block is at ix, iy, iz
                        }

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Flipin awesome! That saved some math ;)

 

I had a sinking feeling that this would have been needed and I would be pulling out my old 3d math logic :)

 

TBH, I yoinked that out of the....boat code.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.