在p5中找到向量和点到点之间的差异

Finding difference between and of vector and point to point in p5

我有 enemies 人面向某个方向,用粗黑线表示。我想创建一个 "view cone",它会在鼠标进入时注册。

这是我的代码:

  canSeeMouse(){

    stroke("#000000");
    strokeWeight(4);

    // Get a point 60px from the enemy, in the direction it is looking
    let lookingAt = p5.Vector.fromAngle(this.a).mult(60).add(this.p);
    // Draw a line for debugging.
    line(this.p.x, this.p.y, lookingAt.x, lookingAt.y);


    // Get the angle between this position and the players
    let toPlayer = Math.atan2(player.p.y - this.p.y, player.p.x - this.p.x);

    // if the angle from this to the player minus the angle this character is looking at is more than PI/4, stop
    if(Math.abs( toPlayer - this.a )  > (Math.PI/4)){
      return false;
    }
    if(this.p.dist(player.p) > 30){
      return false;
    }
    // Draw the sight line
    strokeWeight(1);
    stroke("#ffaaaa");
    line(this.p.x, this.p.y, player.p.x, player.p.y);
  }

这是一个演示: https://codepen.io/EightArmsHQ/pen/9ef2e8266f4cfb14b4ac177c135908b4?editors=0010

我的问题是,我得到的结果不正常:

右上角和左下角是正确的,另外两个显示的视锥是错误的。

我认为这是由于一个角度介于0和Math.PI*2之间,另一个角度来自偏移量,所以尝试使用这样的东西:

if(Math.abs( toPlayer%(Math.PI*2) - this.a%(Math.PI*2) )  > (Math.PI/4)){

但不是这样...我也试过调换减法的顺序:

if(Math.abs( this.a - toPlayer )  > (Math.PI/4)){

但这也没有奏效。我不知道我做错了方程式的哪一部分。

if(Math.cos( toPlayer - this.a) >  Math.cos(Math.PI / 4))

解决所有关于顺序和从零过渡的问题