pocket-physics
Version:
Verlet physics extracted from pocket-ces demos
23 lines (22 loc) • 798 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.segmentIntersection = void 0;
const v2_1 = require("./v2");
const s1 = (0, v2_1.v2)();
const s2 = (0, v2_1.v2)();
function segmentIntersection(p0, p1, p2, p3, intersectionPoint) {
(0, v2_1.sub)(s1, p1, p0);
(0, v2_1.sub)(s2, p3, p2);
const s = (-s1.y * (p0.x - p2.x) + s1.x * (p0.y - p2.y)) /
(-s2.x * s1.y + s1.x * s2.y);
const t = (s2.x * (p0.y - p2.y) - s2.y * (p0.x - p2.x)) /
(-s2.x * s1.y + s1.x * s2.y);
if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
// Collision detected
intersectionPoint.x = p0.x + t * s1.x;
intersectionPoint.y = p0.y + t * s1.y;
return true;
}
return false;
}
exports.segmentIntersection = segmentIntersection;