收藏 分销(赏)

四元数旋转OGRE中的4元数和旋转.pdf

上传人:xrp****65 文档编号:6144262 上传时间:2024-11-28 格式:PDF 页数:8 大小:125.04KB 下载积分:10 金币
下载 相关 举报
四元数旋转OGRE中的4元数和旋转.pdf_第1页
第1页 / 共8页
四元数旋转OGRE中的4元数和旋转.pdf_第2页
第2页 / 共8页


点击查看更多>>
资源描述
四元数旋转:OGRE中的 4元数和旋转 疯狂代码 CrazyC :CrazyC 想象个物体在3D空间中移动过程该物体必然会涉及到旋转例如个怪物他运动方向会改变要改变其方向只需要对其进行旋转即可 旋转方式大致分为 3种:Euler旋转矩阵旋转以及 4元数旋转 这里稍微记录下我目前对于 4元数旋转理解对于 4元数方面数学以及其原理这里不关心只需要学会如何使用即可 无论是哪种旋转物体和该物体局部坐标系的间相对位置相对方位都是不会改变因此在进行两个局部旋转(即相对于局部坐标系)时要注意结果可能不是你预期 对于Euler旋转OGRE中为SceneNode提供了yaw,pitch,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 easierto understand you just ignore this concept completely.The basic formula for creating a quaternionfrom 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);即可实现该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元数它这里表示是种旋转偏移偏移于朝向 OGRE论坛上有这么段话:The reason theres no other way to convert a quaternion to a vector is because a quaternion isrelative.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 alreadyfacing(very simplied example).Without knowing which way the object is already facing,aquaternion is virtually meaningless with respect to orientation.So we just default it to some initialdirection,like Unit Z,and make all orientations relative to that.然后 getOrientation*Vector3:UINT_X又会得到什么?我可以告诉你第句代码整体作用就是获取该物体当前面向方向有关 4元数和向量相乘如图所示:可以进步看出 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)yaw(Degree(180);Ogre:Quaternion quat=src.getRotationTo(mDirection);mNode-rotate(quat);/原因的类可以参看OGRE tutorial以上信息都是个人理解行文也比较乱主要是自己做笔记些基本定义可以参看其他书籍 相关参考:www.ogre3d.org/wiki/index.php/Intermediate_Tutorial_1 www.ogre3d.org/phpBB2/viewtopic.php?t=24219&view=previous&sid=ce193664e1d3d7c4af509e6f4e2718c6 www.ogre3d.org/phpBB2/viewtopic.php?t=27250&view=previous&sid=86f6055c4b749dab640588cf90d6f745 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 youget a dferent result than yaw is done before pitch.Try 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 toconvert 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 thevector 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 疯狂代码 CrazyC
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服