@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
59 lines (49 loc) • 1.93 kB
JavaScript
import { max2 } from "../../../math/max2.js";
import { min2 } from "../../../math/min2.js";
import { v3_angle_between } from "../../vec3/v3_angle_between.js";
/**
* NOTE: used stackoverflow answer as a basis for circle part: https://stackoverflow.com/questions/2592011/bounding-boxes-for-circle-and-arcs-in-3d
* @param {number[]|ArrayLike<number>|Float32Array} result
* @param {number} originX
* @param {number} originY
* @param {number} originZ
* @param {number} directionX
* @param {number} directionY
* @param {number} directionZ
* @param {number} angle in Radians
* @param {number} length
*/
export function computeConeBoundingBox(
result,
originX, originY, originZ,
directionX, directionY, directionZ,
angle,
length
) {
const ax = v3_angle_between(directionX, directionY, directionZ, 1, 0, 0);
const ay = v3_angle_between(directionX, directionY, directionZ, 0, 1, 0);
const az = v3_angle_between(directionX, directionY, directionZ, 0, 0, 1);
//compute radius of the base of the cone
const radius = length * Math.tan(angle);
//compute cone base center
const centerX = originX + directionX * length;
const centerY = originY + directionY * length;
const centerZ = originZ + directionZ * length;
const rX = Math.sin(ax) * radius;
const rY = Math.sin(ay) * radius;
const rZ = Math.sin(az) * radius;
//compute cone base bounds
const cx0 = centerX - rX;
const cy0 = centerY - rY;
const cz0 = centerZ - rZ;
const cx1 = centerX + rX;
const cy1 = centerY + rY;
const cz1 = centerZ + rZ;
//combine bounds of the origin and base of the cone
result[0] = min2(originX, cx0);
result[1] = min2(originY, cy0);
result[2] = min2(originZ, cz0);
result[3] = max2(originX, cx1);
result[4] = max2(originY, cy1);
result[5] = max2(originZ, cz1);
}