3

我想创建一个类似于示例的第三人称相机。如果相机和物体之间的旋转差异太大(可能超过百分之十),相机应该贴在物体后面并旋转。

这是我的实际相机代码:

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;
};

但是如果角度差太大,我需要冻结轴。我怎样才能做到这一点?

任何帮助,将不胜感激。

4

1 回答 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();
于 2012-07-10T09:42:15.480 回答