diff --git a/common.ts b/common.ts index 96c6237..880b214 100644 --- a/common.ts +++ b/common.ts @@ -25,28 +25,30 @@ export function lerp(a: number, b: number, t: number) { } export function quadraticBezier(a: Point, b: Point, c: Point, res=0.05) { - const eps = 0.001; // to prevent issues with float comparaison (p <= 1) + const steps = Math.max(1, Math.ceil(1 / res)) const curve = []; - for (let p = 0; p - 1 < eps; p += res) { - const ab = lerpPoint(a, b, p); - const bc = lerpPoint(b, c, p); - const abc = lerpPoint(ab, bc, p); - curve.push(abc); + for (let i = 0; i <= steps; i++) { + const p = i / steps; + const ab = lerpPoint(a, b, p); + const bc = lerpPoint(b, c, p); + const abc = lerpPoint(ab, bc, p); + curve.push(abc); } return curve; } export function cubicBezier(a: Point, b: Point, c: Point, d: Point, res=0.05) { - const eps = 0.001; // to prevent issues with float comparaison (p <= 1) + const steps = Math.max(1, Math.ceil(1 / res)) const curve = []; - for (let p = 0; p - 1 < eps; p += res) { - const ab = lerpPoint(a, b, p); - const bc = lerpPoint(b, c, p); - const cd = lerpPoint(c, d, p); - const abc = lerpPoint(ab, bc, p); - const bcd = lerpPoint(bc, cd, p); - const abcd = lerpPoint(abc, bcd, p); - curve.push(abcd); + for (let i = 0; i <= steps; i++) { + const p = i / steps; + const ab = lerpPoint(a, b, p); + const bc = lerpPoint(b, c, p); + const cd = lerpPoint(c, d, p); + const abc = lerpPoint(ab, bc, p); + const bcd = lerpPoint(bc, cd, p); + const abcd = lerpPoint(abc, bcd, p); + curve.push(abcd); } return curve; } diff --git a/snake.ts b/snake.ts index e5d5abd..38b4fbb 100644 --- a/snake.ts +++ b/snake.ts @@ -84,7 +84,7 @@ function update(ctx: CanvasRenderingContext2D, time: number) { if (lastTime == null) lastTime = time; const dt = (time - lastTime) / 1000; // s lastTime = time; - apple = apple as Vec2d; + if (apple == null) return; if (!paused) { // update head position