@rpgjs/physic
Version:
A deterministic 2D top-down physics library for RPG, sandbox and MMO games
279 lines (278 loc) • 6.4 kB
JavaScript
const _Vector2 = class _Vector2 {
/**
* Creates a new Vector2
*
* @param x - X component (default: 0)
* @param y - Y component (default: 0)
*/
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
/**
* Creates a copy of this vector
*
* @returns A new Vector2 with the same values
*/
clone() {
return new _Vector2(this.x, this.y);
}
/**
* Sets the components of this vector
*
* @param x - X component
* @param y - Y component
* @returns This vector for chaining
*/
set(x, y) {
this.x = x;
this.y = y;
return this;
}
/**
* Copies values from another vector
*
* @param other - Vector to copy from
* @returns This vector for chaining
*/
copyFrom(other) {
this.x = other.x;
this.y = other.y;
return this;
}
/**
* Adds another vector to this vector (immutable)
*
* @param other - Vector to add
* @returns New vector with the result
*/
add(other) {
return new _Vector2(this.x + other.x, this.y + other.y);
}
/**
* Adds another vector to this vector (in-place)
*
* @param other - Vector to add
* @returns This vector for chaining
*/
addInPlace(other) {
this.x += other.x;
this.y += other.y;
return this;
}
/**
* Subtracts another vector from this vector (immutable)
*
* @param other - Vector to subtract
* @returns New vector with the result
*/
sub(other) {
return new _Vector2(this.x - other.x, this.y - other.y);
}
/**
* Subtracts another vector from this vector (in-place)
*
* @param other - Vector to subtract
* @returns This vector for chaining
*/
subInPlace(other) {
this.x -= other.x;
this.y -= other.y;
return this;
}
/**
* Multiplies this vector by a scalar (immutable)
*
* @param scalar - Scalar value
* @returns New vector with the result
*/
mul(scalar) {
return new _Vector2(this.x * scalar, this.y * scalar);
}
/**
* Multiplies this vector by a scalar (in-place)
*
* @param scalar - Scalar value
* @returns This vector for chaining
*/
mulInPlace(scalar) {
this.x *= scalar;
this.y *= scalar;
return this;
}
/**
* Divides this vector by a scalar (immutable)
*
* @param scalar - Scalar value (must not be zero)
* @returns New vector with the result
*/
div(scalar) {
return new _Vector2(this.x / scalar, this.y / scalar);
}
/**
* Divides this vector by a scalar (in-place)
*
* @param scalar - Scalar value (must not be zero)
* @returns This vector for chaining
*/
divInPlace(scalar) {
this.x /= scalar;
this.y /= scalar;
return this;
}
/**
* Calculates the dot product with another vector
*
* @param other - Vector to dot with
* @returns Dot product value
*/
dot(other) {
return this.x * other.x + this.y * other.y;
}
/**
* 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) {
return this.x * other.y - this.y * other.x;
}
/**
* Calculates the squared length (faster than length, avoids sqrt)
*
* @returns Squared length
*/
lengthSquared() {
return this.x * this.x + this.y * this.y;
}
/**
* Calculates the length (magnitude) of the vector
*
* @returns Length
*/
length() {
return Math.sqrt(this.lengthSquared());
}
/**
* Normalizes this vector to unit length (immutable)
*
* @returns New normalized vector
*/
normalize() {
const len = this.length();
if (len === 0) {
return new _Vector2(0, 0);
}
return this.div(len);
}
/**
* Normalizes this vector to unit length (in-place)
*
* @returns This vector for chaining
*/
normalizeInPlace() {
const len = this.length();
if (len === 0) {
this.x = 0;
this.y = 0;
} else {
this.divInPlace(len);
}
return this;
}
/**
* Calculates the distance to another vector
*
* @param other - Target vector
* @returns Distance
*/
distanceTo(other) {
return this.sub(other).length();
}
/**
* Calculates the squared distance to another vector (faster)
*
* @param other - Target vector
* @returns Squared distance
*/
distanceToSquared(other) {
return this.sub(other).lengthSquared();
}
/**
* Rotates this vector by an angle in radians (immutable)
*
* @param angle - Angle in radians
* @returns New rotated vector
*/
rotate(angle) {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
return new _Vector2(
this.x * cos - this.y * sin,
this.x * sin + this.y * cos
);
}
/**
* Rotates this vector by an angle in radians (in-place)
*
* @param angle - Angle in radians
* @returns This vector for chaining
*/
rotateInPlace(angle) {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
const x = this.x * cos - this.y * sin;
const y = this.x * sin + this.y * cos;
this.x = x;
this.y = y;
return this;
}
/**
* Calculates the angle of this vector in radians
*
* @returns Angle in radians (range: -π to π)
*/
angle() {
return Math.atan2(this.y, this.x);
}
/**
* Linearly interpolates between this vector and another
*
* @param other - Target vector
* @param t - Interpolation factor (0 to 1)
* @returns New interpolated vector
*/
lerp(other, t) {
return new _Vector2(
this.x + (other.x - this.x) * t,
this.y + (other.y - this.y) * t
);
}
/**
* 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, epsilon = 1e-5) {
return Math.abs(this.x - other.x) < epsilon && Math.abs(this.y - other.y) < epsilon;
}
/**
* Returns a string representation of this vector
*
* @returns String representation
*/
toString() {
return `Vector2(${this.x}, ${this.y})`;
}
};
_Vector2.ZERO = new _Vector2(0, 0);
_Vector2.UNIT_X = new _Vector2(1, 0);
_Vector2.UNIT_Y = new _Vector2(0, 1);
let Vector2 = _Vector2;
export {
Vector2
};
//# sourceMappingURL=index2.js.map