c++ - 具有最短路径的四元数 slerp 不起作用

标签 c++ opengl 3d quaternions

我的 slerp 例程如下。从我读过的内容来看,针对 > 0 的检查应该处理它,所以它总是采用最短路径。但它从来没有。在我穿过“极点”的情况下,四元数会翻转并产生具有 NAN 值的角度。

quat quat::slerp(quat dest, float t)
{
  const quat &from = *this;
  static const double epsilon = 0.0001;
  double theta, cosTheta, sinTheta;
  double p, q;

  cosTheta = from.x*dest.x + from.y*dest.y + from.z*dest.z + from.w*dest.w;

  if(cosTheta < 0.0)
  {
    dest = { -from.x, -from.y, -from.z, -from.w };
    cosTheta = -cosTheta;
  }

  if((1.0-fabs(cosTheta)) > epsilon)
  {
    theta = acos(cosTheta);
    sinTheta = sin(theta);
    q = sin((1-t) * theta) / sinTheta;
    p = sin(t*theta) / sinTheta;
  }
  else
  {
    q = 1-t;
    p = t;
  }

  quat qo;
  qo.w = (float)((q * from.w) + (p * dest.w));
  qo.x = (float)((q * from.x) + (p * dest.x));
  qo.y = (float)((q * from.y) + (p * dest.y));
  qo.z = (float)((q * from.z) + (p * dest.z));

  return qo;
}

最佳答案

也许还有其他错误,但这行肯定有一个:

dest = { -from.x, -from.y, -from.z, -from.w };

它用-from 覆盖了dest,这是不正确的。应该是:

dest = { -dest.x, -dest.y, -dest.z, -dest.w };

关于c++ - 具有最短路径的四元数 slerp 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51885082/

相关文章:

c++ - 将绑定(bind)到 FBO 的纹理复制到另一个 OpenGL 上下文

java - 使用 LWJGL 进行桌面捕获

c++ - opengl 1D 纹理 : fill with arbitrary float values

algorithm - 将 3D 矩阵中的数据与另一个矩阵聚类

python - 同时对三个变量数组进行for循环

python - .ply 多边形网格到 2D 深度图

c++ - 编译器错误:std::basic_ios<_Elem, _Traits>::basic_ios(const std::basic_ios<_Elem, _Traits>::_Myt &)

c++ - 如何正确处理 upper_bound 的迭代器

c++ - 将 "2.12e-6"之类的字符串转换为 double

c++ - 信号和解锁命令