# from which direction is player looking at the block

Discussion in 'Plugin Development' started by matejdro, Apr 23, 2011.

Not open for further replies.
1. Offline

### matejdro

I wish to get information, from which direction is player looking at the block. Like north, south etc.

Something similar to event.getBlockFace() in PlayerInteractEvent, but only horizontal positioning (no up, down).

Anyone have idea, how can i get that?

#1
2. Offline

### Crash

Code:
float dir = (float)Math.toDegrees(Math.atan2(player.getLocation().getBlockX() - b.getX(), b.getZ() - player.getLocation().getBlockZ()));
If you want to convert it to BlockFace :
Code:
BlockFace face = getClosestFace(dir);

public BlockFace getClosestFace(float direction){

direction = direction % 360;

if(direction < 0)
direction += 360;

direction = Math.round(direction / 45);

switch((int)direction){

case 0:
return BlockFace.WEST;
case 1:
return BlockFace.NORTH_WEST;
case 2:
return BlockFace.NORTH;
case 3:
return BlockFace.NORTH_EAST;
case 4:
return BlockFace.EAST;
case 5:
return BlockFace.SOUTH_EAST;
case 6:
return BlockFace.SOUTH;
case 7:
return BlockFace.SOUTH_WEST;
default:
return BlockFace.WEST;

}
}

#2
Tabuu_ and EarlyLegend like this.
3. Offline

### matejdro

Wow, that's some heavy math.

Thanks.

#3
4. Offline

### Crash

It's just a tiny bit of trig. If you want it in radians just get rid of Math.toDegrees.

#4
5. Offline

### matejdro

Another question, somehow related to firs one, so i won't open a new thread:

When using player.getLastTwoTargetBlocks(), how can i retreive, which face of the block did player click?

#5
6. Offline

#6
7. Offline

### matejdro

blockface in player interact will work only on nearby blocks. When target block is farther away (RIGHT_CLICK_AIR triggers), it will only report DOWN as block face.

#7
8. Offline

### asdaarg

Then you need to do some collision detection yourself. You get the eye location of the player, and test for each axis whether its above, below or between the block boundaries on that axis. If above, you test the "top" face and below you test the "bottom" face (per axis). Then you have an equation system like this e.g:
Y = 5 (block face plane)
X=EyeX + k*DirX
Y=EyeY + k*DirY
Z=EyeZ + k*DirZ
so you get k = (5-EyeY)/DirY and then you get X and Z
Then check whether X and Z are inside the block boundaries. If it is, then you clicked on that face.

#8
9. Offline

### matejdro

Sorry, I'm quite confused now

I understand that for each face you need to test these equations. But what is the difference between equations for let's say top face and bottom face?

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: May 14, 2016
#9
10. Offline

### asdaarg

Y=4 for bottom face
and Y=5 for top face

#10
11. Offline

### matejdro

Um, i don't really get this one.

Can you explain it again, please?

#11
12. Offline

### asdaarg

Let's say you have a block x=12,y=50,z=-20
Then you have 6 faces to test:
X=12 (north)
X=13 (south)
Y=51 (top)
Y=50 (bottom)
Z=-20 (east)
Z=-19 (west)

#12
13. Offline

### matejdro

I mean i don't understand whole thing. To what should i compare equations.

#13
14. Offline

### asdaarg

Well, this would be easy to explain if you know set theory or at least boolean algebra. Collision detection is basically checking whether two geometric objects intersect. In boolean algebra you can think of a geometric proposition that describes one object being true, and another geometric proposition that describes another object being true, then there is an intersection. i.e. Collision = P1 && P2
So lets do this for the bottom face and the line of sight:
P1 = X=EyeX + k*DirX && Y=EyeY + k*DirY && Z=EyeZ + k*DirZ
and
P2 = Y=4 && X>12 && X<13 && Z>-19 && Z<20

Then
Collision = X=EyeX + k*DirX && Y=EyeY + k*DirY && Z=EyeZ + k*DirZ && Y=4 && X>12 && X<13 && Z>-19 && Z<20

#14
KoryuObihiro likes this.
15. Offline

### matejdro

Wow. Thanks. I will try to learn some boolean algebra and then examine this.

#15
16. Offline

### KoryuObihiro

#16
cholo71796 likes this.
17. Offline

### nisovin

There's a much easier way. You can use Block.getFace(Block) to find how two blocks are touching. So, if you use Player.getLastTwoTargetBlocks(), you can use those two blocks to determine which face is between them. Something like this:

Code:
List<Block> lastBlocks = player.getLastTwoTargetBlocks(null, range);
BlockFace face = lastBlocks.get(1).getFace(lastBlocks.get(0));
Edit: Immediately after posting, I realized this is a necro from months ago. Oh well.

#17
18. Offline

#18
19. Offline

### matejdro

:O

Thanks for raising this. Solution was so obvious. Thanks.

#19
20. Offline

### Telgar

What did you end up doing if I may ask? I have been staring at asdaarg's answer, which doesnt really make a lot of sense to me.

#20
21. Offline

### matejdro

I will try out nisovin's solution. But currently I'm just looping through all directions to see which one have space for placing portals.

#21
22. Offline

### com. BOY

This is what I did for determining what block face is pointing to the player:

Code:java
1. BlockFace blockFace = block.getFace(player.getLocation().getBlock());

#22
23. Offline

### Anonomoose

Thanks, I'm sure the OP from 3 years ago will really appreciate it.

#23