英文:
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:
-
Finding the arccos of the angle
θ = cos-1 [ (a. b) / (|a| |b|) ] -
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.
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 > 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 < 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 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 < HALF_FOV) {
fill(color(255, 0, 0))
}
Note that it would better to store unit direction vectors if you work with directions a lot.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论