From 6a02ea105a4e15e9f36a5b2d7d0a26169164fb1d Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 4 Nov 2018 22:02:19 +0100 Subject: Fix rounding issues in collision handling --- src/controller/gamecontext.ts | 49 +++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'src/controller/gamecontext.ts') diff --git a/src/controller/gamecontext.ts b/src/controller/gamecontext.ts index cda47a5..295872d 100644 --- a/src/controller/gamecontext.ts +++ b/src/controller/gamecontext.ts @@ -6,7 +6,7 @@ import { loadMap } from '../view/map'; import { Renderer } from '../view/renderer/renderer'; import { SpriteView } from '../view/sprite'; -import { LineSegment } from '../math/line'; +import { LineSegment, Movement } from '../math/line'; import { getJSON } from '../util'; import { vec2 } from 'gl-matrix'; @@ -40,10 +40,25 @@ export class GameContext { private readonly entityMovement: vec2 = vec2.create(); private readonly walls: LineSegment[] = [ - new LineSegment(vec2.fromValues(1, 1), vec2.fromValues(1, 0), 10), - new LineSegment(vec2.fromValues(11, 1), vec2.fromValues(0, 1), 10), - new LineSegment(vec2.fromValues(11, 11), vec2.fromValues(-1, 0), 10), - new LineSegment(vec2.fromValues(1, 11), vec2.fromValues(0, -1), 10), + LineSegment.fromPoints(vec2.fromValues(1, 1), vec2.fromValues(11, 1)), + + LineSegment.fromPoints(vec2.fromValues(11, 1), vec2.fromValues(11, 5)), + LineSegment.fromPoints(vec2.fromValues(11, 5), vec2.fromValues(12, 5)), + LineSegment.fromPoints(vec2.fromValues(12, 5), vec2.fromValues(12, 7)), + LineSegment.fromPoints(vec2.fromValues(12, 7), vec2.fromValues(11, 7)), + LineSegment.fromPoints(vec2.fromValues(11, 7), vec2.fromValues(11, 11)), + + LineSegment.fromPoints(vec2.fromValues(11, 11), vec2.fromValues(7, 11)), + LineSegment.fromPoints(vec2.fromValues(7, 11), vec2.fromValues(7, 12)), + LineSegment.fromPoints(vec2.fromValues(7, 12), vec2.fromValues(5, 12)), + LineSegment.fromPoints(vec2.fromValues(5, 12), vec2.fromValues(5, 11)), + LineSegment.fromPoints(vec2.fromValues(5, 11), vec2.fromValues(1, 11)), + + LineSegment.fromPoints(vec2.fromValues(1, 11), vec2.fromValues(1, 7)), + LineSegment.fromPoints(vec2.fromValues(1, 7), vec2.fromValues(0, 7)), + LineSegment.fromPoints(vec2.fromValues(0, 7), vec2.fromValues(0, 5)), + LineSegment.fromPoints(vec2.fromValues(0, 5), vec2.fromValues(1, 5)), + LineSegment.fromPoints(vec2.fromValues(1, 5), vec2.fromValues(1, 1)), ]; private constructor( @@ -70,34 +85,32 @@ export class GameContext { } private updateStep(): void { - let move = new LineSegment(this.entityPos, this.entityMovement, this.speed); - const p = vec2.create(); + const dest = vec2.scaleAndAdd(vec2.create(), this.entityPos, this.entityMovement, this.speed); + const dest2 = vec2.create(); let rescan = true; while (rescan) { rescan = false; - for (const w of this.walls) { - if (!w.collidesMove(move)) - continue; + if (vec2.equals(dest, this.entityPos)) + return; - move.getP2(p); - w.projectPoint(p, p); + const move = new Movement(this.entityPos, dest); - const move2 = LineSegment.fromPoints(this.entityPos, p); - if (move2.l === 0) - return; + for (const w of this.walls) { + if (!w.collidesMove(dest2, move)) + continue; - if (!move.equals(move2)) { - move = move2; + if (!vec2.exactEquals(dest, dest2)) { + vec2.copy(dest, dest2); rescan = true; break; } } } - move.getP2(this.entityPos); + vec2.copy(this.entityPos, dest); } private update(time: number): void { -- cgit v1.2.3