UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

53 lines (39 loc) 1.1 kB
import { vec3 } from "gl-matrix"; import { v3_length } from "../../../../core/geom/vec3/v3_length.js"; import { v3_dot } from "../../../../core/geom/vec3/v3_dot.js"; /** * executes following formula: v1 - v0*( dot(v0,v1) ) * @param {vec3} out * @param {vec3} v0 * @param {vec3} v1 */ export function v3_scale_dot_sub_normalize(out, v0, v1) { const v0x = v0[0]; const v0y = v0[1]; const v0z = v0[2]; const v1x = v1[0]; const v1y = v1[1]; const v1z = v1[2]; // dot product const _dot = v3_dot(v0x, v0y, v0z, v1x, v1y, v1z); // scale const _x0 = v0x * _dot; const _y0 = v0y * _dot; const _z0 = v0z * _dot; // subtract const _x1 = v1x - _x0; const _z1 = v1z - _z0; const _y1 = v1y - _y0; // "safe" normalization const len = v3_length(_x1, _y1, _z1); if (len !== 0) { const m = 1 / len; out[0] = _x1 * m; out[1] = _y1 * m; out[2] = _z1 * m; } else { out[0] = _x1; out[1] = _y1; out[2] = _z1; } }