找到两个向量之间的夹角,并检查差值是否在45度之间。

huangapple go评论68阅读模式
英文:

find the angle between two vectors and check if the difference is between 45 degrees

问题

I have an object in the middle of the screen that has a field of view of 90 degrees. It keeps rotating every frame about its origin.

There is a target object with a random (x, y) vector. I want this target object to change to red color when it is inside the field of view of the object at the center of the screen.

If you see the image above, the target is inside the field of view, then it must be changed to red color. But unfortunately, I could not make this happen.

I have tried two formulas to achieve this:

  1. Finding the arccos of the angle
    θ = cos-1 [ (a. b) / (|a| |b|) ]

  2. Finding the atan2 of the angle:
    θ = atan2(a.y - b.y, a.x - b.x)

Here is the code for number 1:

let dotProduct = p5.Vector.dot(target, centerView);
let productMag = target.mag() * centerView.mag();
let result = dotProduct / productMag;
let angleInRad = acos(result);
if (angleInRad > HALF_FOV) {
  // Change target color to red
  fill(color(255, 0, 0));
}

Here is the code for number 2:

let diff = p5.Vector.sub(target, centerView);
let theta = atan2(diff.y, diff.x);
let result = theta;
if (result < HALF_FOV && result > HALF_FOV * -1) {
  // Change target color to red
  fill(color(255, 0, 0));
}

When I run these codes, the target turns to red color, but it never changes when it is inside the field of view.

You can check and run my current code (p5.js) here.

Any ideas? Thank you.

------UPDATE-------

Thanks to @MBo, who showed me how to fix this. Here is the working example if someone wants to check.

英文:

I have an object in the middle of screen that have a field of view of 90 degrees. It keeps rotating every frame about its origin.

There is a target object with random (x,y) vector. I want this target object to change to red color when it is inside the field of view of the object at the center of screen.
找到两个向量之间的夹角,并检查差值是否在45度之间。

If you see the image above, the target is inside of field of view, then it must be changed to red color. But unfortunately I could not make this happen.

I have tried two formulas to achieve this:

1. Finding the arccos of the angle
θ = cos-1 [ (a. b) / (|a| |b|) ]

2. Finding the atang of the angle:
θ = atan2(a.y - b.y, a.x - b.x)

Here is the code of number 1:

let dotProduct = p5.Vector.dot(target, centerView)
let productMag = target.mag() * centerView.mag();
let result = dotProduct / productMag;
let angleInRad = acos(result);
if (angleInRad &gt; HALF_FOV) {
  // Change target color to red
  fill(color(255, 0, 0))
}

Here is the code of number 2:

let diff = p5.Vector.sub(target, centerView);
let theta = atan2(diff.y, diff.x)
let result = theta
if (result &lt; HALF_FOV &amp;&amp; result &gt; HALF_FOV * -1) {
  // Change target color to red
  fill(color(255, 0, 0))
}

When I run these codes, the target turns to red color, but it never changes when it is inside the field of view.

You can check and run my current code (p5.js) here.

Any ideas? Thank you

------UPDATE-------

Thanks to @MBo that show me how to fix this.
Here is the working example if someone want to check

答案1

得分: 0

Here is the translated code portion:

好的,发现错误 - 你错误地混合了点和向量。工作版本:

let targetvec = createVector(width - width / 4 - origin.x, height / 2 - origin.y)
let centerViewvec = createVector(centerView.x - origin.x, centerView.y - origin.y)

let dotProduct = p5.Vector.dot(targetvec, centerViewvec)
let productMag = targetvec.mag() * centerViewvec.mag();
let result = dotProduct / productMag;
let angleInRad = acos(result);
// console.log(angleInRad)
if (angleInRad < HALF_FOV) {
    fill(color(255, 0, 0))
}

请注意,如果你经常处理方向,最好存储单位方向向量。

I've translated the code part as requested, excluding the code itself.

英文:

OK, found error - you erroneously mixed points and vectors. Working variant :

 let targetvec = createVector(width - width / 4 - origin.x, height / 2 - origin.y)
  let centerViewvec = createVector(centerView.x - origin.x, centerView.y - origin.y)
  
   let dotProduct = p5.Vector.dot(targetvec, centerViewvec)
   let productMag = targetvec.mag() * centerViewvec.mag();
   let result = dotProduct / productMag;
   let angleInRad = acos(result);
  //  console.log(angleInRad)
   if (angleInRad &lt; HALF_FOV) {
       fill(color(255, 0, 0))
   }

Note that it would better to store unit direction vectors if you work with directions a lot.

huangapple
  • 本文由 发表于 2023年4月4日 09:20:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定