forked from nature-of-code/noc-examples-p5.js-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmover.js
33 lines (27 loc) · 771 Bytes
/
mover.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
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
class Mover{
constructor() {
this.position = createVector(random(width),random(height));
this.velocity = createVector();
this.acceleration = createVector();
this.topspeed = 5;
}
update() {
// Compute a vector that points from position to mouse
let mouse = createVector(mouseX,mouseY);
this.acceleration = p5.Vector.sub(mouse,this.position);
// Set magnitude of acceleration
this.acceleration.setMag(0.2);
this.velocity.add(this.acceleration);
this.velocity.limit(this.topspeed);
this.position.add(this.velocity);
}
display() {
stroke(0);
strokeWeight(2);
fill(127);
ellipse(this.position.x, this.position.y, 48, 48);
}
}