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;
|
||||
}
|
||||
|
||||
private updateStep(): void {
|
||||
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;
|
||||
|
||||
private updateStepCollide(out: vec2, dest: vec2): boolean {
|
||||
const move = new Movement(this.entityPos, dest);
|
||||
|
||||
for (const c of this.collision) {
|
||||
if (!c.collide(dest2, move, this.collisionRadius))
|
||||
if (!c.collide(out, move, this.collisionRadius))
|
||||
continue;
|
||||
|
||||
if (!vec2.exactEquals(dest, dest2)) {
|
||||
// Ensure termination
|
||||
if (vec2.squaredDistance(this.entityPos, dest2) >= vec2.squaredDistance(this.entityPos, dest))
|
||||
if (vec2.squaredDistance(this.entityPos, out) >= vec2.squaredDistance(this.entityPos, dest))
|
||||
continue;
|
||||
|
||||
vec2.copy(dest, dest2);
|
||||
rescan = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private updateStep(): void {
|
||||
const dest = vec2.scaleAndAdd(vec2.create(), this.entityPos, this.entityMovement, this.speed);
|
||||
const newDest = vec2.create();
|
||||
|
||||
while (true) {
|
||||
if (vec2.equals(dest, this.entityPos))
|
||||
return;
|
||||
|
||||
if (!this.updateStepCollide(newDest, dest))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
vec2.copy(dest, newDest);
|
||||
}
|
||||
|
||||
vec2.copy(this.entityPos, dest);
|
||||
|
|
Reference in a new issue