Irrlicht 关于vector3 getHorizonAngle()的作用

来源:互联网 发布:windows桌面图标 编辑:程序博客网 时间:2024/05/29 03:43

关于vector3<T> getHorizonAngle()的作用:

下面是出现在CSceneNodeAnimatorCameraFPS.cppline135的几句代码:

// update position

       core::vector3df pos = camera->getPosition();

 

       // Update rotation

       core::vector3df target = (camera->getTarget() - camera->getAbsolutePosition());

       core::vector3df relativeRotation = target.getHorizontalAngle();

下面将对这几句对应的功能目的和方式做一个诠释:

功能目的: 目的点到摄像机的点两点之间向量A, 通过向量B(0,0,1)旋转至该向量A的旋转值(pitch(坡度), row in degreees(Y轴旋转), 0).通过该旋转值可以执行正确的旋转动画.在函数中则实现了FPS镜头下通过鼠标或者键盘改变了摄像机镜头后进行相应的旋转动画.

              方式:函数通过以下的两句代码获得了target变量值,此值为目标点相对摄像机点的相对坐标位置.

core::vector3df pos = camera->getPosition();

core::vector3df target = (camera->getTarget() - camera->getAbsolutePosition());

接下来便调用了该方法:

core::vector3df relativeRotation= target.getHorizontalAngle();

该方法的定义如下:

const f64 PI64       =3.1415926535897932384626433832795028841971693993751;

const f64 RADTODEG64 =180.0 / PI64;

vector3d<T> getHorizontalAngle()const

              {

                     vector3d<T> angle;

                     angle.Y = (T)(atan2(X, Z) * (T) RADTODEG64);

 

                     if (angle.Y < 0.0f)

                            angle.Y += 360.0f;

                     if (angle.Y >= 360.0f)

                            angle.Y -= 360.0f;

 

                     const T z1 = core::squareroot(X*X + Z*Z);

                     angle.X = (T)(atan2(z1, (T)Y) * (T) RADTODEG64 - (T)90.0);

 

                     if (angle.X < (T) 0.0)

                            angle.X += (T) 360.0;

                     if (angle.X >= (T) 360.0)

                            angle.X -= (T) 360.0;

                     return angle;

              }

以下是原文注释

//! Get the rotations that wouldmake a (0,0,1) direction vector point in the same direction as this directionvector.

              /** Thanks to Arrason the Irrlicht forums for this method. This utility method is very useful for

              orientingscene nodes towards specific targets. For example, if this vector represents the difference

              betweentwo scene nodes, then applying the result of getHorizontalAngle() to one scenenode will point

              itat the other one.

              Examplecode:

              //Where target and seeker are of type ISceneNode*

              constvector3df toTarget(target->getAbsolutePosition() -seeker->getAbsolutePosition());

              constvector3df requiredRotation = toTarget.getHorizontalAngle();

              seeker->setRotation(requiredRotation);

              /returnA rotation vector containing the X (pitch) and Y (raw) rotations (in degrees)that when applied to a

              +Z(e.g. 0, 0, 1) direction vector would make it point in the same direction asthis vector. The Z (roll) rotation

              isalways 0, since two Euler rotations are sufficient to point in any givendirection. */

 

    该方法通过几个简单的三角计算得到了pitch坡度值和rowin degrees,两者都是度数,rollZ将一直为0,因为所得旋转角是根据Z标向量来旋转的

    在已知TargetVector坐标值的情况下根据直角三角形的边边关系以及欧拉公式能够简单地求出pitch和row的角度.

原创粉丝点击