As a rule, it is better not to contact object.matrix directly, but simply set the position , rotation and scale object. Let the library handle matrix manipulations. You must have matrixAutoUpdate = true; .
To process part of the rotation, first get the tangent to the curve.
tangent = spline.getTangent( t ).normalize();
You want to rotate the object so that its top vector (local y-vector) points in the direction of the tangent vector of the curve. First calculate the axis to rotate around. An axis is a vector perpendicular to a plane defined by an upward vector and a tangent vector. You use a cross product to get this vector.
axis.crossVectors( up, tangent ).normalize();
Please note that in your case, since the spline lies in a plane perpendicular to the z axis, the calculated axis will be parallel to the z axis. However, it can point in the direction of the positive z axis or the negative z axis - it depends on the direction of the tangent vector.
Now calculate the angle in radians between the up vector and the tangent vector. The dot product of the age vector and the tangent vector give the cosine of the angle between them (since both vectors have a unit length). Then you need to take the arc cosine to get the angle itself.
radians = Math.acos( up.dot( tangent ) );
Now remove the quaternion from the axis and angle.
marker.quaternion.setFromAxisAngle( axis, radians );
EDIT: updated script: http://jsfiddle.net/SCXNQ/891/ for three.js r.69
Westlangley
source share