Irrlicht 关于vector3 getHorizonAngle()的作用

关于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 would make a (0,0,1) direction vector point in the same direction as this direction vector.

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

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

              between two scene nodes, then applying the result of getHorizontalAngle() to one scene node will point

              it at the other one.

              Example code:

              // Where target and seeker are of type ISceneNode*

              const vector3df toTarget(target->getAbsolutePosition() - seeker->getAbsolutePosition());

              const vector3df requiredRotation = toTarget.getHorizontalAngle();

              seeker->setRotation(requiredRotation);

              /return A 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 as this vector. The Z (roll) rotation

              is always 0, since two Euler rotations are sufficient to point in any given direction. */

 

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值