UNPKG

@rpgjs/physic

Version:

A deterministic 2D top-down physics library for RPG, sandbox and MMO games

215 lines 5.75 kB
/** * 2D Vector class with deterministic operations * * All operations are designed to be deterministic and avoid floating-point * precision issues. Methods support both in-place mutations and immutable operations. * * @example * ```typescript * const v1 = new Vector2(1, 2); * const v2 = new Vector2(3, 4); * const sum = v1.add(v2); // Returns new vector (4, 6) * v1.addInPlace(v2); // Mutates v1 to (4, 6) * ``` */ export declare class Vector2 { /** * X component */ x: number; /** * Y component */ y: number; /** * Creates a new Vector2 * * @param x - X component (default: 0) * @param y - Y component (default: 0) */ constructor(x?: number, y?: number); /** * Creates a copy of this vector * * @returns A new Vector2 with the same values */ clone(): Vector2; /** * Sets the components of this vector * * @param x - X component * @param y - Y component * @returns This vector for chaining */ set(x: number, y: number): Vector2; /** * Copies values from another vector * * @param other - Vector to copy from * @returns This vector for chaining */ copyFrom(other: Vector2): Vector2; /** * Adds another vector to this vector (immutable) * * @param other - Vector to add * @returns New vector with the result */ add(other: Vector2): Vector2; /** * Adds another vector to this vector (in-place) * * @param other - Vector to add * @returns This vector for chaining */ addInPlace(other: Vector2): Vector2; /** * Subtracts another vector from this vector (immutable) * * @param other - Vector to subtract * @returns New vector with the result */ sub(other: Vector2): Vector2; /** * Subtracts another vector from this vector (in-place) * * @param other - Vector to subtract * @returns This vector for chaining */ subInPlace(other: Vector2): Vector2; /** * Multiplies this vector by a scalar (immutable) * * @param scalar - Scalar value * @returns New vector with the result */ mul(scalar: number): Vector2; /** * Multiplies this vector by a scalar (in-place) * * @param scalar - Scalar value * @returns This vector for chaining */ mulInPlace(scalar: number): Vector2; /** * Divides this vector by a scalar (immutable) * * @param scalar - Scalar value (must not be zero) * @returns New vector with the result */ div(scalar: number): Vector2; /** * Divides this vector by a scalar (in-place) * * @param scalar - Scalar value (must not be zero) * @returns This vector for chaining */ divInPlace(scalar: number): Vector2; /** * Calculates the dot product with another vector * * @param other - Vector to dot with * @returns Dot product value */ dot(other: Vector2): number; /** * Calculates the 2D cross product (scalar result) * * @param other - Vector to cross with * @returns Cross product value (z-component of 3D cross product) */ cross(other: Vector2): number; /** * Calculates the squared length (faster than length, avoids sqrt) * * @returns Squared length */ lengthSquared(): number; /** * Calculates the length (magnitude) of the vector * * @returns Length */ length(): number; /** * Normalizes this vector to unit length (immutable) * * @returns New normalized vector */ normalize(): Vector2; /** * Normalizes this vector to unit length (in-place) * * @returns This vector for chaining */ normalizeInPlace(): Vector2; /** * Calculates the distance to another vector * * @param other - Target vector * @returns Distance */ distanceTo(other: Vector2): number; /** * Calculates the squared distance to another vector (faster) * * @param other - Target vector * @returns Squared distance */ distanceToSquared(other: Vector2): number; /** * Rotates this vector by an angle in radians (immutable) * * @param angle - Angle in radians * @returns New rotated vector */ rotate(angle: number): Vector2; /** * Rotates this vector by an angle in radians (in-place) * * @param angle - Angle in radians * @returns This vector for chaining */ rotateInPlace(angle: number): Vector2; /** * Calculates the angle of this vector in radians * * @returns Angle in radians (range: -π to π) */ angle(): number; /** * Linearly interpolates between this vector and another * * @param other - Target vector * @param t - Interpolation factor (0 to 1) * @returns New interpolated vector */ lerp(other: Vector2, t: number): Vector2; /** * Checks if this vector equals another (with epsilon tolerance) * * @param other - Vector to compare * @param epsilon - Tolerance for comparison (default: 1e-5) * @returns True if vectors are approximately equal */ equals(other: Vector2, epsilon?: number): boolean; /** * Returns a string representation of this vector * * @returns String representation */ toString(): string; /** * Static zero vector */ static readonly ZERO: Vector2; /** * Static unit X vector */ static readonly UNIT_X: Vector2; /** * Static unit Y vector */ static readonly UNIT_Y: Vector2; } //# sourceMappingURL=Vector2.d.ts.map