Skip to content

Commit

Permalink
aim at closest
Browse files Browse the repository at this point in the history
  • Loading branch information
tailuge committed Oct 16, 2023
1 parent 687a537 commit 2d1ac6d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/diagram.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/controller/playshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ export class PlayShot extends ControllerBase {
}

override handleStationary(_) {
const outcome = this.container.table.outcome
const table = this.container.table
const outcome = table.outcome
this.container.recoder.updateBreak(outcome)
table.cue.aimAtNext(table)
return this.container.rules.update(outcome)
}

Expand Down
20 changes: 19 additions & 1 deletion src/view/cue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TableGeometry } from "../view/tablegeometry"
import { Table } from "../model/table"
import { upCross, unitAtAngle } from "../utils/utils"
import { upCross, unitAtAngle, norm } from "../utils/utils"
import { AimEvent } from "../events/aimevent"
import { AimInputs } from "./aiminputs"
import { Ball, State } from "../model/ball"
Expand Down Expand Up @@ -55,6 +55,24 @@ export class Cue {
ball.rvel.copy(cueToSpin(aim.offset, ball.vel))
}

aimAtNext(table: Table) {
const onTable = table.balls
.filter((ball) => ball.onTable())
.filter((ball) => ball !== table.cueball)
if (onTable.length === 0) {
return
}
const distanceToCueBall = (b) => {
return table.cueball.pos.distanceTo(b.pos)
}
const closest = onTable.reduce(
(a, b) => (distanceToCueBall(a) < distanceToCueBall(b) ? a : b),
onTable[0]
)
const lineTo = norm(closest.pos.clone().sub(table.cueball.pos))
this.aim.angle = Math.atan2(lineTo.y, lineTo.x)
}

adjustSpin(delta: Vector3) {
const newOffset = this.aim.offset.clone().add(delta)
this.setSpin(newOffset)
Expand Down

0 comments on commit 2d1ac6d

Please sign in to comment.