UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

618 lines • 15.3 kB
/** * Class representing a 3D vector. A 3D vector is an ordered triplet of numbers (labeled x, y, and z), which can be used to represent a number of things, such as: * * A point in 3D space. * A direction and length in 3D space. The length will always be the Euclidean distance (straight-line distance) from `(0, 0, 0)` to `(x, y, z)` and the direction is also measured from `(0, 0, 0)` towards `(x, y, z)`. * Any arbitrary ordered triplet of numbers. * There are other things a 3D vector can be used to represent, such as momentum vectors and so on, however these are the most common uses. * * Iterating through a Vector3 instance will yield its components `(x, y, z)` in the corresponding order. * * @implements Iterable<number> * * @author Alex Goldring * @copyright Company Named Limited (c) 2025 */ export class Vector3 implements Iterable<number> { /** * Dot product * @param {Vector3|Vector4} a * @param {Vector3|Vector4} b * @returns {number} */ static dot(a: Vector3 | Vector4, b: Vector3 | Vector4): number; /** * * @param {Vector3} a * @param {Vector3} b * @returns {number} */ static distance(a: Vector3, b: Vector3): number; /** * * @param {number[]} input * @param {number} [offset] * @returns {Vector3} */ static fromArray(input: number[], offset?: number): Vector3; /** * * @param {number} value * @returns {Vector3} */ static fromScalar(value: number): Vector3; /** * * @param {number} [x=0] * @param {number} [y=0] * @param {number} [z=0] */ constructor(x?: number, y?: number, z?: number); /** * Do not assign directly, use {@link set} method instead * @readonly * @type {number} */ readonly x: number; /** * Do not assign directly, use {@link set} method instead * @readonly * @type {number} */ readonly y: number; /** * Do not assign directly, use {@link set} method instead * @readonly * @type {number} */ readonly z: number; /** * Dispatches ( x, y, z, old_x, old_y, old_z ) * @readonly * @type {Signal<number,number,number,number,number,number>} */ readonly onChanged: Signal<number, number, number, number, number, number>; /** * * @param {number} v */ set 0(arg: number); /** * * @return {number} */ get 0(): number; /** * * @param {number} v */ set 1(arg: number); /** * * @return {number} */ get 1(): number; /** * * @param {number} v */ set 2(arg: number); /** * * @return {number} */ get 2(): number; /** * * @param {number[]|Float32Array} array * @param {number} [offset] * @returns {this} */ fromArray(array: number[] | Float32Array, offset?: number): this; /** * @param {number[]|Float32Array|ArrayLike} [array] * @param {number} [offset] * @returns {number[]} */ toArray(array?: number[] | Float32Array | ArrayLike<any>, offset?: number): number[]; /** * * @param {number} x * @param {number} y * @param {number} z * @returns {this} */ set(x: number, y: number, z: number): this; /** * * @param {number} v * @returns {this} */ setScalar(v: number): this; /** * * @param {number} v * @returns {this} */ setX(v: number): this; /** * * @param {number} v * @returns {this} */ setY(v: number): this; /** * * @param {number} v * @returns {this} */ setZ(v: number): this; /** * * @param {number} x * @param {number} y * @returns {this} */ setXY(x: number, y: number): this; /** * * @param {number} x * @param {number} z * @returns {this} */ setXZ(x: number, z: number): this; /** * * @param {number} y * @param {number} z * @returns {this} */ setYZ(y: number, z: number): this; /** * * @param {Vector3} a * @param {Vector3} b * @returns {this} */ addVectors(a: Vector3, b: Vector3): this; /** * * @param {Vector3} other * @returns {this} */ add(other: Vector3): this; /** * * @param {number} x * @param {number} y * @param {number} z * @returns {this} */ _add(x: number, y: number, z: number): this; /** * * @param {Vector3} a * @param {Vector3} b * @returns {this} */ subVectors(a: Vector3, b: Vector3): this; /** * * @param {Vector3} other * @returns {this} */ sub(other: Vector3): this; /** * * @param {number} x * @param {number} y * @param {number} z * @returns {this} */ _sub(x: number, y: number, z: number): this; /** * * @param {number} x * @param {number} y * @param {number} z * @returns {this} */ _multiply(x: number, y: number, z: number): this; /** * * @param {Vector3} other * @returns {this} */ multiply(other: Vector3): this; /** * * @param {Vector3} a * @param {Vector3} b * @returns {this} */ multiplyVectors(a: Vector3, b: Vector3): this; /** * Component-wise division. * @param {number} x * @param {number} y * @param {number} z * @returns {this} */ _divide(x: number, y: number, z: number): this; /** * Component-wise division. * @param {Vector3} other * @returns {this} */ divide(other: Vector3): this; /** * Component-wise division. * `this = a / b` * @param {Vector3} a * @param {Vector3} b * @return {Vector3} */ divideVectors(a: Vector3, b: Vector3): Vector3; /** * Add a scalar value to each component of the vector * @param {number} val * @returns {this} */ addScalar(val: number): this; /** * Subtract scalar value from each component of the vector * @param {number} val * @returns {this} */ subScalar(val: number): this; /** * * @returns {Vector3} */ clone(): Vector3; /** * * @param {number} val * @returns {this} */ multiplyScalar(val: number): this; /** * * @returns {boolean} */ isZero(): boolean; /** * Compute cross-product of two vectors. Result is written to this vector. * @param {Vector3} other * @returns {this} */ cross(other: Vector3): this; /** * * @param {Vector3} first * @param {Vector3} second * @returns {this} */ crossVectors(first: Vector3, second: Vector3): this; /** * * @param {number} ax * @param {number} ay * @param {number} az * @param {number} bx * @param {number} by * @param {number} bz * @returns {this} */ _crossVectors(ax: number, ay: number, az: number, bx: number, by: number, bz: number): this; /** * Sets all components of the vector to absolute value (positive) * @returns {this} */ abs(): this; /** * * @param {Vector3} v * @returns {number} */ dot(v: Vector3): number; /** * Computes length(magnitude) of the vector * @returns {number} */ length(): number; /** * Computes squared length(magnitude) of the vector. * Note: this operation is faster than computing length, because it doesn't involve computing square root * @returns {number} */ lengthSqr(): number; /** * Normalizes the vector, preserving its direction, but making magnitude equal to 1 * @returns {this} */ normalize(): this; /** * @param {number} [squared_error] * @return {boolean} */ isNormalized(squared_error?: number): boolean; /** * * @param {Vector3|{x:number,y:number,z:number}} other * @returns {this} */ copy(other: Vector3 | { x: number; y: number; z: number; }): this; /** * Negates every component of the vector making it {-x, -y, -z} * @returns {this} */ negate(): this; /** * * @param {Vector3} other * @returns {number} */ distanceTo(other: Vector3): number; /** * * @param {number} x * @param {number} y * @param {number} z * @return {number} */ _distanceTo(x: number, y: number, z: number): number; /** * Squared distance between this vector and another. It is faster than computing real distance because no SQRT operation is needed. * @param {Vector3} other * @returns {number} */ distanceSqrTo(other: Vector3): number; /** * * @param {number} x * @param {number} y * @param {number} z * @return {number} */ _distanceSqrTo(x: number, y: number, z: number): number; /** * Angle between two vectors (co-planar) in radians * @param {Vector3} other * @returns {number} */ angleTo(other: Vector3): number; /** * * @param {Quaternion} q * @returns {this} */ applyQuaternion(q: Quaternion): this; /** * Set components X,Y,Z to values 1,0 or -1 based on the sign of their original value. * @example new Vector(5,0,-10).sign().equals(new Vector(1,0,-1)); //true * @returns {this} */ sign(): this; /** * Linear interpolation * @param {Vector3} other * @param {number} fraction between 0 and 1 * @returns {this} */ lerp(other: Vector3, fraction: number): this; /** * * @param {Vector3} a * @param {Vector3} b * @param {number} fraction * @returns {this} */ lerpVectors(a: Vector3, b: Vector3, fraction: number): this; /** * * @param {Vector3} other * @param {number} fraction * @return {this} */ slerp(other: Vector3, fraction: number): this; /** * Spherical linear interpolation * @param {Vector3} a * @param {Vector3} b * @param {number} fraction * @returns {this} */ slerpVectors(a: Vector3, b: Vector3, fraction: number): this; /** * * @param {ArrayLike<number>|number[]|Float32Array} m4 * @returns {this} */ applyMatrix4(m4: ArrayLike<number> | number[] | Float32Array): this; /** * Assume current vector holds a direction, transform using a matrix to produce a new directional unit vector. * Note that the vector is normalized * @param {ArrayLike<number>|number[]|Float32Array} m4 * @returns {this} */ applyDirectionMatrix4(m4: ArrayLike<number> | number[] | Float32Array): this; /** * * @param {number[]|Float32Array} mat * @returns {this} */ applyMatrix3(mat: number[] | Float32Array): this; /** * * @param {ArrayLike<number>|number[]|Float32Array} matrix4 4x4 transform matrix * @returns {this} */ setFromMatrixPosition(matrix4: ArrayLike<number> | number[] | Float32Array): this; /** * * @param {Vector3} other * @returns {boolean} */ equals(other: Vector3): boolean; /** * * @param {number} x * @param {number} y * @param {number} z * @return {boolean} */ _equals(x: number, y: number, z: number): boolean; /** * * @param {Vector3} other * @param {number} [tolerance] * @return {boolean} */ roughlyEquals(other: Vector3, tolerance?: number): boolean; /** * * @param {number} x * @param {number} y * @param {number} z * @param {number} [tolerance] acceptable deviation * @return {boolean} */ _roughlyEquals(x: number, y: number, z: number, tolerance?: number): boolean; /** * Apply component-wise `Math.round` * @returns {this} */ round(): this; /** * Apply component-wise `Math.floor` * @returns {this} */ floor(): this; /** * Apply component-wise `Math.ceil` * @returns {this} */ ceil(): this; /** * Project this vector onto another * @param {Vector3} other * @returns {this} */ projectOntoVector3(other: Vector3): this; /** * Project first vector onto second one * @param {number} x0 * @param {number} y0 * @param {number} z0 * @param {number} x1 * @param {number} y1 * @param {number} z1 * @returns {this} */ _projectVectors(x0: number, y0: number, z0: number, x1: number, y1: number, z1: number): this; /** * Convert spherical coordinates to cartesian * * We assume Y-up coordinate system. * * @param {number} radius * @param {number} phi Also known as Azimuth * @param {number} theta Also known as Elevation * @returns {this} */ setFromSphericalCoords(radius: number, phi: number, theta: number): this; /** * * @param {function} processor * @param {*} [thisArg] * @returns {Vector3} */ process(processor: Function, thisArg?: any): Vector3; toJSON(): { x: number; y: number; z: number; }; /** * * @param {{x:number, y:number, z:number}|number} json */ fromJSON(json: { x: number; y: number; z: number; } | number): void; toString(): string; /** * * @param {BinaryBuffer} buffer * @deprecated */ toBinaryBuffer(buffer: BinaryBuffer): void; /** * * @param {BinaryBuffer} buffer * @deprecated */ fromBinaryBuffer(buffer: BinaryBuffer): void; /** * * @param {BinaryBuffer} buffer * @deprecated */ toBinaryBufferFloat32(buffer: BinaryBuffer): void; /** * * @param {BinaryBuffer} buffer * @deprecated */ fromBinaryBufferFloat32(buffer: BinaryBuffer): void; hash(): number; distanceToSquared: (other: Vector3) => number; lengthSq: () => number; /** * @readonly * @deprecated Use {@link fromArray} instead */ readonly readFromArray: (array: number[] | Float32Array, offset?: number) => this; /** * @readonly * @deprecated Use {@link toArray} instead */ readonly writeToArray: (array?: number[] | Float32Array | ArrayLike<any>, offset?: number) => number[]; /** * @readonly * @deprecated use {@link toArray} instead */ readonly asArray: (array?: number[] | Float32Array | ArrayLike<any>, offset?: number) => number[]; /** * @readonly * @type {boolean} */ readonly isVector3: boolean; /** * * @return {Generator<number>} */ [Symbol.iterator](): Generator<number>; } export namespace Vector3 { let zero: Vector3; let one: Vector3; let minus_one: Vector3; let up: Vector3; let down: Vector3; let left: Vector3; let right: Vector3; let forward: Vector3; let back: Vector3; let typeName: string; } export default Vector3; import Signal from "../events/signal/Signal.js"; //# sourceMappingURL=Vector3.d.ts.map