四元数旋转:OGRE中的 4元数和旋转



想象个物体在3D空间中移动过程该物体必然会涉及到旋转例如个怪物运动方向会改变要改变其方向只需要对其进行旋转即可 旋转方式大致分为 3种:Euler旋转矩阵旋转以及 4元数旋转

这里稍微记录下我目前对于 4元数旋转理解对于 4元数方面数学以及其原理这里不关心只需要学会如何使用即可

无论是哪种旋转物体和该物体局部坐标系的间相对位置相对方位都是不会改变因此在进行两个局部旋转(即相对于局部坐标系)时要注意结果可能不是你预期

对于Euler旋转OGRE中为SceneNode提供了yaw, pitch, roll的类接口这些接口默认都是参照局部坐标系旋转可以通过第 2个参数来指定例如 yaw( Degree( 90 ), SceneNode::TS_WORLD );

OGREQuaternion类用于 4元数处理该类(也可以说是 4元数本身)有 4个成员:x,y,z,w这 4个数分别代表什么?

OGRE论坛上我找到了些可以让人很容易理解信息:

Quaternions can seem pretty daunting because of the use of \'imaginary\' numbers. It\'s much easier to understand you just ignore this concept completely. The basic formula for creating a quaternion from angle/axis is:





Q = cos (angle/2) + i (x * sin(a/2)) + j (y * sin(a/2)) + k(z * sin(a/2))



or

Code:



Q.w = cos (angle / 2)

Q.x = axis.x * sin (angle / 2)

Q.y = axis.y * sin (angle / 2)

Q.z = axis.z * sin (angle / 2)

稍微忽略下那些复数的类概念使用角度/方式创建 4元数公式为:

Q = cos (angle/2) + i (x * sin(a/2)) + j (y * sin(a/2)) + k(z * sin(a/2))



对应代码为:

Q.w = cos (angle / 2)

Q.x = axis.x * sin (angle / 2)

Q.y = axis.y * sin (angle / 2)

Q.z = axis.z * sin (angle / 2)



再看OGRE中有关Quaternion个构造 4元数源代码:

void Quaternion::FromAngleAxis (const Radian& rfAngle,

const Vector3& rkAxis)

{

// assert: axis is unit length



//

// The quaternion representing the rotation is

// q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k)



Radian fHalfAngle ( 0.5*rfAngle );

Real fSin = Math::Sin(fHalfAngle);

w = Math::Cos(fHalfAngle);

x = fSin*rkAxis.x;

y = fSin*rkAxis.y;

z = fSin*rkAxis.z;[Page]

}



虽然可以说 4元数中w代表旋转量x, y, z代表对应轴但是这也不全正确我们看到对于真正旋转量啊的类数据是需要进行有些公式变换后才得到w, x, y, z

但是即使如此我们还是可以这样简单地构造个 4元数用于旋转:

Quaternion q( Degree( -90 ), Vector3::UNIT_X );

该构造个参数指定旋转角度第 2个参数指定旋转轴(可能不是)上面代码就表示饶着X轴(正X方向)旋转-90将该 4元数用于

Scene Node旋转:

sceneNode->rotate( q );

即可实现该nodeX轴旋转-90效果



再看OGRE tutorial段代码:

Vector3 src = mNode->getOrientation * Vector3::UNIT_X;

Ogre::Quaternion quat = src.getRotationTo(mDirection);

mNode->rotate(quat);

SceneNodegetOrientation获得该node方位个 4元数来表示什么是方位?这里我也不清楚但是对于个 4元数它这里表示种旋转偏移偏移于朝向

OGRE论坛上有这么段话:

The reason there\'s no other way to convert a quaternion to a vector is because a quaternion is relative. It has no direction.

With a direction (like a vector) you could say \"face north east\".

But with a quaternion, you say \"face 45 degrees clockwise from whatever direction you are already facing\" (very simplied example). Without knowing which way the object is already facing, a quaternion is virtually meaningless with respect to orientation. So we just default it to some initial direction, like Unit Z, and make all orientations relative to that.

然后

getOrientation * Vector3::UINT_X又会得到什么?我可以告诉你句代码整体作用就是获取该物体当前面向方向有关 4元数和向量相乘如图所示:

\"\"

lineDrawn pixelLineWidth 0\">

可以进步看出 4元数表示了个旋转偏移它和个向量相乘后就获得了另个向量该结果向量代表了这个旋转偏移所确定方向

那么第句代码中为什么要乘上UNIT_X呢?这里所代表物体朝向就是正X方向

第 2句话由向量构造个 4元数它表示从当前朝向旋转到目朝向所需要个 4元数第 3句话就直接使用该 4元数来旋转该node但是有时候似乎旋转不正确(在我实验中我使用模型其朝向是负Y方向化时我又将其饶着X轴旋转了负90后来旋转时就不正确了)这可以通过rotate( q, SceneNode::TS_WORLD)来矫正(所以估计是的前旋转导致)(有时候我在想为什么对于所有物体旋转的类变换都不直接参照于世界坐标系?我们最终看到就是在世界坐标系中

)

注意当旋转角度是180度时这里就会出现为了防止这种可以这样做:

Vector3 src = mNode->getOrientation * Vector3::UNIT_X;

((1.0f + src.dotProduct(mDirection)) < 0.0001f)

{

[Page]mNode->yaw(Degree(180));

}



{

Ogre::Quaternion quat = src.getRotationTo(mDirection);

mNode->rotate(quat);

} //



原因的类可以参看OGRE tutorial以上信息都是个人理解行文也比较乱主要是自己做笔记些基本定义可以参看其他书籍





相关参考:

http://www.ogre3d.org/wiki/index.php/Intermediate_Tutorial_1

http://www.ogre3d.org/phpBB2/viewtopic.php?t=24219&view=previous&sid=ce193664e1d3d7c4af509e6f4e2718c6
http://www.ogre3d.org/phpBB2/viewtopic.php?t=27250&view=previous&sid=86f6055c4b749dab640588cf90d6f745




other infomations:

Euler rotations can be useful and are supported in Ogre\'s API. There are problems however.

One is that the order of operations is important. If you pitch an object 90 degrees, then yaw it 90 you get a dferent result than yaw is done before pitch. Try it.



However to actually rotate an object in Ogre, you\'ll either need to convert it to an axis/angle

rotation, store the data in the matrix as an axis/angle and do the conversions yourself or will need to convert it to a quaternion.



A quaternion is composed of four components: a vector with x, y, z coordinates and a w rotation.



X, Y and Z describe a vector which is the axis of rotation.W is the amount of rotation around the vector axis.



Multiplying a quaternion by a vector gives us a vector describing the rotational off from that vector (a rotated vector).





Vector3::getRotationTo(Vector3) s a quaternion describing a relative rotation from _disibledevent=>





This orientation quaternion can be thought of as a rotational off from an objects initial facing vector.

getOrientation返回 4元数代表个旋转偏移值相对于物体朝向方向



物体参照局部坐标系旋转无论如何旋转它相对于该坐标系而言其方位还是没变化

物体相对于局部坐标旋转只是旋转整个坐标系而已



Tags:  对偶四元数 四元数法 四元数 四元数旋转

延伸阅读

最新评论

发表评论