How to get yaw required for player to see location?

Discussion in 'Plugin Development' started by KJG, Oct 23, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    KJG

    Hello

    I'm looking for a way to get the Yaw value that the player needs to see A point.

    I tried many ways, but I couldn't find it.

    If point A is present in x 100, z 100, I need a way to get how much Yaw value the player needs to look at point A.

    Summary : If Location A and Location B is present, how to find the Yaw value when A is looking at B
     
  2. Offline

    davidclue

    You can get a player instance and use
    Code:
    player.getTargetBlock(Set<Material> filter, int rangeLimit);
    The filter can be null and it will ignore all air blocks, the range is self explanatory and you can use
    Code:
    Block b = player.getTargetBlock(null, 50);
    In a repeating task to check the coordinates of the block.
     
    Last edited: Oct 24, 2020
  3. Offline

    spuddy

    Let's take your scenario of A and B. A is the player's location and B is the target location.

    Since you just want the Yaw, the elevation (Y coordinate) of both locations is I assume irrelevant?
    Let's use Location objects for each point. So the player's location and the target location, we'll call those locA and locB.
    Now, we'll subtract A from B and turn it into a normalized vector.

    Vector dir = locB.subtract(locA).toVector()
    dir.setY(0);
    dir = dir.normalize();

    Now you have direction vector. To get the Yaw (rotation about the Y axis) we use the x and z of the vector. Which is essentially a point on the circumference of a unit circle. Since the cosine of an angle gives us the x value of the point on that circle, we use the inverse function arc cosine to give us the angle of that point (the yaw)

    double yaw = Math.acos(dir.getX())

    But, we're still not quite done as there's two possible answers, acos just returns the principle value. The two possible answers basically just represent if you're measruing the angle clockwise or counter clockwise. That's why we use arc cosine, because the answer is always positive and we only need to negate it to get the other answer. So to check if the angle we get needs to be negative, we check if the Z of the vector is negative.

    if(dir.getZ() < 0) { yaw = -yaw; }

    To merge all of that.

    Code:
        public double getYaw(Location locA, Location locB) {
            Vector dir  = locB.subtract(locA).toVector();
            dir.setY(0);
            dir = dir.normalize();
            return dir.getZ() < 0 ? -Math.acos(dir.getX()) : Math.acos(dir.getX()); 
        }
    The angle is of course in radians, not degrees but you can convert it using Math.toDegrees() easily enough. Also may need to be flipped (negation done the other way around and/or z and x) as Minecraft tends to use unconventional methods in these areas, and I can't test anything currently to verify. So you may have to mess around with it a little.
     
    Last edited: Oct 24, 2020
Thread Status:
Not open for further replies.

Share This Page