mirror of
https://github.com/sigmasternchen/88x31breakout
synced 2025-03-15 07:59:00 +00:00
fix: ball gets stuck in wall in some timing edge cases
This commit is contained in:
parent
fbeb9254ae
commit
e357656d00
1 changed files with 12 additions and 0 deletions
|
@ -3,12 +3,17 @@ import {ballSize} from "./geometry";
|
||||||
import {choice} from "../utils";
|
import {choice} from "../utils";
|
||||||
import {defaultBallSpeed, startAngles} from "./parameters";
|
import {defaultBallSpeed, startAngles} from "./parameters";
|
||||||
|
|
||||||
|
const sameWallCollisionTimeout: number = 50;
|
||||||
|
|
||||||
export class Ball {
|
export class Ball {
|
||||||
public position: Position;
|
public position: Position;
|
||||||
private phi: number;
|
private phi: number;
|
||||||
private speed: number;
|
private speed: number;
|
||||||
private readonly element: HTMLElement;
|
private readonly element: HTMLElement;
|
||||||
|
|
||||||
|
private lastCollisionWallAngle: number;
|
||||||
|
private lastCollisionTimestamp: number;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.position = new Position(0, 0);
|
this.position = new Position(0, 0);
|
||||||
|
|
||||||
|
@ -41,7 +46,14 @@ export class Ball {
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly collision = (wallAngle: number): void => {
|
public readonly collision = (wallAngle: number): void => {
|
||||||
|
if (wallAngle == this.lastCollisionWallAngle && (Date.now() - this.lastCollisionTimestamp) < sameWallCollisionTimeout) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.phi = 2 * wallAngle - this.phi;
|
this.phi = 2 * wallAngle - this.phi;
|
||||||
this.phi -= (0|(this.phi / (Math.PI*2))) * Math.PI * 2;
|
this.phi -= (0|(this.phi / (Math.PI*2))) * Math.PI * 2;
|
||||||
|
|
||||||
|
this.lastCollisionWallAngle = wallAngle;
|
||||||
|
this.lastCollisionTimestamp = Date.now();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue