@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
35 lines (28 loc) • 815 B
JavaScript
import { clamp } from "../../math/clamp.js";
import { v3_dot } from "./v3_dot.js";
import { v3_length } from "./v3_length.js";
/**
*
* @param {number} x0
* @param {number} y0
* @param {number} z0
* @param {number} x1
* @param {number} y1
* @param {number} z1
* @returns {number} value between -1 and 1, cosine of the angle between vectors
*/
export function v3_angle_cos_between(
x0, y0, z0,
x1, y1, z1
) {
const d = v3_dot(x0, y0, z0, x1, y1, z1);
const magnitude_0 = v3_length(x0, y0, z0);
const magnitude_1 = v3_length(x1, y1, z1);
const l = magnitude_0 * magnitude_1;
if (l === 0) {
// collective magnitude is 0, provide arbitrary angle
// avoid division by 0
return 0;
}
return clamp(d / l, -1, 1);
}