controller/gamecontext: refactor collision code
This commit is contained in:
parent
f086908e08
commit
a3962f36aa
1 changed files with 21 additions and 21 deletions
|
@ -94,34 +94,34 @@ export class GameContext {
|
||||||
return diff;
|
return diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private updateStepCollide(out: vec2, dest: vec2): boolean {
|
||||||
|
const move = new Movement(this.entityPos, dest);
|
||||||
|
|
||||||
|
for (const c of this.collision) {
|
||||||
|
if (!c.collide(out, move, this.collisionRadius))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (vec2.squaredDistance(this.entityPos, out) >= vec2.squaredDistance(this.entityPos, dest))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private updateStep(): void {
|
private updateStep(): void {
|
||||||
const dest = vec2.scaleAndAdd(vec2.create(), this.entityPos, this.entityMovement, this.speed);
|
const dest = vec2.scaleAndAdd(vec2.create(), this.entityPos, this.entityMovement, this.speed);
|
||||||
const dest2 = vec2.create();
|
const newDest = vec2.create();
|
||||||
|
|
||||||
let rescan = true;
|
|
||||||
|
|
||||||
while (rescan) {
|
|
||||||
rescan = false;
|
|
||||||
|
|
||||||
|
while (true) {
|
||||||
if (vec2.equals(dest, this.entityPos))
|
if (vec2.equals(dest, this.entityPos))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const move = new Movement(this.entityPos, dest);
|
if (!this.updateStepCollide(newDest, dest))
|
||||||
|
break;
|
||||||
|
|
||||||
for (const c of this.collision) {
|
vec2.copy(dest, newDest);
|
||||||
if (!c.collide(dest2, move, this.collisionRadius))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (!vec2.exactEquals(dest, dest2)) {
|
|
||||||
// Ensure termination
|
|
||||||
if (vec2.squaredDistance(this.entityPos, dest2) >= vec2.squaredDistance(this.entityPos, dest))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
vec2.copy(dest, dest2);
|
|
||||||
rescan = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vec2.copy(this.entityPos, dest);
|
vec2.copy(this.entityPos, dest);
|
||||||
|
|
Reference in a new issue