# THREE.JS - How to know the shortest rotation direction an object must do to look at other?

I am trying to understand a way in THREE.JS (and the mathematical foundation) to get the direction an object should rotate to lookAt other but without actually rotate it. I've tried to use lookAt but that rotates the object completely.

I want to apply a small custom parameterized rotation on that direction. So I don't want all the rotation, just the direction of that shortest to look at rotation.

So, basically I have 2 object positions and I want the shortest rotation direction of one of them so that one looks at the second object position, independently of the second object rotation. And limit the axis so it works on a over the top 2d game setting (although the graphics are 3d)..

The rotation must be around Z for example.

And relative to the current object heading as this will be computed on each game loop iteration depending on the player position.

This is intended for a game where enemies try to follow the player but turn slow to look at him.

• Presumably you've looked at the Quaternion.setFromUnitVevtors method, which computes the shortest rotation that takes a vector pointing in direction A to a vector pointing in direction B? Jan 23 '20 at 12:56
• B vector in my case is a position of other object and not a rotation. This works in that case? Thanks Jan 23 '20 at 16:49
• You can make a direction from a position by subtracting your starting point from the end point. Jan 23 '20 at 17:04
• humm ok right, true.. A-B. nice nice. And what about the rotation direction? I don't want all the rotation, just the direction. And limit axis. Jan 23 '20 at 17:07
• Sounds like you should edit your question to be more precise about what outputs you want, what inputs you have, and how you've tried making the jump between them so far. Jan 23 '20 at 17:12

Solved using this:


// player world position
const vPlayerPositionRelativeToWorld = new THREE.Vector3().copy(player.transform.position);

// player position relative to enemy transform - includes rotation of the transform
const vPlayerPositionRelativeToLocalEnemyPositionAndRotation = enemy.transform.worldToLocal(vPlayerPositionRelativeToWorld);
vPlayerPositionRelativeToLocalEnemyPositionAndRotation.normalize();

// get angle to rotate
const maxRotateValue = .05;
const rotationToLookAtPlayer = Math.asin(vPlayerPositionRelativeToLocalEnemyPositionAndRotation.x);
const rotateAbsoluteValue = Math.min(Math.abs(rotationToLookAtPlayer), maxRotateValue);
const orientedRotationValue =  -Math.sign(rotationToLookAtPlayer) * rotateAbsoluteValue

enemy.transform.rotateZ( orientedRotationValue );


Not sure if there is a more efficient method but it works for this case.