smolworld / src /js /math.js
p3nGu1nZz's picture
✨ Refactor physics module; add math utilities and event handling classes; update state structure for 3D entities
7ffaa9e
raw
history blame contribute delete
823 Bytes
export const TWO_PI = Math.PI * 2;
export const HALF_PI = Math.PI / 2;
export const DEG_TO_RAD = Math.PI / 180;
export const RAD_TO_DEG = 180 / Math.PI;
export function lerp(start, end, factor) {
return start + (end - start) * factor;
}
export function normalizeAngle(angle) {
return angle % TWO_PI;
}
export function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
export function smoothStep(value, edge0, edge1) {
const t = clamp((value - edge0) / (edge1 - edge0), 0, 1);
return t * t * (3 - 2 * t);
}
export function distance(x1, y1, x2, y2) {
const dx = x2 - x1;
const dy = y2 - y1;
return Math.sqrt(dx * dx + dy * dy);
}
export function map(value, inMin, inMax, outMin, outMax) {
return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}