四元数旋转ogre中的4元数和旋转

上传人:mg****85 文档编号:45885713 上传时间:2018-06-20 格式:PDF 页数:8 大小:125.04KB
返回 下载 相关 举报
四元数旋转ogre中的4元数和旋转_第1页
第1页 / 共8页
四元数旋转ogre中的4元数和旋转_第2页
第2页 / 共8页
四元数旋转ogre中的4元数和旋转_第3页
第3页 / 共8页
四元数旋转ogre中的4元数和旋转_第4页
第4页 / 共8页
四元数旋转ogre中的4元数和旋转_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《四元数旋转ogre中的4元数和旋转》由会员分享,可在线阅读,更多相关《四元数旋转ogre中的4元数和旋转(8页珍藏版)》请在金锄头文库上搜索。

1、四元数旋转:OGRE中的 4元数和旋转 疯狂代码 http:/CrazyC :http:/CrazyC 想象个物体在3D空间中移动过程该物体必然会涉及到旋转例如个怪物他运动方向会改变要改变其方向只需要对 其进行旋转即可 旋转方式大致分为 3种:Euler旋转矩阵旋转以及 4元数旋转 这里稍微记录下我目前对于 4元数旋转理解对于 4元数方面数学以及其原理这里不关心只需要学会如何使用即 可 无论是哪种旋转物体和该物体局部坐标系的间相对位置相对方位都是不会改变因此在进行两个局部旋转(即相对 于局部坐标系)时要注意结果可能不是你预期 对于Euler旋转OGRE中为SceneNode提供了yaw, pi

2、tch, roll的类接口这些接口默认都是参照局部坐标系旋转 可以通过第 2个参数来指定例如 yaw( Degree( 90 ), SceneNode:TS_WORLD ); OGRE中Quaternion类用于 4元数处理该类(也可以说是 4元数本身)有 4个成员:x,y,z,w这 4个数分别代表什么 ? 在OGRE论坛上我找到了些可以让人很容易理解信息: Quaternions can seem pretty daunting because of the use of imaginary numbers. Its much easier to understand you just ig

3、nore 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、概念使用角度/轴方式创建 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 Real fSin

5、= 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个参数指定旋转轴(可能不是)上面代

6、码就表示饶着X轴(正X方向)旋转-90度将该 4元数用于个 Scene Node旋转: sceneNode-rotate( q ); 即可实现该node饶X轴旋转-90度效果 再看下OGRE tutorial中段代码: Vector3 src = mNode-getOrientation * Vector3:UNIT_X; Ogre:Quaternion quat = src.getRotationTo(mDirection); mNode-rotate(quat); SceneNodegetOrientation获得该node方位用个 4元数来表示什么是方位?这里我也不清楚但是对于个 4元

7、数它这里表示是种旋转偏移偏移于朝向 OGRE论坛上有这么段话: The reason theres 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 direc

8、tion 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

9、 * Vector3:UINT_X又会得到什么?我可以告诉你第句代码整体作用就是获取该物体当前面向方向有关 4元数和向量相乘如图所示: 可以进步看出 4元数表示了个旋转偏移它和个向量相乘后就获得了另个向量该结果向量代表了这个旋转偏移所 确定方向 那么第句代码中为什么要乘上UNIT_X呢?这里所代表物体朝向就是正X方向 第 2句话由向量构造个 4元数它表示从当前朝向旋转到目朝向所需要个 4元数第 3句话就直接使用该 4元数来旋 转该node但是有时候似乎旋转不正确(在我实验中我使用模型其朝向是负Y方向在化时我又将其饶着X轴旋转了 负90度后来旋转时就不正确了)这可以通过rotate( q, Sc

10、eneNode:TS_WORLD)来矫正(所以估计是的前旋转导 致)(有时候我在想为什么对于所有物体旋转的类变换都不直接参照于世界坐标系?我们最终看到就是在世界坐标 系中 ) 注意当旋转角度是180度时这里就会出现为了防止这种可以这样做: Vector3 src = mNode-getOrientation * Vector3:UNIT_X; (1.0f + src.dotProduct(mDirection) yaw(Degree(180); Ogre:Quaternion quat = src.getRotationTo(mDirection); mNode-rotate(quat); /

11、 原因的类可以参看OGRE tutorial以上信息都是个人理解行文也比较乱主要是自己做笔记些基本定义可以参看其 他书籍 相关参考: http:/www.ogre3d.org/wiki/index.php/Intermediate_Tutorial_1 http:/www.ogre3d.org/phpBB2/viewtopic.php?t=24219&view=previous&sid=ce193664e1d3d7c4af5 09e6f4e2718c6 http:/www.ogre3d.org/phpBB2/viewtopic.php?t=27250&view=previous&sid=86f

12、6055c4b749dab640 588cf90d6f745 other infomations: Euler rotations can be useful and are supported in Ogres 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

13、it. However to actually rotate an object in Ogre, youll 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 co

14、ordinates 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元数代表个旋转偏移值相对于物体朝向方向 物体参照局部坐标系旋转无论如何旋转它相对于该坐标系而言其方位还是没变化 物体相对于局部坐标旋转只是旋转整个坐标系而已 2009-2-12 5:21:02 疯狂代码 http:/CrazyC

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号