How to rotate an object so that it is in a certain direction?

If I know in which direction the object is facing, for example (0, 0, 1). What is the easiest way to rotate an object to a direction vector (1, 1, 1)?

Edit:

I tried this, but something is wrong with this logic:

var newDir = new THREE.Vector3(1,1,1);

var objectDir= new THREE.Vector3(0, 0, 1); 
objectDir.applyQuaternion(object.quaternion); // rotate this vector to object facing direction

var pos = new THREE.Vector3();
pos.addVectors(newDir, object.position);

var axis = objectDir.cross(newDir).normalize();
var angle = objectDir.angleTo(pos);
object.rotateOnAxis(axis, angle);
+4
source share
1 answer

Solved it using lookAt:

var newDir = new THREE.Vector3(1, 1, 1);
var pos = new THREE.Vector3();
pos.addVectors(newDir, object.position);
object.lookAt(pos);
+6
source

Source: https://habr.com/ru/post/1530835/


All Articles