|
|
|
@ -31,7 +31,6 @@ export function testCollision (shape1, shape2) {
|
|
|
|
|
const vertices = [...vertex1, ...vertex2] |
|
|
|
|
|
|
|
|
|
if (testIntersection(vertices)) { |
|
|
|
|
console.debug(shape1, shape2, vertices) |
|
|
|
|
throw new Error('Intersection!') |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
@ -44,33 +43,6 @@ export function testCollision (shape1, shape2) {
|
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Computes all vertices as point-direction pairs. |
|
|
|
|
* |
|
|
|
|
* @param {Shape} shape |
|
|
|
|
* @returns {Array<Array<Vector2D>>} |
|
|
|
|
*/ |
|
|
|
|
function getVertices (shape) { |
|
|
|
|
return [ |
|
|
|
|
[ |
|
|
|
|
shape.X[0], |
|
|
|
|
subtract(shape.X[0], shape.X[1]) |
|
|
|
|
], |
|
|
|
|
[ |
|
|
|
|
shape.X[0], |
|
|
|
|
subtract(shape.X[0], shape.X[3]) |
|
|
|
|
], |
|
|
|
|
[ |
|
|
|
|
shape.X[2], |
|
|
|
|
subtract(shape.X[2], shape.X[1]) |
|
|
|
|
], |
|
|
|
|
[ |
|
|
|
|
shape.X[2], |
|
|
|
|
subtract(shape.X[2], shape.X[3]) |
|
|
|
|
] |
|
|
|
|
] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Checks for intersection of two lines given by v1 and v2, resp. v3 and v4. |
|
|
|
|
* |
|
|
|
@ -78,15 +50,41 @@ function getVertices (shape) {
|
|
|
|
|
* @returns {boolean} |
|
|
|
|
* @see {@link https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line_segment}
|
|
|
|
|
*/ |
|
|
|
|
function testIntersection ([v1, v2, v3, v4]) { |
|
|
|
|
const tdivident = determinant(subtract(v1, v3), subtract(v3, v4)) |
|
|
|
|
export function testIntersection ([v1, v2, v3, v4]) { |
|
|
|
|
const tdivisor = determinant(subtract(v1, v2), subtract(v3, v4)) |
|
|
|
|
const t = tdivident / tdivisor |
|
|
|
|
|
|
|
|
|
const udivident = determinant(subtract(v2, v1), subtract(v1, v3)) |
|
|
|
|
if (tdivisor === 0) { |
|
|
|
|
// Parallel lines never intersect
|
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const udivisor = determinant(subtract(v1, v2), subtract(v3, v4)) |
|
|
|
|
const u = udivident / udivisor |
|
|
|
|
|
|
|
|
|
console.debug(t, u) |
|
|
|
|
return (t <= 0 && t <= 1) || (u <= 0 && u <= 1) |
|
|
|
|
if (udivisor === 0) { |
|
|
|
|
// Parallel lines never intersect
|
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const tdivident = determinant(subtract(v1, v3), subtract(v3, v4)) |
|
|
|
|
const udivident = determinant(subtract(v2, v1), subtract(v1, v3)) |
|
|
|
|
// + 0 turns -0 into +0
|
|
|
|
|
const t = tdivident / tdivisor + 0 |
|
|
|
|
const u = udivident / udivisor + 0 |
|
|
|
|
|
|
|
|
|
return (t >= 0 && t <= 1) && (u >= 0 && u <= 1) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Computes all vertices as point-direction pairs. |
|
|
|
|
* |
|
|
|
|
* @param {Shape} shape |
|
|
|
|
* @returns {Array<Array<Vector2D>>} |
|
|
|
|
*/ |
|
|
|
|
function getVertices (shape) { |
|
|
|
|
return shape.X.map(function (vertex, index) { |
|
|
|
|
return [ |
|
|
|
|
vertex, |
|
|
|
|
shape.X[(index + 1) % 4] |
|
|
|
|
] |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|