UNPKG

@fivem-ts/shared

Version:

FiveM Typescript wrapper shared part

113 lines (112 loc) 3.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Vector3 = void 0; const _1 = require("."); class Vector3 extends _1.Vector { constructor(x, y, _z) { super(x, y); this._z = _z; } get z() { return this._z; } set z(value) { this._z = value; } get Length() { return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); } get normalize() { const length = this.Length; if (length === 0) return new Vector3(0, 0, 0); const normalizedX = this.x / length; const normalizedY = this.y / length; const normalizedZ = this.z / length; return new Vector3(normalizedX, normalizedY, normalizedZ); } clone() { return new Vector3(this.x, this.y, this.z); } distanceSquared(vector) { const w = this.subtract(vector); return this.dotProduct(w); } distance(vector) { return Math.sqrt(this.distanceSquared(vector)); } crossProduct(vector) { const x = this.y * vector.z - this.z * vector.y; const y = this.z * vector.x - this.z * vector.z; const z = this.x * vector.y - this.z * vector.x; return new Vector3(x, y, z); } dotProduct(vector) { return this.x * vector.x + this.y * vector.y + this.z * vector.z; } add(vector) { if (typeof vector === 'number') { return new Vector3(this.x + vector, this.y + vector, this.z + vector); } return new Vector3(this.x + vector.x, this.y + vector.y, this.z + vector.z); } subtract(vector) { if (typeof vector === 'number') { return new Vector3(this.x - vector, this.y - vector, this.z - vector); } return new Vector3(this.x - vector.x, this.y - vector.y, this.z - vector.z); } multiply(vector) { if (typeof vector === 'number') { return new Vector3(this.x * vector, this.y * vector, this.z * vector); } return new Vector3(this.x * vector.x, this.y * vector.y, this.z * vector.z); } divide(vector) { if (typeof vector === 'number') { return new Vector3(this.x / vector, this.y / vector, this.z / vector); } return new Vector3(this.x / vector.x, this.y / vector.y, this.z / vector.z); } replace(vector) { this.x = vector.x; this.y = vector.y; this.z = vector.z; } /** * Calculates the dot product of two vectors. * * @param v1 {Vector3} - The first vector. * @param v2 {Vector3} - The second vector. * * @returns {Vector3} The dot product of the two vectors. */ static dotProduct(v1, v2) { return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; } /** * Calculates the cross product of two vectors. * * @param v1 {Vector3} - The first vector. * @param v2 {Vector3} - The second vector. * * @returns {Vector3} A new Vector3 representing the cross product of the two vectors. */ static crossProduct(v1, v2) { const x = v1.y * v2.z - v1.z * v2.y; const y = v1.z * v2.x - v1.z * v2.z; const z = v1.x * v2.y - v1.z * v2.x; return new Vector3(x, y, z); } /** * Normalizes a vector, scaling it to have a length of 1. * * @param v {Vector3} - The vector to normalize. * * @returns {Vector3} A new Vector3 representing the normalized vector. */ static normalize(v) { return v.divide(v.Length); } } exports.Vector3 = Vector3;