Solved get the direction a player is facing

Discussion in 'Plugin Development' started by Jogy34, Oct 13, 2012.

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

    Jogy34

    I have tried using yaw but if you turn one direction it will end up going negative. In the debug screen (F3) the is and f with a number that changes according to the direction you are facing, is there any way to get this. Also I've thought about using the getDirection() which return a vector but I'm not very good with working with vectors. I need to be able to get whether if a player would walk forward if they would be going mainly in the x+, x-, z+, or z- direction.
     
    victor2748 likes this.
  2. Offline

    bergerkiller

    How I do this in BKCommonLib: (source file)

    Snippet:
    PHP:
        public static final BlockFace[] axis = { BlockFace.NORTHBlockFace.EASTBlockFace.SOUTHBlockFace.WEST };
        public static final 
    BlockFace[] radial = { BlockFace.NORTHBlockFace.NORTH_EASTBlockFace.EASTBlockFace.SOUTH_EASTBlockFace.SOUTHBlockFace.SOUTH_WESTBlockFace.WESTBlockFace.NORTH_WEST };
       
        
    /**
        * Gets the horizontal Block Face from a given yaw angle<br>
        * This includes the NORTH_WEST faces
        *
        * @param yaw angle
        * @return The Block Face of the angle
        */
        
    public static BlockFace yawToFace(float yaw) {
            return 
    yawToFace(yawtrue);
        }
     
        
    /**
        * Gets the horizontal Block Face from a given yaw angle
        *
        * @param yaw angle
        * @param useSubCardinalDirections setting, True to allow NORTH_WEST to be returned
        * @return The Block Face of the angle
        */
        
    public static BlockFace yawToFace(float yawboolean useSubCardinalDirections) {
            if (
    useSubCardinalDirections) {
                return 
    radial[Math.round(yaw 45f) & 0x7];
            } else {
                return 
    axis[Math.round(yaw 90f) & 0x3];
            }
        }
    If you want a value ranging 0 - 3 like in the debug window, simply use Math.round(yaw / 90f);

    Note: you probably have to subtract a value or use inversion (360 - angle), as the dimension spaces differ between CraftBukkit and Minecraft client.
     
    Cloaking_Ocean and victor2748 like this.
  3. Offline

    Jogy34

    Thanks that works.
     
Thread Status:
Not open for further replies.

Share This Page