-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.js
103 lines (84 loc) · 2.19 KB
/
agent.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
class Agent {
constructor(x, y, angle, length, color, i) {
this.x = x;
this.y = y;
this.angle = angle;
this.length = length;
this.color = color;
this.targetX = x;
this.targetY = y;
this.targetAngle = angle;
this.targetLength = length;
this.targetColor = color;
this.id = i;
this.isInteraction = false
}
setTarget(x, y, angle, length) {
this.targetX = x;
this.targetY = y;
this.targetAngle = angle;
this.targetLength = length;
}
setTargetColor(x, y, angle, length, color) {
this.color = color;
this.setTarget(x, y, angle, length);
}
update(t) {
//let isTarget = true;
let isTarget = this.color > 0;
if (mouseIsPressed) {
isTarget = true;
}
if (t < 0.5) {
isTarget = true
}
if (isTarget) {
this.x += (this.targetX - this.x) * 0.001 * deltaTime;
this.y += (this.targetY - this.y) * 0.001 * deltaTime;
this.angle = lerp(this.angle, this.targetAngle, 0.05);
this.length = lerp(this.length, this.targetLength, 0.05);
} else {
this.x += map(noise(this.id * 10, 0, t), 0, 1, -1, 1) * deltaTime * 0.1;
this.y += map(noise(this.id * 10, 100, t), 0, 1, -1, 1) * deltaTime * 0.1;
}
}
display() {
push();
translate(this.x, this.y);
rotate(this.angle);
rectMode(CENTER);
fill(this.color);
rect(0, 0, this.length, 5);
pop();
}
displayShader() {
push();
translate(this.x, this.y);
rotate(this.angle);
rectMode(CENTER);
rect(0, 0, this.length, 10);
pop();
}
applyRepulsion(mouseX, mouseY) {
if (color > 0) {
let d = dist(mouseX, mouseY, this.x, this.y);
if (d < 30) {
let angle = atan2(this.y - mouseY, this.x - mouseX);
this.x += cos(angle) * 5;
this.y += sin(angle) * 5;
this.isInteraction = true;
} else {
this.isInteraction = false;
}
} else {
let d = dist(mouseX, mouseY, this.targetX, this.targetY);
if (d < 70) {
this.x = lerp(this.x, this.targetX, 0.05);
this.y = lerp(this.y, this.targetY, 0.05);
this.isInteraction = true;
} else {
this.isInteraction = false;
}
}
}
}