QtQuickPaintedItem使用方向四元数旋转

时间:2013-07-25 14:02:23

标签: qt rotation

我正在尝试创建一个简单的QML组件,通过给定的四元数旋转项目。

我从OGRE Quaternion类型的ArUco获得四元数,并将其应用于QMatrix4x4。然后我从矩阵中获取QTransform并尝试将其应用于画家。不幸的是,效果完全不同:

void Marker::paint(QPainter *painter) 
{ 
  QMatrix4x4 mat; 
  QQuaternion q = QQuaternion(0.883290,-0.128302,0.436779,-0.112081); 
  mat.rotate(q);

  QRect rect = QRect(width()/4,height()/4,width()/2,height()/2);

  painter->setTransform(mat.toTransform()); 
  painter->fillRect(rect, Qt::red); 
}

结果是:

enter image description here

它应该看起来像这样:

enter image description here

UPDATE#1 :我认为问题是我使用方向四元数旋转矩阵,而我可能应该以其他方式应用该方向?我不完全理解四元数,有人可以解释这是不是一个错误?

更新#2 :我详细了解了它,结果确定方向是相对于参考位置的旋转。我认为我有这样的立场,我不知道如何应用它。

1 个答案:

答案 0 :(得分:0)

上面的代码是正确的,我的例子就是它实际上是一个立方体而不是一个简单的矩形,结果是立方体的前墙。基本上我所要做的就是:

QQuaternion q = QQuaternion(0.883290,-0.128302,0.436779,-0.112081); 
QQuaternion y90 = QQuaternion(sqrt(0.5),sqrt(0.5),0,0); // 90 degree around x quaternion

QQuaternion wyn = q*y90;
wyn = wyn.normalized(); // not needed in this case but if you want to rotate for say 180 degrees it's needed