-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_star.html
325 lines (271 loc) · 10.3 KB
/
a_star.html
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<!DOCTYPE html>
<html>
<head>
<title>我的网页</title>
</head>
<body>
<div>
<script type="module">
const PI = 3.141592653589793238;
import init from './pkg/wasm_engine.js';
import { RVOSimulator, TileMap, TileObstacle, AStar, } from './pkg/wasm_engine.js';
const DefaultMaxSpeed = 2.0;
const RAND_MAX = 0x7fff;
const mapwidth = 10;
const mapHeight = 10;
const scaling = 20;
let paths = [];
let goals = [];
let divs = [];
let agents = [];
let lastpotion = [];
let velocitys = [];
class Vector2 {
constructor(obj) {
this.x = obj.x;
this.y = obj.y;
}
static new(x, y) {
return new Vector2({ x, y });
}
add(other) {
return Vector2.new(this.x + other.x, this.y + other.y);
}
sub(other) {
return Vector2.new(this.x - other.x, this.y - other.y);
}
mul_number(other) {
return Vector2.new(this.x * other, this.y * other);
}
static abs_sq(other) {
return other.x * other.x + other.y * other.y;
}
static normalize(other) {
let length = Math.sqrt(Vector2.abs_sq(other));
return Vector2.new(other.x / length, other.y / length);
}
}
class Path {
constructor(path) {
this.path = path;
this.index = 0;
this.lengths = [];
for (let i = 0; i < path.length - 1; i++) {
this.lengths.push(Vector2.abs_sq(path[i].sub(path[i + 1])));
}
}
next() {
return this.path[this.index++];
}
reset() {
this.index = 0;
}
surplusLengthSq() {
let length = 0;
for (let i = this.index; i < this.lengths.length; i++) {
let inedx = i - 1;
if (inedx < 0) {
inedx = 0;
}
length += this.lengths[inedx];
}
return length;
}
}
function update_divs(sim, divs) {
console.log("{}", sim.get_global_time());
for (let i = 0; i < agents.length; i++) {
let position = sim.get_agent_position(agents[i]);
// console.log("Agent: " + i + "; x: " + position.x + "; y: " + position.y);
let x = Math.floor(position.x);
let y = Math.floor(position.y);
// console.log("Agent2: " + i + "; x: " + x + "; y: " + y);
divs[agents[i]].style.left = x + "px";
divs[agents[i]].style.top = y + "px";
}
}
function set_preferred_velocities(sim, goals) {
let goal_vectors = [];
/*
* Set the preferred velocity to be a vector of unit magnitude (speed) in the
* direction of the goal.
*/
for (let i = 0; i < agents.length; i++) {
let goal_vector = goals[agents[i]].sub(sim.get_agent_position(agents[i]));
let temp = Vector2.abs_sq(goal_vector);
goal_vectors.push(temp);
if (temp > 1) {
goal_vector = Vector2.normalize(goal_vector);
}
sim.set_agent_pref_velocity(agents[i], new Float32Array([goal_vector.x, goal_vector.y]));
velocitys[agents[i]] = goal_vector;
}
}
function reached_goal(sim, a_star, map) {
/* Check if all agents have reached their goals. */
let reached_goal = true;
for (let i = 0; i < agents.length; ++i) {
// console.log("agent" + i + "距离目标: " + Vector2.abs_sq(sim.get_agent_position(i).sub(goals[i])));
let cur_postion = new Vector2(sim.get_agent_position(agents[i]));
if (Vector2.abs_sq(cur_postion.sub(goals[agents[i]])) > 2.0 * 2.0) {
reached_goal = false;
reset_path(sim, a_star, map, cur_postion, agents[i]);
} else {
let path = paths[agents[i]].next();
if (path) {
console.log("end_pos" + agents[i] + ": " + path.x + " " + path.y);
goals[agents[i]] = path;
reached_goal = false;
}
}
let v = sim.get_agent_velocity(agents[i]);
console.log("agent" + i + "速度: " + v.x + " " + v.y);
}
return reached_goal;
}
function reset_path(sim, a_star, map, cur_postion, i) {
let dist_sq = Vector2.abs_sq(cur_postion.sub(lastpotion[i][0]));
if (dist_sq < 0.0001) {
if (lastpotion[i][1] < 10) {
lastpotion[i][1] += 1;
} else {
lastpotion[i][1] = 0;
let start_pos = cur_postion.div(scaling);
let start_index = Math.floor(start_pos.y) * mapHeight + Math.floor(start_pos.x);
let end_index;
while (true) {
let end_pos = paths[i].next();
if (end_pos) {
end_index = end_pos.y * mapHeight + end_pos.x;
} else {
break;
}
}
// goals 中的目标点就是最后一个
if (!end_index) {
let end_pos = goals[i].div(scaling);
end_index = Math.floor(end_pos.y) * mapHeight + Math.floor(end_pos.x);
}
let res = a_star.find_path(map, 2000, start_index, end_index);
window.astar = { result: [] };
paths[i] = a_star.result(res, map);
goals[i] = paths[i].next().mul_number(scaling).add(Vector2.new(scaling / 2, scaling / 2));
}
}
lastpotion[i][0] = cur_postion;
}
function setObstacle(map, sim, ctx, min, max) {
for (let i = min.x; i < max.x; i++) {
for (let j = min.y; j < max.y; j++) {
let index = j * mapHeight + i;
map.set_obstacle(index, TileObstacle.Center);
}
}
let min1 = min.mul_number(scaling);
let max1 = max.mul_number(scaling);
sim.add_obstacle(new Float32Array([min1.x, min1.y, max1.x, min1.y, max1.x, max1.y, min1.x, max1.y]));
ctx.fillRect(min.x * scaling, min.y * scaling, (max.x - min.x) * scaling, (max.y - min.y) * scaling);
}
function find_path(a_star, map, sim, start, end) {
let start_index = start.y * mapHeight + start.x;
let end_index = end.y * mapHeight + end.x;
let res = a_star.find_path(map, 1000, start_index, end_index);
let begin = end_index;
if (!res) {
throw Error("no path!!! start: " + start_index + " end: " + end_index + "");
}
if (res != end_index) {
console.error("node number not enough!!!");
begin = res;
}
window.astar = { result: [] };
a_star.bufferData(begin, map);
return window.astar.result
}
function addAgent(a_star, map, sim, start, end,) {
let path_u32array = find_path(a_star, map, sim, start, end);
let path_arr = [];
for (let i = path_u32array.length - 2; i >= 0; i -= 2) {
let p = Vector2.new(path_u32array[i], path_u32array[i + 1]);
if (p) {
path_arr.push(p.mul_number(scaling).add(Vector2.new(scaling / 2, scaling / 2)));
} else {
break;
}
}
let path = new Path(path_arr);
let p = path.next();
console.log("addAgent, path_arr:", path_arr, p);
let id = sim.add_agent(new Float32Array([p.x / 2, p.y / 2]));
console.log("addAgent, id:", id);
agents.push(id);
goals[id] = path.next();
lastpotion[id] = [p, 0];
paths[id] = path;
let div = document.createElement("div")
div.id = id;
div.style.left = Math.floor(p.x) + "px";
div.style.top = Math.floor(p.y) + "px";
div.style.width = "4px";
div.style.height = "4px";
div.style.borderRadius = "50%";
div.style.backgroundColor = "rgb(255, 0, 0)";
div.style.position = "absolute";
document.body.appendChild(div);
divs[id] = div;
}
init().then(module => {
let map = TileMap.new(mapwidth, mapHeight);
let a_star = AStar.new(mapwidth, mapHeight, 1000);
let sim = RVOSimulator.default(2000, // note: 此参数为障碍物的最大顶点个数,实际传入的定点数必须小于等于此数
);
sim.set_time_step(0.25);
sim.set_agent_defaults(10.0, 10, 5.0, 5.0, 1.5, DefaultMaxSpeed, new Float32Array([0.0, 0.0]));
let canvas = document.createElement("canvas");
canvas.width = mapwidth * scaling;
canvas.height = mapHeight * scaling;
document.body.appendChild(canvas);
let ctx = canvas.getContext('2d');
ctx.strokeStyle = 'rgb(255,0,0)';
ctx.fillStyle = "rgba(100, 100, 100, 0.5)";
ctx.lineWidth = 1;
for (let i = 0; i <= canvas.width; i += scaling) {
ctx.beginPath();
ctx.moveTo(i, 0);
ctx.lineTo(i, canvas.height);
ctx.stroke();
}
for (let j = 0; j <= canvas.height; j += scaling) {
ctx.beginPath();
ctx.moveTo(0, j);
ctx.lineTo(canvas.width, j);
ctx.stroke();
}
setObstacle(map, sim, ctx, Vector2.new(4, 0), Vector2.new(6, 4));
setObstacle(map, sim, ctx, Vector2.new(4, 5), Vector2.new(6, 10));
addAgent(a_star, map, sim, Vector2.new(9, 9), Vector2.new(1, 9));
// addAgent(a_star, map, sim, Vector2.new(8, 9), Vector2.new(0, 9));
// addAgent(a_star, map, sim, Vector2.new(9, 8), Vector2.new(1, 8));
// addAgent(a_star, map, sim, Vector2.new(8, 8), Vector2.new(0, 8));
let r = 0;
let id = setInterval(() => {
// console.log("num: ", r++);
update_divs(sim, divs);
set_preferred_velocities(sim, goals);
sim.do_step();
// console.log("======= time: ", performance.now() - begin);
if (reached_goal(sim, a_star, map)) {
clearInterval(id);
}
}, 16)
});
</script>
</body>
<style>
canvas {
position: absolute;
left: 0px;
top: 0px;
}
</style>
</html>