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

Commit 125922c

Browse files
authored
Merge pull request #92 from nature-of-code/chapter3-rewrites
Changes to chapter 3 examples
2 parents bb6a25c + 4d9db56 commit 125922c

File tree

8 files changed

+44
-49
lines changed

8 files changed

+44
-49
lines changed

chp03_oscillation/NOC_3_02_forces_angular_motion/attractor.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,39 @@
22
// Daniel Shiffman
33
// http://natureofcode.com
44

5-
// An object for a draggable attractive body in our world
6-
75
class Attractor {
8-
96
constructor() {
107
this.position = createVector(width / 2, height / 2);
118
this.mass = 20;
129
this.G = 1;
1310
}
1411

15-
calculateAttraction(m) {
12+
attract(mover) {
1613
// Calculate direction of force
17-
let force = p5.Vector.sub(this.position, m.position);
14+
let force = p5.Vector.sub(this.position, mover.position);
1815
// Distance between objects
1916
let distance = force.mag();
2017
// Limiting the distance to eliminate "extreme" results for very close or very far objects
2118
distance = constrain(distance, 5, 25);
22-
// Normalize vector (distance doesn't matter here, we just want this vector for direction)
23-
force.normalize();
19+
2420
// Calculate gravitional force magnitude
25-
let strength = (this.G * this.mass * m.mass) / (distance * distance);
21+
let strength = (this.G * this.mass * mover.mass) / (distance * distance);
2622
// Get force vector --> magnitude * direction
27-
force.mult(strength);
23+
force.setMag(strength);
2824
return force;
2925
}
3026

27+
// Method to display
3128
display() {
3229
ellipseMode(CENTER);
33-
strokeWeight(2);
3430
stroke(0);
35-
fill(127);
36-
ellipse(this.position.x, this.position.y, this.mass * 2, this.mass * 2);
31+
if (this.dragging) {
32+
fill(50);
33+
} else if (this.rollover) {
34+
fill(100);
35+
} else {
36+
fill(175, 200);
37+
}
38+
ellipse(this.position.x, this.position.y, this.mass * 2);
3739
}
3840
}

chp03_oscillation/NOC_3_02_forces_angular_motion/mover.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
class Mover {
66

7-
constructor(m, x, y) {
8-
this.mass = m;
7+
constructor(x, y, mass) {
8+
this.mass = mass;
9+
this.radius = this.mass * 8;
910
this.position = createVector(x, y);
1011
this.angle = 0;
1112
this.aVelocity = 0;
@@ -36,7 +37,8 @@ class Mover {
3637
push();
3738
translate(this.position.x, this.position.y);
3839
rotate(this.angle);
39-
rect(0, 0, this.mass * 16, this.mass * 16);
40+
ellipse(0, 0, this.radius * 2);
41+
line(0, 0, this.radius, 0);
4042
pop();
4143
}
4244
}

chp03_oscillation/NOC_3_02_forces_angular_motion/sketch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function setup() {
99
createCanvas(640, 360);
1010

1111
for (let i = 0; i < 20; i++) {
12-
movers.push(new Mover(random(0.1, 2), random(width), random(height)));
12+
movers.push(new Mover(random(width), random(height), random(0.1, 2)));
1313
}
1414
attractor = new Attractor();
1515
}
@@ -20,7 +20,7 @@ function draw() {
2020
attractor.display();
2121

2222
for (let i = 0; i < movers.length; i++) {
23-
let force = attractor.calculateAttraction(movers[i]);
23+
let force = attractor.attract(movers[i]);
2424
movers[i].applyForce(force);
2525

2626
movers[i].update();

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)