使用四元数创建第三人称摄像头位置计算

3
我希望创建一个类似示例的第三人称摄像机。摄像机应该固定在物体后面,如果摄像机和物体之间的旋转差异太大(可能超过10%),则应旋转。
这是我的实际摄像机代码:
var targetPosition = this.getTargetPosition();
var targetRotation = this.getTargetRotation();
var tmpQuaternion = new THREE.Quaternion();
tmpQuaternion.setFromAxisAngle(new THREE.Vector3(0, 1, 0), 180 * (Math['PI'] / 180));
this.camera.quaternion = targetRotation;
this.camera.position = targetPosition;
this.camera.quaternion.multiplySelf(tmpQuaternion);
this.camera.quaternion.normalize();
this.camera.updateMatrix();
this.camera.translateZ(200);
this.camera.translateY(50);

但是目前存在几个问题。相机四元数不应该直接设置为目标旋转。但我不知道如何计算相机四元数和目标四元数之间的差异,如果距离太远可能要使用以下方法:

var qm = new THREE.Quaternion();
THREE.Quaternion.slerp(targetRotation, this.camera.quaternion, qm, time);
this.camera.quaternion = qm;

第二个问题是摄像机位置本身。目前,我将相机位置设置为对象位置,并将其平移回视图后方,但平移应该已经在目标位置了,相机位置应该被平移到目标位置。更新1: 我制作了一个示例html:http://ssachtleben.github.com/CameraProblem/更新2: 我现在取得了四元数差异。
getAxisAngle = function(quaternion1, quaternion2) {
  var tmpQuaternion = new THREE.Quaternion();
  tmpQuaternion.setFromAxisAngle(new THREE.Vector3(0, 1, 0), 180 * (Math['PI'] / 180));
  var tmpRotation1 = quaternion1.clone();
  tmpRotation1.multiplySelf(tmpQuaternion);
  tmpRotation1.normalize();
  var tmpRotation2 = quaternion2.clone();
  if (tmpRotation2.w > 1) {
    tmpRotation2.normalize();
  }
  var angle1 = 2 * Math['acos'](tmpRotation1.w);
  var angle2 = 2 * Math['acos'](tmpRotation2.w);
  var diff = angle1 > angle2 ? angle1 - angle2 : angle2 - angle1;
  return diff;
};

但是如果角度差太大,我需要冻结轴。我该怎么做?

任何帮助都将不胜感激。

1个回答

2

好的,最终相机已经修好并且正常工作:

var targetPosition = this.getTargetPosition();
var targetRotation = this.getTargetRotation();
var tmpQuaternion = new THREE.Quaternion();

tmpQuaternion.setFromAxisAngle(new THREE.Vector3(0, 1, 0), 180 * (Math['PI'] / 180));
targetRotation.multiplySelf(tmpQuaternion);
targetRotation.quaternion.normalize();

var qm = new THREE.Quaternion();
THREE.Quaternion.slerp(this.camera.quaternion, targetRotation, qm, 0.07);
this.camera.quaternion = qm;
this.camera.quaternion.normalize();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接