-
Notifications
You must be signed in to change notification settings - Fork 0
/
dog.js
189 lines (166 loc) · 4.51 KB
/
dog.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
function Dog() {
this.pos = createVector(random(0, width), random(0, height));
this.vel = createVector(random(-2, 2), random(-2, 2))
this.acc = createVector();
this.r = 8;
this.maxSpeed = 3;
this.maxForce = 0.3;
this.layers = [
loadImage("dog/dog1.png"),
loadImage("dog/dog1.png"),
loadImage("dog/dog1.png"),
loadImage("dog/dog1.png"),
loadImage("dog/dog2.png"),
loadImage("dog/dog2.png"),
loadImage("dog/dog2.png"),
loadImage("dog/dog2.png"),
loadImage("dog/dog3.png"),
loadImage("dog/dog4.png"),
loadImage("dog/dog5.png"),
loadImage("dog/dog6.png"),
loadImage("dog/dog7.png")
]
}
Dog.prototype.update = function () {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
}
Dog.prototype.behaviors = function () {
var boundaries = this.boundaries();
var separation = this.separation(dogs);
var seek = this.seek(createVector(mouseX, mouseY));
separation.mult(0.1);
boundaries.mult(2);
this.applyForce(separation);
this.applyForce(boundaries);
this.applyForce(seek);
}
Dog.prototype.applyForce = function (f) {
this.acc.add(f);
}
Dog.prototype.align = function (dogs) {
var desired = createVector();
var count = 0;
for (var i = 0; i < dogs.length; i++) {
var v = dogs[i]
if (v !== this) {
var d = dist(this.pos.x, this.pos.y, v.pos.x, v.pos.y);
if (d < 15)
{
count++;
desired.add(v.vel.div(d));
}
}
}
if (count > 0)
{
desired.div(count);
desired.normalize();
desired.mult(this.maxSpeed);
var steer = desired
steer.setMag(this.maxSpeed);
steer.limit(this.maxForce);
return steer;
}
else return createVector(0, 0);
}
Dog.prototype.flee = function (t) {
var desired = p5.Vector.sub(t, this.pos);
var d = desired.mag();
desired.setMag(this.maxSpeed);
desired.mult(-1);
var steer = p5.Vector.sub(desired, this.vel);
steer.setMag(this.maxSpeed);
steer.limit(this.maxForce);
if (d < 70) return steer;
else return createVector(0, 0);
}
Dog.prototype.seek = function (t) {
var desired = p5.Vector.sub(t, this.pos);
var d = desired.mag();
desired.setMag(this.maxSpeed);
var steer = p5.Vector.sub(desired, this.vel);
steer.setMag(this.maxSpeed);
steer.limit(this.maxForce);
if (d < 150) return steer;
else return createVector(0, 0);
}
// Separation
// Method checks for nearby dogs and steers away
Dog.prototype.separation = function (dogs) {
var desiredSeparation = 50;
var sum = createVector();
var count = 0;
// For every boid in the system, check if it's too close
for (var i = 0; i < dogs.length; i++) {
var d =dist(this.pos.x, this.pos.y, dogs[i].pos.x, dogs[i].pos.y);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < desiredSeparation)) {
// Calculate vector pointing away from neighbor
var diff = p5.Vector.sub(this.pos, dogs[i].pos);
diff.normalize();
diff.div(d); // Weight by distance
sum.add(diff);
count++; // Keep track of how many
}
}
// Average -- divide by how many
if (count > 0) {
sum.div(count);
// Our desired vector is the average scaled to maximum speed
sum.normalize();
sum.setMag(this.maxSpeed);
// Implement Reynolds: Steering = Desired - Velocity
var steer = p5.Vector.sub(sum, this.velocity);
steer.limit(this.maxforce);
return steer;
}
else return createVector(0, 0);
};
Dog.prototype.arrive = function (t) {
var desired = p5.Vector.sub(t, this.pos);
var d = desired.mag();
var speed = this.maxSpeed;
if (d < 100) speed = map(d, 0, 100, 0, this.maxSpeed);
desired.setMag(speed)
var steer = p5.Vector.sub(desired, this.vel);
steer.limit(this.maxForce);
return steer;
}
Dog.prototype.show = function () {
push()
var theta = this.vel.heading() + PI / 2
for (var i = 0; i < this.layers.length; i++) {
push();
translate(this.pos.x, this.pos.y - i);
if (this.vel.mag() > 0.01) rotate(theta);
var layer = this.layers[i];
image(layer, -layer.width / 2, -layer.height / 2);
pop();
}
pop();
}
Dog.prototype.boundaries = function () {
var desired = null;
if (this.pos.x < 0) {
desired = createVector(this.maxSpeed, this.vel.y);
}
else if (this.pos.x > width) {
desired = createVector(-this.maxSpeed, this.vel.y);
}
if (this.pos.y < 0) {
desired = createVector(this.vel.x, this.maxSpeed);
}
else if (this.pos.y > height) {
desired = createVector(this.vel.x, -this.maxSpeed);
}
if (desired !== null) {
desired.normalize();
desired.mult(this.maxSpeed);
var steer = p5.Vector.sub(desired, this.vel);
steer.limit(this.maxForce);
return steer;
}
return createVector(0, 0);
};