5

I'm attempting rotate an arrow on the surface of a planet to face the direction it is traveling. I can get the direction vector and the up vector from the surface normal. How would I go about turning this into a quaternion for my arrows rotation. I'm using three.js so any suggested methods they provided that I can use would be useful. Cheers.

enter image description here

The information I have so far.

var upVector = new THREE.Vector3();
upVector.subVectors( new THREE.Vector3(0,0,0), lastVec );

var dirVector = new THREE.Vector3();
upVector.subVectors( lastVec, destVec );

var sideVector = new THREE.Vector3();
sideVector.crossVectors( dirVector, upVector );

var newUp = new THREE.Vector3();
newUp.crossVectors( dirVector, sideVector );

1 Answer 1

11

Try (with myDirectionVector):

var mx = new THREE.Matrix4().lookAt(myDirectionVector,new THREE.Vector3(0,0,0),new THREE.Vector3(0,1,0));
var qt = new THREE.Quaternion().setFromRotationMatrix(mx);
2
  • Thanks for the suggestion, this is what I've got so far, will test with my project jsfiddle.net/wyb9jm5e/3 Commented Sep 29, 2015 at 19:54
  • Cool, now just update myDirectionVector for each frame (since right now you're forcing it to a constant), and the vector-following will be super-obvious :)
    – bjorke
    Commented Sep 29, 2015 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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