This repository was archived by the owner on Sep 22, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 8 files changed +44
-49
lines changed
NOC_3_02_forces_angular_motion
NOC_3_10_PendulumExampleSimplified Expand file tree Collapse file tree 8 files changed +44
-49
lines changed Original file line number Diff line number Diff line change 2
2
// Daniel Shiffman
3
3
// http://natureofcode.com
4
4
5
- // An object for a draggable attractive body in our world
6
-
7
5
class Attractor {
8
-
9
6
constructor ( ) {
10
7
this . position = createVector ( width / 2 , height / 2 ) ;
11
8
this . mass = 20 ;
12
9
this . G = 1 ;
13
10
}
14
11
15
- calculateAttraction ( m ) {
12
+ attract ( mover ) {
16
13
// Calculate direction of force
17
- let force = p5 . Vector . sub ( this . position , m . position ) ;
14
+ let force = p5 . Vector . sub ( this . position , mover . position ) ;
18
15
// Distance between objects
19
16
let distance = force . mag ( ) ;
20
17
// Limiting the distance to eliminate "extreme" results for very close or very far objects
21
18
distance = constrain ( distance , 5 , 25 ) ;
22
- // Normalize vector (distance doesn't matter here, we just want this vector for direction)
23
- force . normalize ( ) ;
19
+
24
20
// 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 ) ;
26
22
// Get force vector --> magnitude * direction
27
- force . mult ( strength ) ;
23
+ force . setMag ( strength ) ;
28
24
return force ;
29
25
}
30
26
27
+ // Method to display
31
28
display ( ) {
32
29
ellipseMode ( CENTER ) ;
33
- strokeWeight ( 2 ) ;
34
30
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 ) ;
37
39
}
38
40
}
Original file line number Diff line number Diff line change 4
4
5
5
class Mover {
6
6
7
- constructor ( m , x , y ) {
8
- this . mass = m ;
7
+ constructor ( x , y , mass ) {
8
+ this . mass = mass ;
9
+ this . radius = this . mass * 8 ;
9
10
this . position = createVector ( x , y ) ;
10
11
this . angle = 0 ;
11
12
this . aVelocity = 0 ;
@@ -36,7 +37,8 @@ class Mover {
36
37
push ( ) ;
37
38
translate ( this . position . x , this . position . y ) ;
38
39
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 ) ;
40
42
pop ( ) ;
41
43
}
42
44
}
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ function setup() {
9
9
createCanvas ( 640 , 360 ) ;
10
10
11
11
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 ) ) ) ;
13
13
}
14
14
attractor = new Attractor ( ) ;
15
15
}
@@ -20,7 +20,7 @@ function draw() {
20
20
attractor . display ( ) ;
21
21
22
22
for ( let i = 0 ; i < movers . length ; i ++ ) {
23
- let force = attractor . calculateAttraction ( movers [ i ] ) ;
23
+ let force = attractor . attract ( movers [ i ] ) ;
24
24
movers [ i ] . applyForce ( force ) ;
25
25
26
26
movers [ i ] . update ( ) ;
Original file line number Diff line number Diff line change 10
10
// This constructor could be improved to allow a greater variety of pendulums
11
11
class Pendulum {
12
12
13
- constructor ( origin_ , r_ ) {
13
+ constructor ( x , y , r ) {
14
14
// Fill all variables
15
- this . origin = origin_ . copy ( ) ;
15
+ this . origin = createVector ( x , y ) ;
16
16
this . position = createVector ( ) ;
17
- this . r = r_ ;
17
+ this . r = r ;
18
18
this . angle = PI / 4 ;
19
19
20
20
this . aVelocity = 0.0 ;
@@ -25,13 +25,6 @@ class Pendulum {
25
25
this . dragging = false ;
26
26
}
27
27
28
-
29
- go ( ) {
30
- this . update ( ) ;
31
- this . drag ( ) ; // for user interaction
32
- this . display ( ) ;
33
- }
34
-
35
28
// Function to update position
36
29
update ( ) {
37
30
// As long as we aren't dragging the pendulum, let it swing!
Original file line number Diff line number Diff line change 18
18
19
19
// For a more substantial explanation, visit:
20
20
// http://www.myphysicslab.com/pendulum1.html
21
- let p ;
21
+ let pendulum ;
22
22
23
23
function setup ( ) {
24
24
createCanvas ( 640 , 360 ) ;
25
25
// 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 ) ;
28
27
}
29
28
30
29
function draw ( ) {
31
30
background ( 51 ) ;
32
- p . go ( ) ;
31
+ pendulum . update ( ) ;
32
+ pendulum . drag ( ) ; // for user interaction
33
+ pendulum . display ( ) ;
33
34
}
34
35
35
36
function mousePressed ( ) {
36
- p . clicked ( mouseX , mouseY ) ;
37
+ pendulum . clicked ( mouseX , mouseY ) ;
37
38
}
38
39
39
40
function mouseReleased ( ) {
40
- p . stopDragging ( ) ;
41
+ pendulum . stopDragging ( ) ;
41
42
}
Original file line number Diff line number Diff line change 9
9
// This constructor could be improved to allow a greater variety of pendulums
10
10
class Pendulum {
11
11
12
- constructor ( origin , r ) {
12
+ constructor ( x , y , r ) {
13
13
// Fill all variables
14
- this . origin = origin . copy ( ) ;
14
+ this . origin = createVector ( x , y ) ;
15
15
this . position = createVector ( ) ;
16
16
this . r = r ;
17
17
this . angle = PI / 4 ;
@@ -21,10 +21,6 @@ class Pendulum {
21
21
this . damping = 0.995 ; // Arbitrary damping
22
22
this . ballr = 48.0 ; // Arbitrary ball radius
23
23
}
24
- go ( ) {
25
- this . update ( ) ;
26
- this . display ( ) ;
27
- } ;
28
24
29
25
// Function to update position
30
26
update ( ) {
Original file line number Diff line number Diff line change 18
18
19
19
// For a more substantial explanation, visit:
20
20
// http://www.myphysicslab.com/pendulum1.html
21
- let p ;
21
+ let pendulum ;
22
22
23
23
function setup ( ) {
24
24
createCanvas ( 640 , 360 ) ;
25
25
// 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 ) ;
27
27
28
28
}
29
29
30
30
function draw ( ) {
31
31
background ( 51 ) ;
32
- p . go ( ) ;
32
+ pendulum . update ( ) ;
33
+ pendulum . display ( ) ;
34
+
33
35
}
Original file line number Diff line number Diff line change 7
7
8
8
class Spring {
9
9
10
- constructor ( x , y , l ) {
10
+ constructor ( x , y , length ) {
11
11
this . anchor = createVector ( x , y ) ;
12
- this . restLength = l ;
12
+ this . restLength = length ;
13
13
this . k = 0.2 ;
14
14
}
15
15
// Calculate and apply spring force
16
- connect ( b ) {
16
+ connect ( bob ) {
17
17
// 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 ) ;
19
19
// What is distance
20
20
let d = force . mag ( ) ;
21
21
// Stretch is difference between current distance and rest length
@@ -25,7 +25,7 @@ class Spring {
25
25
// F = k * stretch
26
26
force . normalize ( ) ;
27
27
force . mult ( - 1 * this . k * stretch ) ;
28
- b . applyForce ( force ) ;
28
+ bob . applyForce ( force ) ;
29
29
}
30
30
31
31
// Constrain the distance between bob and anchor between min and max
@@ -74,8 +74,7 @@ class Spring {
74
74
stroke ( 255 ) ;
75
75
fill ( 127 ) ;
76
76
strokeWeight ( 2 ) ;
77
- rectMode ( CENTER ) ;
78
- rect ( this . anchor . x , this . anchor . y , 10 , 10 ) ;
77
+ ellipse ( this . anchor . x , this . anchor . y , 10 ) ;
79
78
}
80
79
81
80
displayLine ( b ) {
You can’t perform that action at this time.
0 commit comments