-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto-agent2.html
More file actions
176 lines (149 loc) · 5.98 KB
/
auto-agent2.html
File metadata and controls
176 lines (149 loc) · 5.98 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<html>
<head>
<title>Autonomous Agent 2 - Flee, Pursue, Evade</title>
<script src="https://cdn.jsdelivr.net/npm/p5@1.5.0/lib/p5.min.js"></script>
<script>
const FRAMES_AHEAD = 10;
class Vehicle {
constructor(x, y) {
this.pos = createVector(x, y);
this.velocity = createVector(0 ,0);
this.acceleration = createVector();
this.mass = 1;
this.maxSpeed = 10;
this.radius = 16;
this.maxForce = 0.25;
}
flee(target) {
return this.seek(target).mult(-1);
}
evade(vehicle) {
let pursuit = this.pursue(vehicle);
pursuit.mult(-1);
return pursuit;
}
pursue(vehicle) {
let target = vehicle.pos.copy();
let prediction = vehicle.velocity.copy();
target.add(prediction.mult(FRAMES_AHEAD));
// for debug purpose
// fill(0, 255, 0);
// circle(target.x, target.y, 16);
return this.seek(target);
}
seek(target) {
// let desiredVelocity = p5.Vector.sub(target, this.pos);
// desiredVelocity.setMag(this.maxSpeed); // My strategy is to select a desired velocity equals to max speed (other strategies could have been selected).
// let steeringForce = p5.Vector.sub(desiredVelocity, this.velocity);
// steeringForce.limit(this.maxForce);
// this.applyForce(steeringForce);
let force = p5.Vector.sub(target, this.pos);
force.setMag(this.maxSpeed);
force.sub(this.velocity);
force.limit(this.maxForce);
return force;
}
edges() {
if (this.pos.y >= (height - this.radius)) {
this.pos.y = height - this.radius;
this.velocity.y *= -1;
} else if (this.pos.y <= 0) {
this.pos.y = this.radius;
this.velocity.y *= -1;
}
if (this.pos.x >= (width - this.radius)) {
this.pos.x = width - this.radius;
this.velocity.x *= - 1;
} else if (this.pos.x <= 0) {
this.pos.x = this.radius;
this.velocity.x *= -1;
}
}
applyForce(force) {
this.acceleration.add(force); // F = m * a => a = F / m (1) => a = F;
}
update() {
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
this.pos.add(this.velocity);
this.acceleration.set(0, 0);
}
show() {
stroke(255);
strokeWeight(2);
fill(255);
push();
translate(this.pos.x, this.pos.y);
rotate(this.velocity.heading());
triangle(-this.radius, - this.radius /2 , -this.radius, this.radius/2, this.radius, 0);
pop();
}
}
class Target extends Vehicle {
constructor(x, y) {
super(x, y);
this.velocity = p5.Vector.random2D().mult(5);
}
show() {
stroke(255);
strokeWeight(2);
fill('#F063A4');
push();
translate(this.pos.x, this.pos.y)
circle(0, 0, this.radius * 2);
pop();
}
}
class Enemy extends Vehicle {
constructor(x, y) {
super(x, y);
this.velocity = p5.Vector.random2D().mult(5);
}
show() {
stroke(255);
strokeWeight(2);
fill('#8eb03f');
push();
translate(this.pos.x, this.pos.y)
circle(0, 0, this.radius * 2);
pop();
}
}
let pursuer;
let target;
let enemies = [];
const TotalEnemies = 0;
function setup() {
createCanvas(800, 600);
pursuer = new Vehicle(100, 100);
target = new Target(random(width), random(height));
for (let i = 0; i < TotalEnemies; i++) {
enemies.push(new Enemy(random(width), random(height)));
}
}
function draw() {
background(0);
for (const enemy of enemies) {
pursuer.applyForce(pursuer.evade(enemy));
}
pursuer.applyForce(pursuer.pursue(target));
let d = p5.Vector.dist(pursuer.pos, target.pos);
if (d < pursuer.radius + target.radius) {
target = new Target(random(width), random(height));
pursuer.pos.set(width / 2, height / 2);
}
pursuer.update();
pursuer.show();
pursuer.edges();
target.edges();
target.update();
target.show();
for (const enemy of enemies) {
enemy.edges();
enemy.update();
enemy.show();
}
}
</script>
</head>
</html>