尝试研究如何使用四元数来旋转沿路径移动的相机以寻找新的方向矢量

Trying to workout how to use quaternions to rotate a camera that is moving along a path to look in new direction vector

本文关键字:相机 寻找 移动 方向 路径 何使用 旋转 四元数      更新时间:2023-09-26

>我正在尝试平稳地旋转相机并且不改变相机方向的y向量,我可以使用look,它会在闪光灯下改变相机方向,但这对我不起作用,我希望随着相机方向的变化而平滑过渡。 我一直在阅读,并不了解所有内容,但在我看来,四元数是解决这个问题的方法。

我让this.object(我的相机)沿着设定的路径(this.spline.points)移动。 相机在任何时候的位置是(thisx,thisy,thisz)

我有cc[i]

我希望相机面对的方向的方向矢量(以前我使用lookat(cc[i]),它可以正确改变方向,但太快/瞬时)

使用我阅读的信息,我在下面尝试了这个,它只是导致屏幕在相机移动时变黑。

谁能解释一下我是否走在正确的轨道上,如何纠正我的代码。

谢谢

var thisx = this.object.matrixWorld.getPosition().x.toPrecision(3);
var thisy = this.object.matrixWorld.getPosition().y.toPrecision(3);
var thisz = this.object.matrixWorld.getPosition().z.toPrecision(3);
var i = 0;
do {
    var pathx = this.spline.points[i].x.toPrecision(3);
    var pathz = this.spline.points[i].z.toPrecision(3);
    if (thisx == pathx && thisz == pathz){
    this.object.useQuaternion = true;
    this.object.quaternion = new THREE.Quaternion(thisx, thisy, thisz, 1);
    var newvect;
    newvect.useQuaternion = true;
    newvect.quaternion = new THREE.Quaternion(thisx+cc[i].x, thisy+cc[i].y, thisz+cc[i].z, 1);
    var newQuaternion = new THREE.Quaternion();
    THREE.Quaternion.slerp(this.object.quaternion, newvect.quaternion, newQuaternion, 0.5);
    this.object.quaternion = newQuaternion;

     //this.object.lookAt( cc[i]);
            i = cc.length;
    } else i++;
} while(i < cc.length);

没有必要调用this.object.useQuaternion = true。这是默认行为。此外,this.object.quaternion包含当前的旋转,因此也不需要生成它。

您可能想尝试不同的方法 - 从样条位置、lookAt 和向上向量构造旋转矩阵,创建四元数路径作为预处理步骤:

var eye = this.spline.points[i].clone().normalize(); 
var center = cc[i].normalize();
var up = this.object.up.normalize();
var rotMatrix = new THREE.Matrix4().lookAt(eye, center, up);

然后,您可以从旋转矩阵创建四元数:

var quaternionAtSplineCoordinates = [];
quaternionAtSplineCoordinates.push(new THREE.Quaternion().setFromRotationMatrix(rotMatrix));

获得该路径后,您可以将四元数应用于动画循环中的摄像机 - 前提是您有足够多的样本。否则,您可以考虑使用 slerp 生成中间点。