Skip to content

Commit

Permalink
shorter urls
Browse files Browse the repository at this point in the history
  • Loading branch information
tailuge committed Oct 20, 2023
1 parent 06be86a commit f53749c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/container/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion src/model/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
11 changes: 11 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
17 changes: 12 additions & 5 deletions src/view/cue.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
}

Expand Down

0 comments on commit f53749c

Please sign in to comment.