# How can i get the position behind a player

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

1. ### Yamakaja

Shouldn't it be -distance for x and z?
Also, please avoid spoon feeding on the forums ... Especially that is relatively simple

#21
Last edited: May 10, 2016
2. ### stonar96

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
Last edited: May 10, 2016
3. ### BillyGalbreath

Code (Text):

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

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. ### Yamakaja

I don't think thats correct, but i'm looking for some evidence

5. ### stonar96

I am looking for some evidence that it is wrong. I have already described why it's true.

7. ### ProJoosh

Too many spoons were given today :'(

• Agree x 1

• Agree x 1
9. ### Yamakaja

Right ..., wth minecraft?

• Funny x 1
• Friendly x 1