Skip to content

Commit

Permalink
added spring constant changing
Browse files Browse the repository at this point in the history
  • Loading branch information
noel-friedrich committed Apr 27, 2024
1 parent 1ea0f67 commit 60240f2
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions js/commands/simulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ terminal.addCommand("simulate", async function(args) {

let friction = 0.001

const k = 0.001 // spring stiffness
let k = 0.001 // spring stiffness

const v1 = new Vector2d(0, 0)
const v2 = new Vector2d(0, 0)
Expand Down Expand Up @@ -128,6 +128,12 @@ terminal.addCommand("simulate", async function(args) {
const newIndex = (springLengths.indexOf(springLength) ?? 0) + 1
springLength = springLengths[newIndex % springLengths.length]
}

if (event.key == "k") {
const ks = [0.1, 0.01, 0.001, 0.0001, 0.00001]
const newIndex = (ks.indexOf(k) ?? 0) + 1
k = ks[newIndex % ks.length]
}
})

let focusedBallPos = null
Expand Down Expand Up @@ -191,11 +197,13 @@ terminal.addCommand("simulate", async function(args) {
"f : change friction",
"m : change mass ratio",
"l : change spring length",
"k : change spring constant",
"space : (un)follow mass",
"",
`friction=${friction}`,
`mass_ratio=${m1}`,
`spring_length=${springLength / 10}`
`spring_length=${springLength / 10}`,
`spring_const=${k}`
]

context.font = "20px monospace"
Expand Down Expand Up @@ -281,15 +289,15 @@ terminal.addCommand("simulate", async function(args) {

drawInstructions()

drawSpring(screenPos(r1), screenPos(r2), "black")
drawBall(screenPos(r1), "#a5a1ff", 20 * Math.sqrt(m1)) // light blue
drawBall(screenPos(r2), "#ffa1a1", 20) // light red
drawBall(screenPos(R()), "#eea3ff", 5) // light violet

drawPath(p1Path, "blue")
drawPath(p2Path, "red")
drawPath(RPath, "violet")

drawSpring(screenPos(r1), screenPos(r2), "black", Math.floor(springLength / 10))
drawBall(screenPos(r1), "#a5a1ff", 20 * Math.sqrt(m1)) // light blue
drawBall(screenPos(r2), "#ffa1a1", 20) // light red
drawBall(screenPos(R()), "#eea3ff", 5) // light violet

if (focusedBallPos && currMousePos) {
if (focusedBallPos == r1) {
drawArrow(focusedBallPos, currMousePos, "blue")
Expand Down

0 comments on commit 60240f2

Please sign in to comment.