UNPKG

@rpgjs/physic

Version:

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

86 lines (85 loc) 1.99 kB
import { Vector2 } from "./index2.js"; const EPSILON = 1e-5; function approximatelyEqual(a, b, epsilon = EPSILON) { return Math.abs(a - b) < epsilon; } function clamp(value, min, max) { return Math.max(min, Math.min(max, value)); } function lerp(a, b, t) { return a + (b - a) * t; } function distance(x1, y1, x2, y2) { const dx = x2 - x1; const dy = y2 - y1; return Math.sqrt(dx * dx + dy * dy); } function distanceSquared(x1, y1, x2, y2) { const dx = x2 - x1; const dy = y2 - y1; return dx * dx + dy * dy; } function angleBetween(x1, y1, x2, y2) { return Math.atan2(y2 - y1, x2 - x1); } function normalizeAngle(angle) { while (angle > Math.PI) { angle -= 2 * Math.PI; } while (angle < -Math.PI) { angle += 2 * Math.PI; } return angle; } function angularDistance(from, to) { let diff = to - from; while (diff > Math.PI) { diff -= 2 * Math.PI; } while (diff < -Math.PI) { diff += 2 * Math.PI; } return diff; } function projectPointOnLineSegment(point, lineStart, lineEnd) { const line = lineEnd.sub(lineStart); const pointVec = point.sub(lineStart); const lineLengthSq = line.lengthSquared(); if (lineLengthSq < EPSILON) { return lineStart.clone(); } const t = clamp(pointVec.dot(line) / lineLengthSq, 0, 1); return lineStart.add(line.mul(t)); } function closestPointOnAABB(point, aabb) { return new Vector2( clamp(point.x, aabb.minX, aabb.maxX), clamp(point.y, aabb.minY, aabb.maxY) ); } function pointInCircle(point, center, radius) { return point.distanceToSquared(center) <= radius * radius; } function degToRad(degrees) { return degrees * Math.PI / 180; } function radToDeg(radians) { return radians * 180 / Math.PI; } export { EPSILON, angleBetween, angularDistance, approximatelyEqual, clamp, closestPointOnAABB, degToRad, distance, distanceSquared, lerp, normalizeAngle, pointInCircle, projectPointOnLineSegment, radToDeg }; //# sourceMappingURL=index5.js.map