0
\$\begingroup\$

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.

Thank you in advance

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – DMGregory Jan 23 at 12:56
  • \$\begingroup\$ B vector in my case is a position of other object and not a rotation. This works in that case? Thanks \$\endgroup\$ – Rui d'Orey Jan 23 at 16:49
  • \$\begingroup\$ You can make a direction from a position by subtracting your starting point from the end point. \$\endgroup\$ – DMGregory Jan 23 at 17:04
  • \$\begingroup\$ 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. \$\endgroup\$ – Rui d'Orey Jan 23 at 17:07
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Jan 23 at 17:12
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.