How can i get the position behind a player

Discussion in 'Spigot Plugin Development' started by DommiHD, May 10, 2016.

  1. Shouldn't it be -distance for x and z?
    Also, please avoid spoon feeding on the forums ;) ... Especially that is relatively simple
     
    #21 Yamakaja, May 10, 2016
    Last edited: May 10, 2016
  2. No, it should be correct.

    Player faces to positive z: cos(0) = 1 but should subract distance from the current z coordinate. That's why it's negative.
    Player faces to negative x: sin(90) = 1 and should add distance to the current x coordinate. That's why it's positive.

    That's not spoon feeding, that's just help with math in 4 lines of code and not a whole solution for his problem.
     
    #22 stonar96, May 10, 2016
    Last edited: May 10, 2016
  3. Code (Text):

    // lets get the player's yaw rotation
    double rot = (player.getLocation().getYaw() - 90) % 360;
    if (rot < 0) {
        rot += 360.0;
    }

    // start with the block at the feet
    Block block = player.getLocation().getBlock();

    // get the block relative to where they are looking
    if ((0 <= rot && rot < 45) || (315 <= rot && rot < 360)) {
        // looking north, so get south block
        block = block.getRelative(BlockFace.SOUTH);
    } else if (45 <= rot && rot < 135) {
        // looking east, so get west block
        block = block.getRelative(BlockFace.WEST);
    } else if (135 <= rot && rot < 225) {
        // looking south, so get north block
        block = block.getRelative(BlockFace.NORTH);
    } else if (225 <= rot && rot < 315) {
        // looking west, so get east block
        block = block.getRelative(BlockFace.EAST);
    } else {
        // should not happen. if it does, find a better clamping algorithm for the yaw rotation
    }

    // now you have the block behind the player ^_^
    // get relative down if you want the block they just walked on (for flat surfaces)
    block = block.getRelative(BlockFace.DOWN);
     
     
  4. I don't think thats correct, but i'm looking for some evidence :p
     
  5. I am looking for some evidence that it is wrong. I have already described why it's true.
     
  6. Too many spoons were given today :'(
     
    • Agree Agree x 1
    • Agree Agree x 1
  7. Right ..., wth minecraft?
     
    • Funny Funny x 1
    • Friendly Friendly x 1