How to get BlockFace.TOP and BlockFace.BOTTOM from something like Player.getTargetBlockPosition() #2154
Unanswered
thebadlorax
asked this question in
Help
Replies: 1 comment
-
The upcoming (hopefully) raycasting api gives a unit vector for each intersection it comes across, until then I don't think there is a good way to do it. Vec getTargetBlockSurfaceNormal(
int distance,
Player player,
Instance instance
) {
Pos playerPos = player.getPosition();
Ray ray = new Ray(
playerPos.add(0, player.getEyeHeight(), 0),
playerPos.direction(),
distance,
config -> config.blockFilter(block -> !block.isAir()));
BlockCastResult castResult = ray.cast(instance);
return castResult.firstBlockCollision().entrySurfaceNormal();
} Or get the block position at the same time with this: Pos getTargetBlockWithSurfaceNormal(
int distance,
Player player,
Instance instance
) {
Pos playerPos = player.getPosition();
Ray ray = new Ray(
playerPos.add(0, player.getEyeHeight(), 0),
playerPos.direction(),
distance,
config -> config.blockFilter(block -> !block.isAir()));
BlockCastResult castResult = ray.cast(instance);
BlockCastResult.BlockRayCollision collision = castResult.firstBlockCollision();
Vec surfaceNormal = collision.entrySurfaceNormal();
return PositionUtils.lookAlong(
new Pos(collision.entry()),
surfaceNormal.x(),
surfaceNormal.y(),
surfaceNormal.z());
} Until then, the best I can do for you is this: BlockFace getFromPitchAndYaw(float pitch, float yaw) {
if (pitch <= -45) {
return BlockFace.TOP;
} else if (pitch >= 45) {
return BlockFace.BOTTOM;
}
float yawDegrees = (yaw - 90) % 360;
if (yawDegrees < 0) {
yawDegrees += 360;
}
if (0 <= yawDegrees && yawDegrees < 45) {
return BlockFace.WEST;
} else if (45 <= yawDegrees && yawDegrees < 135) {
return BlockFace.NORTH;
} else if (135 <= yawDegrees && yawDegrees < 225) {
return BlockFace.EAST;
} else if (225 <= yawDegrees && yawDegrees < 315) {
return BlockFace.SOUTH;
} else { // 315 <= yawDegrees && yawDegrees < 360
return BlockFace.WEST;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
im so stuck rn, i need these blockfaces but theres literally no way to get blockfaces from pitch and with yaw.
right now my code looks like:
blockDisplay.getPosition().relative(BlockFace.fromYaw(this.player.getPosition().yaw()).getOppositeFace())
,and this results in this, but when i try this, you can see it can never choose BlockFace.TOP() (or bottom). Please Save Me!!!!!
Beta Was this translation helpful? Give feedback.
All reactions