37 lines
931 B
TypeScript
37 lines
931 B
TypeScript
import { Collidable } from './collision';
|
|
import { Line, Movement, normal } from './line';
|
|
|
|
import { vec2 } from 'gl-matrix';
|
|
|
|
export class Point implements Collidable {
|
|
constructor(public readonly p: vec2) {}
|
|
|
|
public collide(out: vec2, move: Movement, r: number): boolean {
|
|
const moveLine = move.toLineSegment();
|
|
|
|
if (moveLine.projectPointDistance(this.p) < 0) return false;
|
|
|
|
const d = moveLine.distancePoint(this.p) / r;
|
|
if (Math.abs(d) >= 1) return false;
|
|
|
|
const e = Math.sqrt(1 - d * d);
|
|
|
|
const t = moveLine.getNormal(vec2.create());
|
|
vec2.scale(t, t, d);
|
|
vec2.scaleAndAdd(t, t, moveLine.v, e);
|
|
|
|
const tr = vec2.scale(vec2.create(), t, r);
|
|
|
|
const refMove = move.translate(tr);
|
|
|
|
if (vec2.sqrDist(this.p, move.src) > r * r && !refMove.passes(this.p)) return false;
|
|
|
|
normal(t, t);
|
|
|
|
const tang = new Line(this.p, t);
|
|
tang.projectPoint(out, refMove.dest);
|
|
vec2.sub(out, out, tr);
|
|
|
|
return true;
|
|
}
|
|
}
|