3
3
* @description Demonstration of multiple force acting on bodies
4
4
* (<a href="http://natureofcode.com">natureofcode.com</a>)
5
5
*/
6
- // Demonstration of multiple force acting on
6
+ // Demonstration of multiple force acting on
7
7
// bodies (Mover class)
8
8
// Bodies experience gravity continuously
9
9
// Bodies experience fluid resistance when in "water"
10
10
11
11
// Five moving bodies
12
- var movers = [ ] ;
12
+ let movers = [ ] ;
13
13
14
14
// Liquid
15
- var liquid ;
15
+ let liquid ;
16
16
17
17
function setup ( ) {
18
18
createCanvas ( 640 , 360 ) ;
19
19
reset ( ) ;
20
20
// Create liquid object
21
- liquid = new Liquid ( 0 , height / 2 , width , height / 2 , 0.1 ) ;
21
+ liquid = new Liquid ( 0 , height / 2 , width , height / 2 , 0.1 ) ;
22
22
}
23
23
24
24
function draw ( ) {
25
25
background ( 127 ) ;
26
-
26
+
27
27
// Draw water
28
28
liquid . display ( ) ;
29
29
30
- for ( var i = 0 ; i < movers . length ; i ++ ) {
31
-
30
+ for ( let i = 0 ; i < movers . length ; i ++ ) {
31
+
32
32
// Is the Mover in the liquid?
33
33
if ( liquid . contains ( movers [ i ] ) ) {
34
34
// Calculate drag force
35
- var dragForce = liquid . calculateDrag ( movers [ i ] ) ;
35
+ let dragForce = liquid . calculateDrag ( movers [ i ] ) ;
36
36
// Apply drag force to Mover
37
37
movers [ i ] . applyForce ( dragForce ) ;
38
38
}
39
39
40
40
// Gravity is scaled by mass here!
41
- var gravity = createVector ( 0 , 0.1 * movers [ i ] . mass ) ;
41
+ let gravity = createVector ( 0 , 0.1 * movers [ i ] . mass ) ;
42
42
// Apply gravity
43
43
movers [ i ] . applyForce ( gravity ) ;
44
-
44
+
45
45
// Update and display
46
46
movers [ i ] . update ( ) ;
47
47
movers [ i ] . display ( ) ;
48
48
movers [ i ] . checkEdges ( ) ;
49
49
}
50
-
50
+
51
51
}
52
52
53
53
@@ -57,63 +57,63 @@ function mousePressed() {
57
57
58
58
// Restart all the Mover objects randomly
59
59
function reset ( ) {
60
- for ( var i = 0 ; i < 9 ; i ++ ) {
61
- movers [ i ] = new Mover ( random ( 0.5 , 3 ) , 40 + i * 70 , 0 ) ;
60
+ for ( let i = 0 ; i < 9 ; i ++ ) {
61
+ movers [ i ] = new Mover ( random ( 0.5 , 3 ) , 40 + i * 70 , 0 ) ;
62
62
}
63
63
}
64
64
65
- var Liquid = function ( x , y , w , h , c ) {
65
+ let Liquid = function ( x , y , w , h , c ) {
66
66
this . x = x ;
67
67
this . y = y ;
68
68
this . w = w ;
69
69
this . h = h ;
70
70
this . c = c ;
71
71
} ;
72
-
72
+
73
73
// Is the Mover in the Liquid?
74
74
Liquid . prototype . contains = function ( m ) {
75
- var l = m . position ;
75
+ let l = m . position ;
76
76
return l . x > this . x && l . x < this . x + this . w &&
77
77
l . y > this . y && l . y < this . y + this . h ;
78
78
} ;
79
-
79
+
80
80
// Calculate drag force
81
81
Liquid . prototype . calculateDrag = function ( m ) {
82
82
// Magnitude is coefficient * speed squared
83
- var speed = m . velocity . mag ( ) ;
84
- var dragMagnitude = this . c * speed * speed ;
83
+ let speed = m . velocity . mag ( ) ;
84
+ let dragMagnitude = this . c * speed * speed ;
85
85
86
86
// Direction is inverse of velocity
87
- var dragForce = m . velocity . copy ( ) ;
87
+ let dragForce = m . velocity . copy ( ) ;
88
88
dragForce . mult ( - 1 ) ;
89
-
89
+
90
90
// Scale according to magnitude
91
91
// dragForce.setMag(dragMagnitude);
92
92
dragForce . normalize ( ) ;
93
93
dragForce . mult ( dragMagnitude ) ;
94
94
return dragForce ;
95
95
} ;
96
-
96
+
97
97
Liquid . prototype . display = function ( ) {
98
98
noStroke ( ) ;
99
99
fill ( 50 ) ;
100
100
rect ( this . x , this . y , this . w , this . h ) ;
101
101
} ;
102
102
103
- function Mover ( m , x , y ) {
103
+ function Mover ( m , x , y ) {
104
104
this . mass = m ;
105
- this . position = createVector ( x , y ) ;
106
- this . velocity = createVector ( 0 , 0 ) ;
107
- this . acceleration = createVector ( 0 , 0 ) ;
105
+ this . position = createVector ( x , y ) ;
106
+ this . velocity = createVector ( 0 , 0 ) ;
107
+ this . acceleration = createVector ( 0 , 0 ) ;
108
108
}
109
109
110
110
// Newton's 2nd law: F = M * A
111
111
// or A = F / M
112
112
Mover . prototype . applyForce = function ( force ) {
113
- var f = p5 . Vector . div ( force , this . mass ) ;
113
+ let f = p5 . Vector . div ( force , this . mass ) ;
114
114
this . acceleration . add ( f ) ;
115
115
} ;
116
-
116
+
117
117
Mover . prototype . update = function ( ) {
118
118
// Velocity changes according to acceleration
119
119
this . velocity . add ( this . acceleration ) ;
@@ -127,15 +127,15 @@ Mover.prototype.display = function() {
127
127
stroke ( 0 ) ;
128
128
strokeWeight ( 2 ) ;
129
129
fill ( 255 , 127 ) ;
130
- ellipse ( this . position . x , this . position . y , this . mass * 16 , this . mass * 16 ) ;
130
+ ellipse ( this . position . x , this . position . y , this . mass * 16 , this . mass * 16 ) ;
131
131
} ;
132
132
133
133
// Bounce off bottom of window
134
134
Mover . prototype . checkEdges = function ( ) {
135
- if ( this . position . y > ( height - this . mass * 8 ) ) {
135
+ if ( this . position . y > ( height - this . mass * 8 ) ) {
136
136
// A little dampening when hitting the bottom
137
137
this . velocity . y *= - 0.9 ;
138
- this . position . y = ( height - this . mass * 8 ) ;
138
+ this . position . y = ( height - this . mass * 8 ) ;
139
139
}
140
140
} ;
141
141
0 commit comments