I am using SplineCurve3 to plot a line only on the X and Y axis, I have a cube successfully animating along this line by using spline.getPoint(t)
where t is 0-1 in time. I am trying to orient the cube to the line via its up vector which is Y using the dot product.
It is almost aligned but ever so slightly out. I thought that I would use the dot product of the Y vector and the tangent of the current point as the angle to rotate a quaternion to.
Here is my render function:
function render() {
var updateMatrix = new THREE.Matrix4();
updateMatrix.setPosition(spline.getPoint(t));
var angle = new THREE.Vector3(0,1,0).dot(spline.getTangent(t).normalize());
var quat = new THREE.Quaternion;
quat.setFromAxisAngle(new THREE.Vector3(0,0,1), angle);
updateMatrix.setRotationFromQuaternion(quat);
marker.rotation.getRotationFromMatrix(updateMatrix);
marker.matrixWorld = updateMatrix;
t = (t >= 1) ? 0 : t += 0.002;
renderer.render(scene, camera);
}
And here is a fiddle demonstrating my problem, can anyone tell me where I'm going wrong with the rotation aspect?
You can edit my - jsfiddle example