-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforces.js
58 lines (47 loc) · 1.03 KB
/
forces.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var particle;
var gravity;
var wind;
function setup() {
createCanvas(800, 800);
particle = new Particle();
}
function draw() {
background(51);
particle.update();
particle.display();
gravity = createVector(0, 0.1);
particle.applyForce(gravity);
particle.edges();
wind = createVector(0.01, 0);
if(mouseIsPressed) {
particle.applyForce(wind);
}
}
// Object below ///////////////////////////////////////////
function Particle() {
this.pos = createVector(width/2, height/2);
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.applyForce = function(force) {
this.acc.add(force);
}
this.update = function() {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.set(0, 0);
}
this.display = function() {
fill(255);
ellipse(this.pos.x, this.pos.y, 48, 48);
}
this.edges = function() {
if (this.pos.y > height) {
this.vel.y *= -1;
this.pos.y = height;
}
if (this.pos.x > width) {
this.vel.x *= -1;
this.pos.x = width;
}
}
}