Skip to content

Instantly share code, notes, and snippets.

@Robijnvogel
Created December 4, 2017 01:17
Show Gist options
  • Save Robijnvogel/71121967e60879b8814b15064f1c0aaf to your computer and use it in GitHub Desktop.
Save Robijnvogel/71121967e60879b8814b15064f1c0aaf to your computer and use it in GitHub Desktop.
//only three-sided pyramidic cone?
boolean isPosInCone(BlockPos pos, Vec3D topPoint, Vec3D coneVector, int slopeAngle) {
//Cone
int coneX = coneVector.X;
int coneY = coneVector.Y;
int coneZ = coneVector.Z;
//XY and ZY planes
int coneDiagXZ = squareRoot(square(coneX) + square(coneZ));
int coneAngleXZ;
if (coneDiagXZ == 0) {
coneAngleXZ = 0;
} else {
coneAngleXZ = tan-1(coneY / coneDiagXZ); //this would be the angle to project onto the other planes to check if it's within the margins.
}
int maxConeAngleXZ = coneAngleXZ + slopeAngle; //min value to project
int minConeAngleXZ = coneAngleXZ - slopeAngle; //max value to project
int maxConeY = tan(maxConeAngleXZ) * coneDiagXZ;
int minConeY = tan(minConeAngleXZ) * coneDiagXZ;
int maxProjectedAngleX = tan-1(maxConeY/coneX);
int minProjectedAngleX = tan-1(minConeY/coneX);
int maxProjectedAngleZ = tan-1(maxConeY/coneZ);
int minProjectedAngleZ = tan-1(minConeY/coneZ);
//ZX plane
int coneDiagYZ = squareRoot(square(coneY) + square(coneZ));
int coneAngleYZ;
if (coneDiagYZ == 0) {
coneAngleYZ = 0;
} else {
coneAngleYZ = tan-1(coneX / coneAngleYZ);
}
int maxConeAngleYZ = coneAngleYZ + slopeAngle; //min value to project
int minConeAngleYZ = coneAngleYZ - slopeAngle; //max value to project
int maxConeX = tan(maxConeAngleYZ) * coneDiagYZ;
int minConeX = tan(minConeAngleYZ) * coneDiagYZ;
int maxProjectedAngleY = tan-1(maxConeX/coneZ);
int minProjectedAngleY = tan-1(minConeX/coneZ);
//BlockState
Vec3D blockVector = pos - topPoint;
int blockX = blockVector.X;
int blockY = blockVector.Y;
int blockZ = blockVector.Z;
//XY and ZY planes
int blockDiagXZ = squareRoot(square(blockX) + square(blockZ));
int blockAngleXZ;
if (blockDiagXZ == 0) {
blockAngleXZ = 0;
} else {
blockAngleXZ = tan-1(blockY / blockDiagXZ); //this would be the angle to project onto the other planes to check if it's within the margins.
}
int blockProjectedY = tan(blockAngleXZ) * blockDiagXZ;
int blockProjectedAngleX = tan-1(blockProjectedY/blockX);
if (!angleIsBetween(blockProjectedAngleX, minProjectedAngleX, maxProjectedAngleX) return false;
int blockProjectedAngleZ = tan-1(blockProjectedY/blockZ);
if (!angleIsBetween(blockProjectedAngleZ, minProjectedAngleZ, maxProjectedAngleZ) return false;
//ZX plane
int blockDiagYZ = squareRoot(square(blockY) + square(blockZ));
int blockAngleYZ;
if (blockDiagYZ == 0) {
blockAngleYZ = 0;
} else {
blockAngleYZ = tan-1(blockX / blockAngleYZ);
}
int blockProjectedX = tan(blockAngleYZ) * blockDiagYZ;
int blockProjectedAngleY = tan-1(blockProjectedX/blockZ);
if (!angleIsBetween(blockProjectedAngleY, maxProjectedAngleY, maxProjectedAngleY) return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment