pocket-physics
Version:
Verlet physics extracted from pocket-ces demos
42 lines (41 loc) • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.rewindToCollisionPoint = void 0;
const v2_1 = require("./v2");
const segment_intersection_1 = require("./segment-intersection");
const tunnelPoint = (0, v2_1.v2)();
const offset = (0, v2_1.v2)();
const v = (0, v2_1.v2)();
const direction = (0, v2_1.v2)();
const radiusSegment = (0, v2_1.v2)();
const cposIncludingRadius = (0, v2_1.v2)();
function rewindToCollisionPoint(point3, radius3, point1, point2) {
// detect if a collision has occurred but would have been missed due to
// point3 moving beyond the edge in one time step.
// TODO: this should accept some sort of manifold or collision contact object,
// not randum args (radius, etc). Without a manifold it's imposible to know
// if the point "tunneled" due to a collision or the result of _resolving_
// a collision!
// Compute where the cpos would be if the segment actually included the
// radius.
// Without this, we would rewind to the point3 center, and the direction
// of two points colliding with each other exactly is undefined.
(0, v2_1.sub)(v, point3.cpos, point3.ppos);
(0, v2_1.normalize)(direction, v);
(0, v2_1.scale)(radiusSegment, direction, radius3);
(0, v2_1.add)(cposIncludingRadius, radiusSegment, point3.cpos);
const hasTunneled = (0, segment_intersection_1.segmentIntersection)(cposIncludingRadius, point3.ppos, point1, point2, tunnelPoint);
if (!hasTunneled)
return false;
// Translate point3 to tunnelPoint, including the radius of the point.
(0, v2_1.sub)(offset, cposIncludingRadius, tunnelPoint);
(0, v2_1.sub)(point3.cpos, point3.cpos, offset);
(0, v2_1.sub)(point3.ppos, point3.ppos, offset);
return true;
}
exports.rewindToCollisionPoint = rewindToCollisionPoint;
function debuggerIfNaN(point) {
if (isNaN(point.x) || isNaN(point.y)) {
debugger;
}
}