[SOLVED] Get angle between two entities

I’m trying to get the angle between for example the enemy and the player.
My result is NaN or an angle that stays the same.
So how i have to use the method below?

var q = new pc.Quat();
q.setFromAxisAngle(new pc.Vec3(0, 1, 0), 90);
var v = new pc.Vec3();
var angle = q.getAxisAngle(v);
// Should output 90
console.log(angle)
// Should output [0, 1, 0]
console.log(v.toString());

You can use the dot product method to get the angle between two entities.

Take a look here:

Thanks for your reaction.

Also with the code below the result is NaN…

var vecA = this.entity.getPosition();
var vecB = this.player.getPosition();
var dot = vecA.dot(vecB);
var angleInRadians = Math.acos(dot);
var angle = angleInRadians * pc.math.RAD_TO_DEG;
console.log(angle);

For that code to work the vectors need to be normalized, so with a small change it will work:

var vecA = this.entity.getPosition().normalize();
var vecB = this.player.getPosition().normalize();
var dot = vecA.dot(vecB);
var angleInRadians = Math.acos(dot);
var angle = angleInRadians * pc.math.RAD_TO_DEG;
console.log(angle);

What does .normalize() do?
It’s working but i can’t use it on this way i think.

I want to check if the player is inside the view field of the enemy.
So between -45 degrees and 45 degrees.

Any suggestion?

Normalizes scales everything to be between 0-1.

In that case I think you need first to get the vector between the two entities and then get the angle between the forward vector of the enemy (to have a point of reference for the field of view) and that new vector.

Something like this:

var enemyPos = this.entity.getPosition();
var playerPos = this.player.getPosition();

var targetDir = new pc.Vec3().copy(enemyPos);
targetDir.sub(playerPos).normalize();

var dot = targetDir.dot(this.entity.forward);
var angleInRadians = Math.acos(dot);
var angleToPlayer = angleInRadians * pc.math.RAD_TO_DEG;

if (angleToPlayer >= -45 && angleToPlayer <= 45){
   console.log('inside field of view!');
}

Not tested, but I think something like this may work here.

I get this error:

Cannot read property ‘x’ of undefined

Can you provide a simple sample project to debug?

I have to go to work now, but I will continue with it tomorrow.

1 Like

Goodmorning @Leonidas!
Here is a simple sample project with your script:
https://playcanvas.com/editor/scene/867937

Good day Albert!

Here you go, had a mistake in my code on how to get the forward vector of an entity. Now it’s fixed together with a small performance improvement (reuse a helper vector instead of creating a new one per frame):

https://playcanvas.com/editor/scene/868079

That’s working! This forum is nothing without you! :heart:

1 Like