Angle of player to block

Discussion in 'Bukkit Help' started by Dragonphase, Jan 4, 2013.

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

    Dragonphase

    Hi, I was hoping to find a solution to this myself and actually came quite far, but still, I have so many problems with this code.

    What I am trying to do is determine which face of the block the player is looking at, and since there is no simple function to do this with Bukkit, I've had to design my own way of doing it:

    PHP:
    int playerYaw Math.round(player.getLocation().getYaw());
    int Yaw playerYaw 90;
    int angle 0;
     
    if (
    Yaw 360Yaw -= 360;
     
    switch (
    yawToFace(playerYaw)){
                    case 
    NORTH:
                        
    newLoc.add(00, -1).getBlock().setType(blockMaterial);
                        
    player.sendMessage("north");
                        break;
                    case 
    NORTH_EAST:
                        
    angle = (int) Math.round(Math.atan2(player.getLocation().getZ() - loc.getZ(), player.getLocation().getX() - loc.getX()) * 180 Math.PI) + 180;
                       
                        if (
    Yaw anglenewLoc.add(100).getBlock().setType(blockMaterial);
                        else  
    newLoc.add(00, -1).getBlock().setType(blockMaterial);
                        break;
                    case 
    EAST:
                        
    newLoc.add(100).getBlock().setType(blockMaterial);
                        break;
                    case 
    SOUTH_EAST:
                        
    angle = (int) Math.round(Math.atan2(player.getLocation().getZ() - loc.getZ(), player.getLocation().getX() - loc.getX()) * 180 Math.PI) + 180;
                       
                        if (
    Yaw anglenewLoc.add(001).getBlock().setType(blockMaterial);
                        else  
    newLoc.add(100).getBlock().setType(blockMaterial);
                        break;
                    case 
    SOUTH:
                        
    newLoc.add(001).getBlock().setType(blockMaterial);
                        break;
                    case 
    SOUTH_WEST:
                        
    angle = (int) Math.round(Math.atan2(player.getLocation().getZ() - loc.getZ(), player.getLocation().getX() - loc.getX()) * 180 Math.PI) + 180;
                       
                        if (
    Yaw anglenewLoc.add(-100).getBlock().setType(blockMaterial);
                        else  
    newLoc.add(001).getBlock().setType(blockMaterial);
                        break;
                    case 
    WEST:
                        
    newLoc.add(-100).getBlock().setType(blockMaterial);
                        break;
                    case 
    NORTH_WEST:
                        
    angle = (int) Math.round(Math.atan2(player.getLocation().getZ() - loc.getZ(), player.getLocation().getX() - loc.getX()) * 180 Math.PI) + 180;
                       
                        if (
    Yaw anglenewLoc.add(00, -1).getBlock().setType(blockMaterial);
                        else  
    newLoc.add(-100).getBlock().setType(blockMaterial);
                        break;
                    default:
                        break;
                    }
    What you see there is my code, unorganized and simply pasted straight from my project which is currently in debugging mode.

    The function yawToFace was taken somewhere from bukkit forums, here is its' associated code:

    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 };
       
        public static 
    BlockFace yawToFace(float yaw){
            return 
    yawToFace(yawtrue);
        }
       
        public static 
    BlockFace yawToFace(float yawboolean useSubCardinalDirections){
            if (
    useSubCardinalDirections){
                return 
    radial[Math.round(yaw 45f) & 0x7];
            }else{
                return 
    axis[Math.round(yaw 90f) & 0x3];
            }
        }

    Here are the problems, in order of importance:

    1. pitchToYaw's north is MineCraft's south
    2. pitchToYaw is 90 degrees greater than angle
    3. angle gives a range between -180 and 180, whereas pitchToYaw gives a range between 0 and 360
    4. All of the code works, except from the case on "SOUTH_WEST" and "NORTH_EAST", where the pitchToYaw is about 4-10 points less than what it should be (which means that Yaw will not be greater than angle, and the block will almost always be set on the left of the clicked block)
    If there is another way to get around this, such as a function which tells me what face of the block the player is looking at, or if anyone can help me, I would greatly appreciate it.
    Thanks,
    Dragonphase
     
Thread Status:
Not open for further replies.

Share This Page