如何从之前的两个数据包中推断出新的四元数旋转



我已经无计可施了!我正在我的第一人称射击游戏中减少滞后,现在只是添加一些外推法。我可以推断位置;获取最后两个位置和速度,然后将速度添加到现有位置(*delta时间)。然而,我不能做同样的旋转。默认情况下,角度是Euler,但我可以(并且确实)将它们转换为四元数,因为它们可能会受到万向节锁定。如何从之前的两个方向推断出一个新的方向?我有数据包之间的时间,2个数据包和当前方向。

我在这里找到了一个很好的答案:http://answers.unity3d.com/questions/168779/extrapolating-quaternion-rotation.html

我根据自己的需要调整了代码,它运行得很好!

对于两个四元数qa,qb,这将使用相同的公式进行插值和外推。t是插值/外推法的量,从qa->qb开始0.1=0.1,从qa->qb开始外推一整步,等等。我使用了自写函数来允许使用四元数/轴角度和opencv cv::Mat,但我可能会选择Eigen来代替

Quat qc = QuaternionMultiply(qb, QuaternionInverse(qa)); // rot is the rotation from qa to qb     
AxisAngle axisAngleC = QuaternionToAxisAngle(qc); // find axis-angle representation
double ang = axisAngleC.angle; //axis will remain the same, changes apply to the angle
if (ang > M_PI) ang -= 2.0*M_PI; // assume rotation to take the shortest path
ang = fmod(ang * t,2.0*M_PI);   // multiply angle by the interpolation/extrapolation factor and remove multiples of 2PI
axisAngleC.angle = ang;
return QuaternionMultiply(AxisAngleToQuaternion(axisAngleC),qa); // combine with first rotation

如果你将两个方向表示为向量,它们的向量叉积将给出旋转轴,向量点积可以用来找到旋转角度。

然后,可以使用与计算标量速度相同的方式计算角速度,并使用它来计算围绕先前确定的轴的外推旋转。

最新更新