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
<!DOCTYPE html>
<html>
<head>
<title>我的网页</title>
</head>
<body>
<script type="module">
const PI = 3.141592653589793238;
import init from './pkg/pi_orca.js';
import { RVOSimulator, Vector2 } from './pkg/pi_orca.js';
const temp = 200;
const num = 150;
const remove_id = num / 2;
const maxSpeed = 2.0;
let lastpotion = [];
let is_remove = false;
function setup_scenario(sim, goals, divs, agents) {
sim.set_time_step(0.25);
sim.set_agent_defaults(15.0, 10, 10.0, 10.0, 2.5, maxSpeed, Vector2.default());
for (let i = 0; i < num; i++) {
let x = Math.cos(i * 2.0 * PI / num) * temp;
let y = Math.sin(i * 2.0 * PI / num) * temp;
console.log("x: ", x);
console.log("y: ", y);
divs[i] = document.createElement("div");
divs[i].style.left = Math.floor(x + temp) + "px";
divs[i].style.top = Math.floor(y + temp) + "px";
divs[i].style.width = "4px";
divs[i].style.height = "4px";
divs[i].style.borderRadius = "50%";
divs[i].style.backgroundColor = "rgb(" + Math.floor(i / num * 256) + ", 0, 0)";
// console.log(Math.floor(1 / num * 256));
divs[i].style.position = "absolute";
document.body.appendChild(divs[i]);
agents[i] = sim.add_agent(Vector2.new(x, y));
// console.log("-sim.getAgentPosition(i): ", sim.get_agent_position(i).neg());
goals[i] = sim.get_agent_position(agents[i]).neg();
}
}
function update_visualization(sim, divs,agents) {
console.log("{}", sim.get_global_time());
for (let i = 0; i < num; i++) {
if (i == remove_id && is_remove) {
continue;
}
let position = sim.get_agent_position(agents[i]);
// lastpotion[i] = position;
// console.log("Agent: " + i + "; x: " + position.x() + "; y: " + position.y());
let x = Math.floor(position.x()) + temp;
let y = Math.floor(position.y()) + temp;
// console.log("Agent2: " + i + "; x: " + x + "; y: " + y);
divs[i].style.left = x + "px";
divs[i].style.top = y + "px";
// console.log("divs[i]: ", divs[i]);
}
// console.log(log);
}
function set_preferred_velocities(sim, goals, agents) {
/*
* Set the preferred velocity to be a vector of unit magnitude (speed) in the
* direction of the goal.
*/
for (let i = 0; i < num; i++) {
if (i == remove_id && is_remove) {
continue;
}
let goal_vector = goals[i].sub(sim.get_agent_position(agents[i]));
if (Vector2.abs_sq(goal_vector) > 1) {
goal_vector = Vector2.normalize(goal_vector);
}
sim.set_agent_pref_velocity(agents[i], goal_vector);
}
}
function reached_goal(sim, goals) {
/* Check if all agents have reached their goals. */
for (let i = 0; i < num; i++) {
if (i == remove_id && is_remove) {
continue;
}
if (Vector2.abs_sq((sim.get_agent_position(agents[i]).sub(goals[i])))
> sim.get_agent_radius(agents[i]) * sim.get_agent_radius(agents[i])) {
// if (sim.get_global_time() > 866) {
// console.log("i: ", i);
// console.log("sim.getAgentPosition(i): ", sim.get_agent_position(i).x(), " ", sim.get_agent_position(i).y());
// console.log("goals[i]: ", goals[i].x(), ", ", goals[i].y());
// console.log("sim.getAgentRadius(i): ", sim.get_agent_radius(i));
// console.log("sim.getGlobalTime(): ", sim.get_global_time());
// console.log("Vector2.absSq((sim.getAgentPosition().sub(goals[i])))`", Vector2.absSq((sim.get_agent_position().sub(goals[i]))));
// console.log("sim.getAgentPosition().sub(goals[i]))", sim.get_agent_position().sub(goals[i]));
// }
return false;
}
}
return true;
}
init().then(module => {
// 在这里调用 Rust 函数
// console.log(module)
let sim = RVOSimulator.default(2000);
let goals = [];
let divs = [];
let agents = [];
setup_scenario(sim, goals, divs, agents);
let begin = performance.now();
let r = 0;
let id = setInterval(() => {
let begin = performance.now();
// console.log("num: ", r++);
update_visualization(sim, divs, agents);
set_preferred_velocities(sim, goals, agents);
sim.do_step();
// if (performance.now() - begin > 5 * 1000) {
// if (!is_remove) {
// let r = sim.remove_agent(remove_id);
// is_remove = true;
// }
// }
console.log("======= time: ", performance.now() - begin);
// if (reached_goal(sim, goals)) {
// clearInterval(id);
// }
}, 16)
});
</script>
</body>
</html>