summaryrefslogtreecommitdiffstats
path: root/src/controller
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2018-11-04 22:02:19 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2018-11-04 22:42:25 +0100
commit6a02ea105a4e15e9f36a5b2d7d0a26169164fb1d (patch)
tree69fedbb9c6daf53bf5942beab3dbb9bc14726340 /src/controller
parent34d7b2197bc0e5f40a1e36b3fb8838abf91d4d6c (diff)
downloadrpgedit-6a02ea105a4e15e9f36a5b2d7d0a26169164fb1d.tar
rpgedit-6a02ea105a4e15e9f36a5b2d7d0a26169164fb1d.zip
Fix rounding issues in collision handling
Diffstat (limited to 'src/controller')
-rw-r--r--src/controller/gamecontext.ts49
1 files changed, 31 insertions, 18 deletions
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 {