Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

three.js

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);
like image 587
Lalle Avatar asked Mar 09 '14 13:03

Lalle


1 Answers

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);
like image 55
Lalle Avatar answered Oct 14 '22 01:10

Lalle