-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmover.js
44 lines (40 loc) · 1014 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
34
35
36
37
38
39
40
41
42
43
44
class Leader {
constructor(x, y) {
this.position = createVector(x, y)
this.velocity = createVector(1, 1)
this.home = createVector(x, y)
this.pc = 0
}
tick() {
const speed = 8
if (this.pc % 45 == 0) {
this.pc = 0
const toHome = p5.Vector.sub(this.position, this.home)
const theta = randomGaussian(toHome.heading(), Math.PI/3)
this.velocity.setHeading(theta)
this.velocity.normalize()
this.velocity.mult(speed)
}
this.position.sub(this.velocity)
this.pc += 1
this.x = this.position.x
this.y = this.position.y
}
}
class Mover {
constructor(x, y) {
this.position = createVector(x, y)
this.leader = new Leader(x, y)
}
tick() {
this.leader.tick()
const speed = 200
const toLeader = p5.Vector.sub(this.leader.position, this.position)
toLeader.div(speed)
this.position.add(toLeader)
}
direction() {
const v = p5.Vector.sub(this.position, this.leader.position)
return [v.x, v.y]
}
}