From aee504be88f155adafc412936663607da72d7e4a Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 5 Nov 2018 00:51:30 +0100 Subject: Add collision radius --- src/controller/gamecontext.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/controller') diff --git a/src/controller/gamecontext.ts b/src/controller/gamecontext.ts index 295872d..8107b31 100644 --- a/src/controller/gamecontext.ts +++ b/src/controller/gamecontext.ts @@ -39,6 +39,7 @@ export class GameContext { private readonly entityPos: vec2 = vec2.clone([6, 6]); private readonly entityMovement: vec2 = vec2.create(); + private readonly collisionRadius = 7 / 16; private readonly walls: LineSegment[] = [ LineSegment.fromPoints(vec2.fromValues(1, 1), vec2.fromValues(11, 1)), @@ -99,10 +100,14 @@ export class GameContext { const move = new Movement(this.entityPos, dest); for (const w of this.walls) { - if (!w.collidesMove(dest2, move)) + if (!w.collidesMoveCircle(dest2, move, this.collisionRadius)) continue; if (!vec2.exactEquals(dest, dest2)) { + // Ensure termination + if (vec2.squaredDistance(this.entityPos, dest2) >= vec2.squaredDistance(this.entityPos, dest)) + return; + vec2.copy(dest, dest2); rescan = true; break; -- cgit v1.2.3