Jump to content

Get Yaw/Pitch to look at block.


Niroxbtw

Recommended Posts

Hello, 

im trying to make a method that determines a yaw/pitch to look at a certain block. A method with Pythagoras doesn't work because it fails when theres a block in the angle. So my idea was to create a dummy entity at the player position which will try out all yaws/pitches and then ray trace to see what blocks get hit and what yaw/pitch was used to hit that block. So i first created a new EntityLivingBase:

    EntityLivingBase playerSim = new EntityLivingBase(mc.theWorld) {
        @Override
        public ItemStack getHeldItem() {
            return null;
        }

        @Override
        public ItemStack getEquipmentInSlot(int slotIn) {
            return null;
        }

        @Override
        public ItemStack getCurrentArmor(int slotIn) {
            return null;
        }

        @Override
        public void setCurrentItemOrArmor(int slotIn, ItemStack stack) {
        }

        @Override
        public ItemStack[] getInventory() {
            return new ItemStack[0];
        }
    };

Then i wrote the method that trys out all yaws and pitches:

public ArrayList<Pair<BlockPos, Pair<Float, Float>>> getLookForAllVisibleBlocks(EntityLivingBase entityLivingBase) {
        entityLivingBase.setPosition(mc.thePlayer.getPosition().getX() + 0.5d, mc.thePlayer.getPosition().getY(), mc.thePlayer.getPosition().getZ() + 0.5d );
        ArrayList<Pair<BlockPos, Pair<Float, Float>>> visibleBlocksWithLook = new ArrayList<>();
        for (int yaw = 0; yaw < 360; yaw++) {
            for (int pitch = 0; pitch < 180; pitch++) {
                entityLivingBase.rotationPitch = pitch - 90;
                entityLivingBase.rotationYawHead = yaw - 180;
                Vec3 vec3 = entityLivingBase.getPositionEyes(1);
                Vec3 vec31 = entityLivingBase.getLook(1);
                Vec3 vec32 = vec3.addVector(vec31.xCoord * 5, vec31.yCoord * 5, vec31.zCoord * 5);
                MovingObjectPosition playerSimRay = mc.theWorld.rayTraceBlocks(vec3, vec32, false, false, true);
                boolean add = true;
                for (Pair<BlockPos, Pair<Float, Float>> visibleBlock: visibleBlocksWithLook) {
                    if (visibleBlock.getLeft().equals(playerSimRay.getBlockPos())) {
                        add = false;
                    }
                }
                if (add) {
                    visibleBlocksWithLook.add(Pair.of(playerSimRay.getBlockPos(), Pair.of((float) (yaw - 180), (float) (pitch - 90))));
                }
            }
        }
        return visibleBlocksWithLook;
    }

This indeed returns a pitch and a yaw but its failing 50% of the time and i dont have any idea why it does. My guess is that the rotationPitch/rotationYawHead are different from whats used in the f3 menu. I tried out to log the yaw and pitch and the pitch seemed to be exactly the same but the yaw was different but it seemed like it was a multiple of the rotations in deg and since this method only counts up to 360 (for the yaw) it shouldnt be a problem. There also could be something else wrong with this. Thanks for the help in advance.

 

EDIT:

The Ray Trace can't be the problem since i tested it out. I also tested the yaw and pitch and they are also correct. I have no clue at all what the problem could be.

 

Edited by Niroxbtw
Link to comment
Share on other sites

If you know the position of the block you want to look at, you can easily calculate the x and y angle necessary to view the block. However, if you want to rotate the player's head to look at that position, it requires a little more finesse. Specifically, rotations are stored between [-180,180). Additionally, the x rotation is clamped to [-90,90]. The head rotations are set via `#setYHeadRot` and `#setXRot`.

Link to comment
Share on other sites

Thanks for your reply. The rotation of the player is not a problem for me. My main problem is to get a required yaw/pitch. Since i thought that it would be pretty hard to calculate i tried to brute force it. That doesnt seem to work.

Quote

If you know the position of the block you want to look at, you can easily calculate the x and y angle necessary to view the block.

How would i calculate that since pythagoras and basic trig wont work because there are cases where the yaw and pitch of pythagoras (trig) is blocked by another block?

 

Link to comment
Share on other sites

On 4/4/2023 at 11:27 AM, Niroxbtw said:

How would i calculate that since pythagoras and basic trig wont work because there are cases where the yaw and pitch of pythagoras (trig) is blocked by another block?

 

But you know the block position of the block you want to look at correct? Then why would it matter if there is another block in the way?

Link to comment
Share on other sites

That's terribly inefficient. The better way to do it would be to loop through the surrounding blocks, starting with those closest, and find the block you want, only then checking whether the raytrace hits the wanted block. You can actually do some easy optimizations there since you just need to stop looking through any blocks that aren't air.

However, if you already have the block position, then it makes no sense to do the ray trace since you already know where the block is. You would be better off trying to figure out how to represent the data.

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.



×
×
  • Create New...

Important Information

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