Skip to content

Instantly share code, notes, and snippets.

@Robijnvogel
Created December 3, 2017 17:48
Show Gist options
  • Save Robijnvogel/9d65e86c955fc3f5726e211e1fd6a5c3 to your computer and use it in GitHub Desktop.
Save Robijnvogel/9d65e86c955fc3f5726e211e1fd6a5c3 to your computer and use it in GitHub Desktop.
//lookYaw within 0 - 360
//lookPitch within 0 - 180
//coneReach within 0 - 360
getBlockPossesWithinCone(BlockPos origPos, float lookYaw, float lookPitch, int coneReach, int coneLength) {
//todo check preconditions
double deviation = ( (double) coneReach ) / 2
float minYaw = (lookYaw - deviation + 360.0 ) % 360;
float maxYaw = (lookYaw + deviation ) % 360;
float minPitch = (lookPitch - deviation + 360.0 ) % 360;
float maxPitch = (lookPitch + deviation ) % 360;
int xMin = origPos.x - coneLength;
int xMax = origPos.x + coneLength;
int yMin = origPos.y - coneLength;
int yMax = origPos.y + coneLength;
int zMin = origPos.z - coneLength;
int zMax = origPos.z + coneLength;
//restricting yMax
if (maxPitch <= 90) {
if (minPitch < maxPitch || minPitch >= 270) {
yMax = origPos.y;
}
} else if (maxPitch >= 270) {
if (minPitch > maxPitch || minPitch <= 90)) {
yMax = origPos.y;
}
}
//restricting yMin
if (maxPitch >= 90 && maxPitch <= 270) {
if (minPitch < maxPitch) {
yMin = origPos.y;
}
}
if (minPitch < maxPitch) {
//the player is (almost) looking straight up or down, so no restrictions can be made in the x or z direction.
} else {
//todo do a similar check for x and z min and max depending on min and max pitch **and** yaw
// don't forget the "if (minYaw < maxYaw)" possibility
}
Set<BlockPos> BoxBlockPosSet = new HashSet<BlockPos>();
BoxBlockPosSet.addAll(Lists.newArrayList(BlockPos.getAllInBox(
new BlockPos(xMin, yMin, zMin),
new BlockPos(xMax, yMax, zMax)
)));
Set<BlockPos> coneBlockPosSet = new HashSet<BlockPos>();
for(BlockPos pos: BoxBlockPosSet) {
xDiff = pos.x - orig.x;
yDiff = pos.y - orig.y;
zDiff = pos.z - orig.z;
if (squareRoot( square(xDiff) + square(yDiff) + square(zDiff) ) <= coneLength) {
Vec3D vector = getVector(origPos, pos);
posYaw = vector.getYaw();
posPitch = vector.getPitch();
//and here is where I need more coffee (I don't think using minYaw and maxYaw will be needed.
if (vector within cone) {
coneBlockPosSet.add(pos);
}
}
//else it is too far away from the player, so don't add it to the cone set
}
return coneBlockPosSet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment