# 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.