Lalle
Lalle

Reputation: 732

How to rotate an object to make it face a certain direction?

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

Edit:

I've 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's 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);

Upvotes: 6

Views: 8480

Answers (1)

Lalle
Lalle

Reputation: 732

Solved it with lookAt:

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

Upvotes: 11

Related Questions