-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (105 loc) · 3.82 KB
/
index.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
var normalize = require('vectors/normalize')(2)
,add = require('vectors/add')(2)
,sub = require('vectors/sub')(2)
,mag = require('vectors/mag')(2)
,mult = require('vectors/mult')(2)
,limit = require('vectors/limit')(2)
;
function truncate(vector, max){
var i = max / vector.length;
i = i< 1.0 ? i : 1.0;
return mult(vector, i);
};
function straight(target, position){
var target = target.slice(),
position = position.slice();
return normalize(sub(target, position));
}
function seek(target, position, current_velocity, max_velocity, slowing_radius){
//make clones of inputs to not mutate originals
var target = target.slice(),
position = position.slice(),
current_velocity = current_velocity.slice();
desired = sub(target, position);
distance = mag(desired);
desired = normalize(desired);
if (distance <= slowing_radius) {
mult(desired, (max_velocity * (distance/slowing_radius)));
} else {
mult(desired, max_velocity);
}
force = sub(desired, current_velocity);
return force;
};
function flee(target, position, current_velocity, max_velocity){
var target = target.slice(),
position = position.slice(),
current_velocity = current_velocity.slice();
desired = sub(position, target);
desired = normalize(desired);
desired = mult(desired, max_velocity);
force = sub(desired, current_velocity);
return force;
}
function setAngle(vector, value){
var len = mag(vector);
vector[0] = Math.cos(value) * len;
vector[1] = Math.sin(value) * len;
}
function wander(current_velocity, wander_distance, wander_radius, wander_angle, angle_change){
var current_velocity = current_velocity.slice();
var circleCenter = current_velocity;
circleCenter = normalize(circleCenter);
mult(circleCenter, wander_distance);
var displacement = [0, -1];
mult(displacement, wander_radius);
setAngle(displacement, wander_angle);
// wander_angle += Math.random() * angle_change - angle_change * .5;
// TODO this needs mutate and persist outside function scope;
wanderForce = add(circleCenter, displacement);
return wanderForce;
}
function evade(target, position, max_velocity, current_velocity, target_velocity){
var target = target.slice(),
position = position.slice(),
current_velocity = current_velocity.slice(),
target_velocity = target_velocity.slice();
distance = sub(target, position);
var updatesNeeded = mag(distance) / max_velocity;
var tv = mult(target_velocity.slice(), updatesNeeded);
targetFuturePosition = add(target_velocity.slice(), tv);
return flee(targetFuturePosition, position, current_velocity, max_velocity);
}
function pursuit(target, position, max_velocity, current_velocity, target_velocity){
var target = target.slice(),
position = position.slice(),
current_velocity = current_velocity.slice(),
target_velocity = target_velocity.slice(),
distance = sub(target, position),
updatesNeeded = mag(distance) / max_velocity,
tv = mult(target_velocity.slice(), updatesNeeded),
targetFuturePosition = add(target_velocity.slice(), tv);
return seek(targetFuturePosition, position, current_velocity, max_velocity, 0);
}
function avoidance(target, position, velocity,
max_avoid_ahead, max_velocity, avoidance_force){
var target = target.slice(),
position = position.slice(),
velocity = velocity.slice();
var tv = velocity.slice();
tv = normalize(tv);
tv = mult(tv, (max_avoid_ahead * mag(velocity)) / max_velocity);
var ahead = add(position.slice(), tv);
var avoidance = sub(ahead, avoidance);
avoidance = normalize(avoidance);
avoidance = mult(avoidance_force);
return avoidance;
}
module.exports = {
straight: straight,
seek: seek,
flee: flee,
wander: wander,
pursuit: pursuit,
avoidance: avoidance
};