rotateTowards: function(target, angularSpeed) { // TODO
// If is on the same position
if (this.position.isEqual(target)) {
return;
}
var vecStart = this.rotation.getForwardVector();
var vecTarget = this.position.sub(target).normalize();
var angleLeft = Math.acos(vecStart.dot(vecTarget));
var fullAngle = angularSpeed * deltaT;
// If already rotated towards the target
if (Math.areScalarsSimilar(angleLeft, 0)) {
return;
}
// If vectors are opposite
else if (Math.areScalarsSimilar(angleLeft, Math.PI)) {
// Turn around up-axis
this.rotation.multiply(new Quaternion().axisToQuaternion(fullAngle, upVec()));
}
// If vectors casually differs
else {
var rotAxis = vecStart.cross(vecTarget);
// If able to make another full rotation step
if (angleLeft > fullAngle) {
this.tempQuat.axisToQuaternion(fullAngle, rotAxis);
this.pitch += this.tempQuat.getPitch();
this.yaw += this.tempQuat.getYaw();
this.adjustAngles();
this.rotation.pitchYawRollToQuaternion(this.pitch, this.yaw, 0);
log(vecStart.toString());
}
// If the angle left is smaller than the angular speed
else {
// Set the final rotation
this.tempQuat.axisToQuaternion(angleLeft, rotAxis);
this.pitch += this.tempQuat.getPitch();
this.yaw += this.tempQuat.getYaw();
this.adjustAngles();
this.rotation.pitchYawRollToQuaternion(this.pitch, this.yaw, 0);
}
}
MVMatrix.setRotation(this.rotation.quaternionToMatrix());
},
And a math library that I use: https://www.dropbox.com/s/9ejj64v59xzqn6y/myMath.js