Skip to content

Commit

Permalink
fround for consistency across browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
tailuge committed Feb 2, 2025
1 parent 887c22a commit bc95d9a
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 36 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.

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

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/model/ball.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ export class Ball {
return this.futurePos
}

fround() {
this.pos.x = Math.fround(this.pos.x)
this.pos.y = Math.fround(this.pos.y)
this.vel.x = Math.fround(this.vel.x)
this.vel.y = Math.fround(this.vel.y)
this.rvel.x = Math.fround(this.rvel.x)
this.rvel.y = Math.fround(this.rvel.y)
this.rvel.z = Math.fround(this.rvel.z)
}

serialise() {
return {
pos: this.pos.clone(),
Expand Down
7 changes: 4 additions & 3 deletions src/model/physics/collisionthrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Vector3 } from "three"
import { Ball } from "../ball"
import { Collision } from "./collision"
import { I, m, R } from "./constants"
import { exp } from "../../utils/utils"

/**
* Based on
Expand All @@ -13,7 +14,7 @@ export class CollisionThrow {
tangentialImpulse: number

private dynamicFriction(vRel: number): number {
return 0.01 + 0.108 * Math.exp(-1.088 * vRel)
return 0.01 + 0.108 * exp(-1.088 * vRel)
}

public updateVelocities(a: Ball, b: Ball) {
Expand Down Expand Up @@ -49,9 +50,9 @@ export class CollisionThrow {
// Normal impulse (inelastic collision)
this.normalImpulse = (-(1 + e) * vRelNormalMag) / (2 / m)

// Tangential impulse (frictional constraint) reduced by 1/3 until understood
// Tangential impulse (frictional constraint) reduced by 1/4 until understood
this.tangentialImpulse =
0.3 *
0.25 *
Math.min((μ * Math.abs(this.normalImpulse)) / vRelMag, 1 / 7) *
-vRelTangential

Expand Down
35 changes: 15 additions & 20 deletions src/model/physics/mathaven.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { atan2, cos, pow, sin, sqrt } from "../../utils/utils"
import { cosθ, sinθ } from "./constants"

export class Mathaven {
Expand Down Expand Up @@ -50,14 +51,14 @@ export class Mathaven {
const v_yC = this.vy + this.ωx * R

// Update slip speeds and angles at the cushion (I)
this.s = Math.sqrt(Math.pow(v_xI, 2) + Math.pow(v_yI, 2))
this.φ = Math.atan2(v_yI, v_xI)
this.s = sqrt(pow(v_xI, 2) + pow(v_yI, 2))
this.φ = atan2(v_yI, v_xI)
if (this.φ < 0) {
this.φ += 2 * Math.PI
}
// Update slip speeds and angles at the table (C)
this. = Math.sqrt(Math.pow(v_xC, 2) + Math.pow(v_yC, 2))
this.φʹ = Math.atan2(v_yC, v_xC)
this. = sqrt(pow(v_xC, 2) + pow(v_yC, 2))
this.φʹ = atan2(v_yC, v_xC)
if (this.φʹ < 0) {
this.φʹ += 2 * Math.PI
}
Expand Down Expand Up @@ -96,14 +97,14 @@ export class Mathaven {
// Update centroid velocity components
this.vx -=
(1 / M) *
(μw * Math.cos(this.φ) +
μs * Math.cos(this.φʹ) * (sinθ + μw * Math.sin(this.φ) * cosθ)) *
(μw * cos(this.φ) +
μs * cos(this.φʹ) * (sinθ + μw * sin(this.φ) * cosθ)) *
ΔP
this.vy -=
(1 / M) *
(cosθ -
μw * sinθ * Math.sin(this.φ) +
μs * Math.sin(this.φʹ) * (sinθ + μw * Math.sin(this.φ) * cosθ)) *
μw * sinθ * sin(this.φ) +
μs * sin(this.φʹ) * (sinθ + μw * sin(this.φ) * cosθ)) *
ΔP
}

Expand All @@ -115,15 +116,15 @@ export class Mathaven {

this.ωx +=
-(5 / (2 * M * R)) *
(μw * Math.sin(this.φ) +
μs * Math.sin(this.φʹ) * (sinθ + μw * Math.sin(this.φ) * cosθ)) *
(μw * sin(this.φ) +
μs * sin(this.φʹ) * (sinθ + μw * sin(this.φ) * cosθ)) *
ΔP
this.ωy +=
-(5 / (2 * M * R)) *
(μw * Math.cos(this.φ) * sinθ -
μs * Math.cos(this.φʹ) * (sinθ + μw * Math.sin(this.φ) * cosθ)) *
(μw * cos(this.φ) * sinθ -
μs * cos(this.φʹ) * (sinθ + μw * sin(this.φ) * cosθ)) *
ΔP
this.ωz += (5 / (2 * M * R)) * (μw * Math.cos(this.φ) * cosθ) * ΔP
this.ωz += (5 / (2 * M * R)) * (μw * cos(this.φ) * cosθ) * ΔP
}

private updateWorkDone(ΔP: number): void {
Expand All @@ -133,13 +134,7 @@ export class Mathaven {
}

public solvePaper(v0: number, α: number, ω0S: number, ω0T: number) {
this.solve(
v0 * Math.cos(α),
v0 * Math.sin(α),
-ω0T * Math.sin(α),
ω0T * Math.cos(α),
ω0S
)
this.solve(v0 * cos(α), v0 * sin(α), -ω0T * sin(α), ω0T * cos(α), ω0S)
}

public solve(vx, vy, ωx, ωy, ωz): void {
Expand Down
6 changes: 3 additions & 3 deletions src/model/physics/physics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Vector3 } from "three"
import { norm, upCross, up } from "../../utils/utils"
import { norm, upCross, up, sin, cos } from "../../utils/utils"
import { muS, muC, g, m, Mz, Mxy, R, I, e } from "./constants"
import { Mathaven } from "./mathaven"
import { ee, μs, μw } from "../../diagram/constants"
Expand Down Expand Up @@ -61,8 +61,8 @@ export function rotateApplyUnrotate(theta, v, w, model) {
const epsilon = R * 0.1
const theta_a = Math.asin(epsilon / R)

const sin_a = Math.sin(theta_a)
const cos_a = Math.cos(theta_a)
const sin_a = sin(theta_a)
const cos_a = cos(theta_a)

export function s0(v, w) {
return new Vector3(
Expand Down
1 change: 1 addition & 0 deletions src/model/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class Table {
}
this.balls.forEach((a) => {
a.update(t)
a.fround()
})
}

Expand Down
24 changes: 24 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,27 @@ export function roundVec2(v) {
v.z = round2(v.z)
return v
}

export function atan2(y, x) {
return Math.fround(Math.atan2(y, x))
}

export function pow(y, x) {
return Math.fround(Math.pow(y, x))
}

export function sin(theta) {
return Math.fround(Math.sin(theta))
}

export function cos(theta) {
return Math.fround(Math.cos(theta))
}

export function sqrt(theta) {
return Math.fround(Math.sqrt(theta))
}

export function exp(theta) {
return Math.fround(Math.exp(theta))
}
11 changes: 4 additions & 7 deletions 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, norm } from "../utils/utils"
import { upCross, unitAtAngle, norm, atan2, sin } from "../utils/utils"
import { AimEvent } from "../events/aimevent"
import { AimInputs } from "./aiminputs"
import { Ball, State } from "../model/ball"
Expand All @@ -14,7 +14,7 @@ export class Cue {
helperMesh: Mesh
placerMesh: Mesh
readonly offCenterLimit = 0.3
readonly maxPower = 150 * R
readonly maxPower = 160 * R
t = 0
aimInputs: AimInputs
aim: AimEvent = new AimEvent()
Expand Down Expand Up @@ -61,7 +61,7 @@ export class Cue {
return
}
const lineTo = norm(ball.pos.clone().sub(cueball.pos))
this.aim.angle = Math.atan2(lineTo.y, lineTo.x)
this.aim.angle = atan2(lineTo.y, lineTo.x)
}

adjustSpin(delta: Vector3, table: Table) {
Expand Down Expand Up @@ -105,10 +105,7 @@ export class Cue {
this.helperMesh.rotation.z = this.aim.angle
const offset = this.spinOffset()
const swing =
(Math.sin(this.t + Math.PI / 2) - 1) *
2 *
R *
(this.aim.power / this.maxPower)
(sin(this.t + Math.PI / 2) - 1) * 2 * R * (this.aim.power / this.maxPower)
const distanceToBall = unitAtAngle(this.aim.angle)
.clone()
.multiplyScalar(swing)
Expand Down

0 comments on commit bc95d9a

Please sign in to comment.