diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-04 22:02:19 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-04 22:42:25 +0100 |
commit | 6a02ea105a4e15e9f36a5b2d7d0a26169164fb1d (patch) | |
tree | 69fedbb9c6daf53bf5942beab3dbb9bc14726340 /src/math | |
parent | 34d7b2197bc0e5f40a1e36b3fb8838abf91d4d6c (diff) | |
download | rpgedit-6a02ea105a4e15e9f36a5b2d7d0a26169164fb1d.tar rpgedit-6a02ea105a4e15e9f36a5b2d7d0a26169164fb1d.zip |
Fix rounding issues in collision handling
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/line.ts | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/src/math/line.ts b/src/math/line.ts index 98e126b..902c9b6 100644 --- a/src/math/line.ts +++ b/src/math/line.ts @@ -37,9 +37,23 @@ export class Line { const d2 = d / crossz(this.v, l2.v); return vec2.scaleAndAdd(out, l2.p, l2.v, d2); } +} + +export class Movement { + public readonly v: vec2; - public equals(l2: Line): boolean { - return vec2.equals(this.p, l2.p) && vec2.equals(this.v, l2.v); + constructor( + public readonly p1: vec2, + public readonly p2: vec2, + ) { + this.v = vec2.sub(vec2.create(), p2, p1); + } + + public intersectLine(out: vec2, l: Line): vec2 { + const vp = vec2.sub(vec2.create(), l.p, this.p1); + const d = crossz(vp, this.v); + const d2 = d / crossz(this.v, l.v); + return vec2.scaleAndAdd(out, l.p, l.v, d2); } } @@ -69,30 +83,19 @@ export class LineSegment extends Line { return (d >= 0 && d <= this.l); } - public collidesLine(l2: Line): boolean { - const x = this.intersectLine(vec2.create(), l2); - return this.collidesPoint(x); - } - - public collidesLineSegment(l2: LineSegment): boolean { - const x = this.intersectLine(vec2.create(), l2); - if (!this.collidesPoint(x)) - return false; - if (!l2.collidesPoint(x)) + public collidesMove(out: vec2, move: Movement): boolean { + if (this.distancePoint(move.p1) < 0) return false; - return true; - } + if (this.distancePoint(move.p2) >= 0) + return false; - public collidesMove(move: LineSegment): boolean { - if (!this.collidesLineSegment(move)) + const x = move.intersectLine(vec2.create(), this); + if (!this.collidesPoint(x)) return false; - const p2 = move.getP2(vec2.create()); - return this.distancePoint(p2) < 0; - } + this.projectPoint(out, move.p2); - public equals(l2: LineSegment): boolean { - return super.equals(l2) && this.l === l2.l; + return true; } } |