Fix rounding issues in collision handling
This commit is contained in:
parent
34d7b2197b
commit
6a02ea105a
2 changed files with 56 additions and 40 deletions
|
@ -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;
|
||||
|
||||
if (vec2.equals(dest, this.entityPos))
|
||||
return;
|
||||
|
||||
const move = new Movement(this.entityPos, dest);
|
||||
|
||||
for (const w of this.walls) {
|
||||
if (!w.collidesMove(move))
|
||||
if (!w.collidesMove(dest2, move))
|
||||
continue;
|
||||
|
||||
move.getP2(p);
|
||||
w.projectPoint(p, p);
|
||||
|
||||
const move2 = LineSegment.fromPoints(this.entityPos, p);
|
||||
if (move2.l === 0)
|
||||
return;
|
||||
|
||||
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 {
|
||||
|
|
Reference in a new issue