@rpgjs/physic
Version:
A deterministic 2D top-down physics library for RPG, sandbox and MMO games
278 lines (277 loc) • 6.61 kB
JavaScript
import { Vector2 } from "./index2.js";
class AABB {
/**
* Creates a new AABB
*
* @param minX - Minimum X coordinate
* @param minY - Minimum Y coordinate
* @param maxX - Maximum X coordinate
* @param maxY - Maximum Y coordinate
*/
constructor(minX, minY, maxX, maxY) {
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
}
/**
* Creates an AABB from center and size
*
* @param centerX - Center X coordinate
* @param centerY - Center Y coordinate
* @param width - Width of the box
* @param height - Height of the box
* @returns New AABB
*/
static fromCenterSize(centerX, centerY, width, height) {
const halfWidth = width / 2;
const halfHeight = height / 2;
return new AABB(
centerX - halfWidth,
centerY - halfHeight,
centerX + halfWidth,
centerY + halfHeight
);
}
/**
* Creates an AABB from a center point and size
*
* @param center - Center point
* @param width - Width of the box
* @param height - Height of the box
* @returns New AABB
*/
static fromCenterSizeVector(center, width, height) {
return AABB.fromCenterSize(center.x, center.y, width, height);
}
/**
* Creates a copy of this AABB
*
* @returns New AABB with the same values
*/
clone() {
return new AABB(this.minX, this.minY, this.maxX, this.maxY);
}
/**
* Copies values from another AABB
*
* @param other - AABB to copy from
* @returns This AABB for chaining
*/
copyFrom(other) {
this.minX = other.minX;
this.minY = other.minY;
this.maxX = other.maxX;
this.maxY = other.maxY;
return this;
}
/**
* Gets the width of the AABB
*
* @returns Width
*/
getWidth() {
return this.maxX - this.minX;
}
/**
* Gets the height of the AABB
*
* @returns Height
*/
getHeight() {
return this.maxY - this.minY;
}
/**
* Gets the center point of the AABB
*
* @returns Center point
*/
getCenter() {
return new Vector2(
(this.minX + this.maxX) / 2,
(this.minY + this.maxY) / 2
);
}
/**
* Gets the center X coordinate
*
* @returns Center X
*/
getCenterX() {
return (this.minX + this.maxX) / 2;
}
/**
* Gets the center Y coordinate
*
* @returns Center Y
*/
getCenterY() {
return (this.minY + this.maxY) / 2;
}
/**
* Gets the area of the AABB
*
* @returns Area
*/
getArea() {
return this.getWidth() * this.getHeight();
}
/**
* Checks if a point is inside this AABB
*
* @param x - Point X coordinate
* @param y - Point Y coordinate
* @returns True if point is inside
*/
containsPoint(x, y) {
return x >= this.minX && x <= this.maxX && y >= this.minY && y <= this.maxY;
}
/**
* Checks if a vector point is inside this AABB
*
* @param point - Point to check
* @returns True if point is inside
*/
contains(point) {
return this.containsPoint(point.x, point.y);
}
/**
* Checks if another AABB is completely inside this AABB
*
* @param other - AABB to check
* @returns True if other AABB is inside
*/
containsAABB(other) {
return this.minX <= other.minX && this.minY <= other.minY && this.maxX >= other.maxX && this.maxY >= other.maxY;
}
/**
* Checks if this AABB intersects with another AABB
*
* @param other - AABB to check intersection with
* @returns True if AABBs intersect
*/
intersects(other) {
return !(this.maxX < other.minX || this.minX > other.maxX || this.maxY < other.minY || this.minY > other.maxY);
}
/**
* Calculates the intersection AABB with another AABB
*
* @param other - AABB to intersect with
* @returns Intersection AABB, or null if no intersection
*/
intersection(other) {
if (!this.intersects(other)) {
return null;
}
return new AABB(
Math.max(this.minX, other.minX),
Math.max(this.minY, other.minY),
Math.min(this.maxX, other.maxX),
Math.min(this.maxY, other.maxY)
);
}
/**
* Calculates the union AABB with another AABB
*
* @param other - AABB to union with
* @returns Union AABB
*/
union(other) {
return new AABB(
Math.min(this.minX, other.minX),
Math.min(this.minY, other.minY),
Math.max(this.maxX, other.maxX),
Math.max(this.maxY, other.maxY)
);
}
/**
* Expands this AABB by a given amount in all directions
*
* @param amount - Amount to expand by
* @returns New expanded AABB
*/
expand(amount) {
return new AABB(
this.minX - amount,
this.minY - amount,
this.maxX + amount,
this.maxY + amount
);
}
/**
* Expands this AABB by a given amount (in-place)
*
* @param amount - Amount to expand by
* @returns This AABB for chaining
*/
expandInPlace(amount) {
this.minX -= amount;
this.minY -= amount;
this.maxX += amount;
this.maxY += amount;
return this;
}
/**
* Translates this AABB by a given offset
*
* @param dx - X offset
* @param dy - Y offset
* @returns New translated AABB
*/
translate(dx, dy) {
return new AABB(
this.minX + dx,
this.minY + dy,
this.maxX + dx,
this.maxY + dy
);
}
/**
* Translates this AABB by a given offset (in-place)
*
* @param dx - X offset
* @param dy - Y offset
* @returns This AABB for chaining
*/
translateInPlace(dx, dy) {
this.minX += dx;
this.minY += dy;
this.maxX += dx;
this.maxY += dy;
return this;
}
/**
* Checks if this AABB equals another (with epsilon tolerance)
*
* @param other - AABB to compare
* @param epsilon - Tolerance for comparison (default: 1e-5)
* @returns True if AABBs are approximately equal
*/
equals(other, epsilon = 1e-5) {
return Math.abs(this.minX - other.minX) < epsilon && Math.abs(this.minY - other.minY) < epsilon && Math.abs(this.maxX - other.maxX) < epsilon && Math.abs(this.maxY - other.maxY) < epsilon;
}
/**
* Returns a string representation of this AABB
*
* @returns String representation
*/
toString() {
return `AABB(${this.minX}, ${this.minY}, ${this.maxX}, ${this.maxY})`;
}
/**
* Clamps a point to be inside the AABB
*
* @param point - Point to clamp
* @returns Clamped point
*/
clamp(point) {
return new Vector2(
Math.max(this.minX, Math.min(this.maxX, point.x)),
Math.max(this.minY, Math.min(this.maxY, point.y))
);
}
}
export {
AABB
};
//# sourceMappingURL=index4.js.map