@@ -23,8 +23,8 @@ function draw() {
23
23
background ( 51 ) ;
24
24
25
25
for ( let i = 0 ; i < num ; i ++ ) {
26
- springs [ i ] . update ( ) ;
27
- springs [ i ] . display ( ) ;
26
+ springs [ i ] . update ( ) ;
27
+ springs [ i ] . display ( ) ;
28
28
}
29
29
}
30
30
@@ -36,7 +36,7 @@ function mousePressed() {
36
36
37
37
function mouseReleased ( ) {
38
38
for ( let i = 0 ; i < num ; i ++ ) {
39
- springs [ i ] . released ( ) ;
39
+ springs [ i ] . released ( ) ;
40
40
}
41
41
}
42
42
@@ -55,94 +55,94 @@ function Spring (_x, _y, _s, _d, _m, _k_in, _others, _id) {
55
55
this . over = false ;
56
56
this . move = false ;
57
57
58
- // Spring simulation constants
58
+ // Spring simulation constants
59
59
this . mass = _m ; // Mass
60
- this . k = 0.2 ; // Spring constant
60
+ this . k = 0.2 ; // Spring constant
61
61
this . k = _k_in ;
62
62
this . damp = _d ; // Damping
63
63
this . rest_posx = _x ; // Rest position X
64
64
this . rest_posy = _y ; // Rest position Y
65
65
66
66
// Spring simulation variables
67
- //float pos = 20.0; // Position
68
- this . velx = 0.0 ; // X Velocity
69
- this . vely = 0.0 ; // Y Velocity
70
- this . accel = 0 ; // Acceleration
71
- this . force = 0 ; // Force
67
+ //float pos = 20.0; // Position
68
+ this . velx = 0.0 ; // X Velocity
69
+ this . vely = 0.0 ; // Y Velocity
70
+ this . accel = 0 ; // Acceleration
71
+ this . force = 0 ; // Force
72
72
73
73
this . friends = _others ;
74
74
this . id = _id ;
75
75
76
76
this . update = function ( ) {
77
77
78
- if ( this . move ) {
79
- this . rest_posy = mouseY ;
80
- this . rest_posx = mouseX ;
81
- }
78
+ if ( this . move ) {
79
+ this . rest_posy = mouseY ;
80
+ this . rest_posx = mouseX ;
81
+ }
82
82
83
- this . force = - this . k * ( this . y_pos - this . rest_posy ) ; // f=-ky
84
- this . accel = this . force / this . mass ; // Set the acceleration, f=ma == a=f/m
85
- this . vely = this . damp * ( this . vely + this . accel ) ; // Set the velocity
86
- this . y_pos = this . y_pos + this . vely ; // Updated position
83
+ this . force = - this . k * ( this . y_pos - this . rest_posy ) ; // f=-ky
84
+ this . accel = this . force / this . mass ; // Set the acceleration, f=ma == a=f/m
85
+ this . vely = this . damp * ( this . vely + this . accel ) ; // Set the velocity
86
+ this . y_pos = this . y_pos + this . vely ; // Updated position
87
87
88
88
89
- this . force = - this . k * ( this . x_pos - this . rest_posx ) ; // f=-ky
90
- this . accel = this . force / this . mass ; // Set the acceleration, f=ma == a=f/m
91
- this . velx = this . damp * ( this . velx + this . accel ) ; // Set the velocity
92
- this . x_pos = this . x_pos + this . velx ; // Updated position
89
+ this . force = - this . k * ( this . x_pos - this . rest_posx ) ; // f=-ky
90
+ this . accel = this . force / this . mass ; // Set the acceleration, f=ma == a=f/m
91
+ this . velx = this . damp * ( this . velx + this . accel ) ; // Set the velocity
92
+ this . x_pos = this . x_pos + this . velx ; // Updated position
93
93
94
94
95
- if ( ( this . overEvent ( ) || this . move ) && ! ( this . otherOver ( ) ) ) {
96
- this . over = true ;
97
- } else {
98
- this . over = false ;
99
- }
95
+ if ( ( this . overEvent ( ) || this . move ) && ! ( this . otherOver ( ) ) ) {
96
+ this . over = true ;
97
+ } else {
98
+ this . over = false ;
99
+ }
100
100
}
101
101
102
102
// Test to see if mouse is over this spring
103
103
this . overEvent = function ( ) {
104
- let disX = this . x_pos - mouseX ;
105
- let disY = this . y_pos - mouseY ;
106
- let dis = createVector ( disX , disY ) ;
107
- if ( dis . mag ( ) < this . size / 2 ) {
108
- return true ;
109
- } else {
110
- return false ;
111
- }
104
+ let disX = this . x_pos - mouseX ;
105
+ let disY = this . y_pos - mouseY ;
106
+ let dis = createVector ( disX , disY ) ;
107
+ if ( dis . mag ( ) < this . size / 2 ) {
108
+ return true ;
109
+ } else {
110
+ return false ;
111
+ }
112
112
}
113
113
114
114
// Make sure no other springs are active
115
115
this . otherOver = function ( ) {
116
- for ( let i = 0 ; i < num ; i ++ ) {
117
- if ( i != this . id ) {
118
- if ( this . friends [ i ] . over == true ) {
119
- return true ;
120
- }
121
- }
122
- }
123
- return false ;
116
+ for ( let i = 0 ; i < num ; i ++ ) {
117
+ if ( i != this . id ) {
118
+ if ( this . friends [ i ] . over == true ) {
119
+ return true ;
120
+ }
121
+ }
122
+ }
123
+ return false ;
124
124
}
125
125
126
126
this . display = function ( ) {
127
- if ( this . over ) {
128
- fill ( 153 ) ;
129
- } else {
130
- fill ( 255 ) ;
131
- }
132
- ellipse ( this . x_pos , this . y_pos , this . size , this . size ) ;
127
+ if ( this . over ) {
128
+ fill ( 153 ) ;
129
+ } else {
130
+ fill ( 255 ) ;
131
+ }
132
+ ellipse ( this . x_pos , this . y_pos , this . size , this . size ) ;
133
133
}
134
134
135
135
this . pressed = function ( ) {
136
- if ( this . over ) {
137
- this . move = true ;
138
- } else {
139
- this . move = false ;
140
- }
136
+ if ( this . over ) {
137
+ this . move = true ;
138
+ } else {
139
+ this . move = false ;
140
+ }
141
141
}
142
142
143
143
this . released = function ( ) {
144
- this . move = false ;
145
- this . rest_posx = this . y_pos ;
146
- this . rest_posy = this . y_pos ;
144
+ this . move = false ;
145
+ this . rest_posx = this . y_pos ;
146
+ this . rest_posy = this . y_pos ;
147
147
}
148
148
} ;
0 commit comments