This repository was archived by the owner on Sep 22, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +21
-17
lines changed
chp03_oscillation/NOC_3_02_forces_angular_motion Expand file tree Collapse file tree 3 files changed +21
-17
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 ( ) ;
You can’t perform that action at this time.
0 commit comments