<!DOCTYPE html>
<html>
<head>
<title>我的网页</title>
</head>
<body>
<script type="module">
const PI = 3.14159265358979323846;
import init from './pkg/pi_orca.js';
import { RVOSimulator } from './pkg/pi_orca.js';
const temp = 20;
const num = 4;
const remove_id = num / 2;
const maxSpeed = 1.0;
let lastpotion = [];
let is_remove = false;
class Vector2 {
constructor(obj) {
this.x = obj.x;
this.y = obj.y;
}
static new(x, y) {
return new Vector2({ x, 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);
}
}
function setup_scenario(sim, goals, divs, agents) {
sim.set_time_step(0.25);
sim.set_rng_seed(performance.now());
sim.set_agent_defaults(15.0, 10, 5.0, 5.0, 2.5, maxSpeed, 0.0, 0.0);
for (let i = 0; i < num; i++) {
let a = Math.cos(i * 2.0 * PI / num);
let x = a * temp;
let b = Math.sin(i * 2.0 * PI / num);
let y = b * 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)";
divs[i].style.position = "absolute";
divs[i].id = "div" + i;
document.body.appendChild(divs[i]);
agents[i] = sim.add_agent(0, 10, maxSpeed);
sim.set_agent_goal(agents[i], -x, -y);
}
}
function update_visualization(sim, divs, agents) {
console.log("{}", sim.get_global_time());
let result = true;
for (let i = 0; i < num; i++) {
if (i == remove_id && is_remove) {
continue;
}
let position = sim.get_agent_position(agents[i]);
let velocity = sim.get_agent_velocity(agents[i]);
let pref_velocity = sim.get_agent_pref_velocity(agents[i]);
console.log("role", agents[i], "当前位置:", position)
console.log("role", agents[i], "当前速度:", velocity)
console.log("role", agents[i], "期望速度:", pref_velocity)
let x = Math.floor(position.x) + temp;
let y = Math.floor(position.y) + temp;
divs[i].style.left = x + "px";
divs[i].style.top = y + "px";
let goal = sim.get_agent_goal(agents[i]);
if (goal) {
let max_speed = sim.get_agent_max_speed(agents[i]);
let custom_speed = sim.get_agent_custom_speed(agents[i]);
let speed = max_speed > custom_speed ? custom_speed : max_speed;
let position = sim.get_agent_position(agents[i]);
let dist_pos = Vector2.new(goal.x, goal.y).sub(Vector2.new(position.x, position.y));
let dist_sq = Vector2.abs_sq(dist_pos);
let velocity = Vector2.normalize(dist_pos).mul_number(speed * sim.get_time_step());
if (dist_sq > Vector2.abs_sq(velocity)) {
result = false;
}
}
}
if (result) {
console.log("到达目标点");
}
return result;
}
init().then(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();
if (update_visualization(sim, divs, agents)) {
}
let res = sim.do_step();
console.log("======= time: ", performance.now() - begin, " res: " + res);
}, 16)
});
</script>
</body>
</html>