Is object facing another

I'd like to derive a value for how much one object is facing another. For example if it is directly facing the value would be 1 and facing directly away -1. I have the object's global position and a quaternion for orientation. I'm using three.js

This is what I have so far, but its not giving me numbers I would expect.

Storing the object orientation as 3 part vector:

const lookVector = new THREE.Vector3(0,0,1);
const direction = lookVector.clone().applyMatrix4(torsoMesh.matrix);
this.lookVector = direction.sub(this.worldPosition);

Then using this to derive an angle to another point:

angleTo(pointOfInterest){
    const vectorToPOI =  pointOfInterest.sub(this.worldPosition);
    const angle = this.lookVector.angleTo(vectorToPOI);
    return angle;
}

Answers


one way to achieve this is using dot product. It is very simple, you just normalize both vectors and multiply each component respectively, then sum them up. I won't go into more detail here, because there is loads of resources explaining this on the internet. But here is an example of how to do it in three.js:

this.lookVector.normalize();
pointOfInterest.normalize();
var angleValue = this.lookVector.dot(pointOfInterest);

this will give exactly what you asked for (just remember that by calling normalize() on a vector, you are modifying it).

angleValue is actually just the cosine of the angle between the two vectors, so another way would just be to do:

.
.
return Math.cos(angle);

hope I understood you question correctly.


I know the mesh objects keep rotation values in radians. Maybe you can have a "parent mesh" so to speak and its its rotation values to determine this.


Need Your Help

'Element is no longer attached to the DOM' after drop down selection refreshes the page

python unit-testing selenium

I'm writing a Selenium unittest which selects an option from a dropdown menu triggering a page refresh (not a full refresh, but JS changes the DOM) to show data based on that selection. My testcase...

Has anyone ever seen this SQL Server Error? Failed Assertion = 'm_iteratorInSafeState'. I can't find anything online to help

sql-server sql-server-2005

We have a client running our .NET application which connects to SQL Server 2005 on the backend. A few weeks ago the database crashed and the whole server had to be rebooted. This happened again to...