pi_orca 0.4.4

A* Path Finding Algorithm
<!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)";
        // console.log(Math.floor(1 / num * 256));
        divs[i].style.position = "absolute";
        divs[i].id = "div" + i;
        document.body.appendChild(divs[i]);

        agents[i] = sim.add_agent(0, 10, maxSpeed);
        // console.log("-sim.getAgentPosition(i): ", sim.get_agent_position(i).neg());
        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]);


        // lastpotion[i] = position;
        // console.log("Agent: " + i + "; x: " + position.x() + "; y: " + position.y());
        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;
        // 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("Agent: " + i + "; x: " + position.x + "; y: " + position.y);
        let goal = sim.get_agent_goal(agents[i]);
        // console.log("goal: ", goal);
        if (goal) {
          let max_speed = sim.get_agent_max_speed(agents[i]);
          // console.log("max_speed: ", max_speed);
          let custom_speed = sim.get_agent_custom_speed(agents[i]);
          // console.log("custom_speed: ", custom_speed);
          let speed = max_speed > custom_speed ? custom_speed : max_speed;
          // console.log("speed: ", 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));
          // console.log("dist_pos: ", dist_pos);
          let dist_sq = Vector2.abs_sq(dist_pos);
          // console.log("dist_sq: ", dist_sq);
          let velocity = Vector2.normalize(dist_pos).mul_number(speed * sim.get_time_step());
          // console.log("velocity: ", velocity);
          // console.log("Vector2.abs_sq(velocity): ", Vector2.abs_sq(velocity));
          if (dist_sq > Vector2.abs_sq(velocity)) {
            // pref_velocity = velocity;
            // 还未到达目标点
            result = false;
          }
        }
      }

      if (result) {
        console.log("到达目标点");
      }
      return result;
    }

    // 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;
    //     }
    //     let position = sim.get_agent_position(i);
    //     if (Vector2.abs_sq(goals[i].sub(sim.get_agent_position(agents[i]))) > 2) {

    //       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();
        if (update_visualization(sim, divs, agents)) {
          // clearInterval(id);
        }
        let res = sim.do_step();
        console.log("======= time: ", performance.now() - begin, " res: " + res);
      }, 16)
    });


  </script>
</body>

</html>