Skip to content
This repository was archived by the owner on Sep 22, 2024. It is now read-only.

Commit 4d9db56

Browse files
committed
minor updates to pendulum and spring examples
1 parent ba9045d commit 4d9db56

File tree

5 files changed

+23
-32
lines changed

5 files changed

+23
-32
lines changed

chp03_oscillation/NOC_3_10_PendulumExample/pendulum.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
// This constructor could be improved to allow a greater variety of pendulums
1111
class Pendulum {
1212

13-
constructor(origin_, r_) {
13+
constructor(x, y, r) {
1414
// Fill all variables
15-
this.origin = origin_.copy();
15+
this.origin = createVector(x, y);
1616
this.position = createVector();
17-
this.r = r_;
17+
this.r = r;
1818
this.angle = PI / 4;
1919

2020
this.aVelocity = 0.0;
@@ -25,13 +25,6 @@ class Pendulum {
2525
this.dragging = false;
2626
}
2727

28-
29-
go() {
30-
this.update();
31-
this.drag(); // for user interaction
32-
this.display();
33-
}
34-
3528
// Function to update position
3629
update() {
3730
// As long as we aren't dragging the pendulum, let it swing!

chp03_oscillation/NOC_3_10_PendulumExample/sketch.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,25 @@
1818

1919
// For a more substantial explanation, visit:
2020
// http://www.myphysicslab.com/pendulum1.html
21-
let p;
21+
let pendulum;
2222

2323
function setup() {
2424
createCanvas(640, 360);
2525
// Make a new Pendulum with an origin position and armlength
26-
p = new Pendulum(createVector(width / 2, 0), 175);
27-
26+
pendulum = new Pendulum(width / 2, 0, 175);
2827
}
2928

3029
function draw() {
3130
background(51);
32-
p.go();
31+
pendulum.update();
32+
pendulum.drag(); // for user interaction
33+
pendulum.display();
3334
}
3435

3536
function mousePressed() {
36-
p.clicked(mouseX, mouseY);
37+
pendulum.clicked(mouseX, mouseY);
3738
}
3839

3940
function mouseReleased() {
40-
p.stopDragging();
41+
pendulum.stopDragging();
4142
}

chp03_oscillation/NOC_3_10_PendulumExampleSimplified/pendulum.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
// This constructor could be improved to allow a greater variety of pendulums
1010
class Pendulum {
1111

12-
constructor(origin, r) {
12+
constructor(x, y, r) {
1313
// Fill all variables
14-
this.origin = origin.copy();
14+
this.origin = createVector(x, y);
1515
this.position = createVector();
1616
this.r = r;
1717
this.angle = PI / 4;
@@ -21,10 +21,6 @@ class Pendulum {
2121
this.damping = 0.995; // Arbitrary damping
2222
this.ballr = 48.0; // Arbitrary ball radius
2323
}
24-
go() {
25-
this.update();
26-
this.display();
27-
};
2824

2925
// Function to update position
3026
update() {

chp03_oscillation/NOC_3_10_PendulumExampleSimplified/sketch.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@
1818

1919
// For a more substantial explanation, visit:
2020
// http://www.myphysicslab.com/pendulum1.html
21-
let p;
21+
let pendulum;
2222

2323
function setup() {
2424
createCanvas(640, 360);
2525
// Make a new Pendulum with an origin position and armlength
26-
p = new Pendulum(createVector(width / 2, 0), 175);
26+
pendulum = new Pendulum(width / 2, 0, 175);
2727

2828
}
2929

3030
function draw() {
3131
background(51);
32-
p.go();
32+
pendulum.update();
33+
pendulum.display();
34+
3335
}

chp03_oscillation/NOC_3_11_spring/spring.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
class Spring {
99

10-
constructor(x, y, l) {
10+
constructor(x, y, length) {
1111
this.anchor = createVector(x, y);
12-
this.restLength = l;
12+
this.restLength = length;
1313
this.k = 0.2;
1414
}
1515
// Calculate and apply spring force
16-
connect(b) {
16+
connect(bob) {
1717
// Vector pointing from anchor to bob location
18-
let force = p5.Vector.sub(b.position, this.anchor);
18+
let force = p5.Vector.sub(bob.position, this.anchor);
1919
// What is distance
2020
let d = force.mag();
2121
// Stretch is difference between current distance and rest length
@@ -25,7 +25,7 @@ class Spring {
2525
// F = k * stretch
2626
force.normalize();
2727
force.mult(-1 * this.k * stretch);
28-
b.applyForce(force);
28+
bob.applyForce(force);
2929
}
3030

3131
// Constrain the distance between bob and anchor between min and max
@@ -74,8 +74,7 @@ class Spring {
7474
stroke(255);
7575
fill(127);
7676
strokeWeight(2);
77-
rectMode(CENTER);
78-
rect(this.anchor.x, this.anchor.y, 10, 10);
77+
ellipse(this.anchor.x, this.anchor.y, 10);
7978
}
8079

8180
displayLine(b) {

0 commit comments

Comments
 (0)