From f53749c770255c46364da9a08e9a90768646f153 Mon Sep 17 00:00:00 2001 From: tailuge Date: Fri, 20 Oct 2023 09:31:21 +0100 Subject: [PATCH] shorter urls --- src/container/container.ts | 1 + src/model/table.ts | 14 +++++++++++++- src/utils/utils.ts | 11 +++++++++++ src/view/cue.ts | 17 ++++++++++++----- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/container/container.ts b/src/container/container.ts index e0668f0f..e46a578f 100644 --- a/src/container/container.ts +++ b/src/container/container.ts @@ -91,6 +91,7 @@ export class Container { this.view.update(computedElapsed, this.table.cue.aim) this.table.cue.update(computedElapsed) if (!stateBefore && this.table.allStationary()) { + this.table.roundCueBallPosition() this.eventQueue.push(new StationaryEvent()) } this.sound.processOutcomes(this.table.outcome) diff --git a/src/model/table.ts b/src/model/table.ts index c02d0e6f..cfacecf1 100644 --- a/src/model/table.ts +++ b/src/model/table.ts @@ -9,7 +9,8 @@ import { TableGeometry } from "../view/tablegeometry" import { Outcome } from "./outcome" import { PocketGeometry } from "../view/pocketgeometry" import { bounceHanBlend } from "./physics/physics" -import { zero } from "../utils/utils" +import { roundVec, zero } from "../utils/utils" +import { R } from "./physics/constants" interface Pair { a: Ball @@ -212,4 +213,15 @@ export class Table { b.state = State.Stationary }) } + + roundCueBallPosition() { + const pos = roundVec(this.cueball.pos) + if ( + this.balls + .filter((b) => b !== this.cueball) + .every((b) => b.pos.distanceTo(pos) > 2 * R) + ) { + this.cueball.pos.copy(pos) + } + } } diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 8df29a70..f03178fc 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -29,9 +29,20 @@ export function round(num) { return Math.round((num + Number.EPSILON) * 1000) / 1000 } +export function round1(num) { + return Math.round((num + Number.EPSILON) * 10) / 10 +} + export function roundVec(v) { v.x = round(v.x) v.y = round(v.y) v.z = round(v.z) return v } + +export function roundVec1(v) { + v.x = round1(v.x) + v.y = round1(v.y) + v.z = round1(v.z) + return v +} diff --git a/src/view/cue.ts b/src/view/cue.ts index 3afecb4f..f16acd0e 100644 --- a/src/view/cue.ts +++ b/src/view/cue.ts @@ -1,6 +1,13 @@ import { TableGeometry } from "../view/tablegeometry" import { Table } from "../model/table" -import { upCross, unitAtAngle, norm } from "../utils/utils" +import { + upCross, + unitAtAngle, + norm, + round, + roundVec1, + round1, +} from "../utils/utils" import { AimEvent } from "../events/aimevent" import { AimInputs } from "./aiminputs" import { Ball, State } from "../model/ball" @@ -32,19 +39,19 @@ export class Cue { } rotateAim(angle) { - this.aim.angle += angle + this.aim.angle = round(this.aim.angle + angle) this.mesh.rotation.z = this.aim.angle this.helperMesh.rotation.z = this.aim.angle this.aimInputs.showOverlap() } adjustPower(delta) { - this.aim.power = Math.min(this.maxPower, this.aim.power + delta) + this.aim.power = round1(Math.min(this.maxPower, this.aim.power + delta)) this.updateAimInput() } setPower(value: number) { - this.aim.power = value * this.maxPower + this.aim.power = round1(value * this.maxPower) } hit(ball: Ball) { @@ -82,7 +89,7 @@ export class Cue { if (offset.length() > this.offCenterLimit) { offset.normalize().multiplyScalar(this.offCenterLimit) } - this.aim.offset = offset + this.aim.offset = roundVec1(offset) this.updateAimInput() }