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

Commit ba9045d

Browse files
committed
changes to example 3.2
1 parent bb6a25c commit ba9045d

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
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();

0 commit comments

Comments
 (0)