UNPKG

pocket-physics

Version:

Verlet physics extracted from pocket-ces demos

49 lines (48 loc) 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.overlapAABBAABB = exports.createAABBOverlapResult = void 0; const v2_1 = require("./v2"); /** * Create a result object to use for overlap tests. */ const createAABBOverlapResult = () => { return { resolve: (0, v2_1.v2)(), hitPos: (0, v2_1.v2)(), normal: (0, v2_1.v2)() }; }; exports.createAABBOverlapResult = createAABBOverlapResult; /** * Compute the "collision manifold" for two AABB, storing the result in `result`. * Note: The `normal` is always perpendicular to an AABB edge, which may produce * some slighly weird-looking collisions. `collisionResponseAABB()` will compute * a normal using the midpoints, which looks more natural. */ const overlapAABBAABB = (center1X, center1Y, width1, height1, center2X, center2Y, width2, height2, result) => { const dx = center2X - center1X; const px = width2 / 2 + width1 / 2 - Math.abs(dx); const dy = center2Y - center1Y; const py = height2 / 2 + height1 / 2 - Math.abs(dy); if (px <= 0) return null; if (py <= 0) return null; (0, v2_1.set)(result.resolve, 0, 0); (0, v2_1.set)(result.hitPos, 0, 0); (0, v2_1.set)(result.normal, 0, 0); if (px < py) { const sx = dx < 0 ? -1 : 1; result.resolve.x = px * sx; result.normal.x = sx; // Really not sure about these values. result.hitPos.x = center1X + (width1 / 2) * sx; result.hitPos.y = center2Y; } else { const sy = dy < 0 ? -1 : 1; result.resolve.y = py * sy; result.normal.y = sy; // Really not sure about these values. result.hitPos.x = center2X; result.hitPos.y = center1Y + (height1 / 2) * sy; } return result; }; exports.overlapAABBAABB = overlapAABBAABB;