Skip to content

Instantly share code, notes, and snippets.

@BluSunrize
Created June 21, 2017 13:13
Show Gist options
  • Save BluSunrize/7039a79da56a325afb6bcf02fe75dfbc to your computer and use it in GitHub Desktop.
Save BluSunrize/7039a79da56a325afb6bcf02fe75dfbc to your computer and use it in GitHub Desktop.
Java method (using Minecraft vectors) to detect a point in cone
/**
* Checks if point is contained within a cone in 3D space
*
* @param start tip of the cone
* @param normDirection normalized (length==1) vector, direction of cone
* @param radius radius at the end of the cone
* @param length length of the cone
* @param truncationLength optional lenght at which the cone is truncated (flat tip)
* @param relativePoint point to be checked, relative to {@code start}
*/
public static boolean isPointInCone(Vec3d start, Vec3d normDirection, double radius, double length, float truncationLength, Vec3d relativePoint)
{
double projectedDist = relativePoint.dotProduct(normDirection); //Orthogonal projection, establishing point's distance on cone direction vector
if(projectedDist<truncationLength || projectedDist>length) //If projected distance is before truncation or beyond length, point not contained
return false;
double radiusAtDist = projectedDist/length * radius; //Radius of the cone at the projected distance
Vec3d orthVec = relativePoint.subtract(normDirection.scale(projectedDist)); //Orthogonal vector between point and cone direction
return orthVec.lengthSquared()<(radiusAtDist*radiusAtDist); //Check if Vector's length is shorter than radius -> point in cone
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment