This repository was archived by the owner on Sep 22, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +23
-32
lines changed
NOC_3_10_PendulumExampleSimplified Expand file tree Collapse file tree 5 files changed +23
-32
lines changed 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