Unity 四元数

1,Quaterion 四元数

四元数可理解为一个旋转轴向量和一个旋转角度。注意:这里不是点绕轴旋转,而是一个矢量绕轴旋转。旋转后得到的也是一个矢量。因此,如果想要计算某个点P绕着某个固定的轴旋转一个角度后的点的坐标,那么首先需要在轴上固定一点S, 连接SP组成矢量。用四元数计算旋转后的矢量,再加上S点坐标,就得到了旋转后的坐标。

例:计算某点绕某一轴旋转一定的角度后的点的坐标

static function RotateAroundPoint(point : Vector3, pivot : Vector3, angle : Quaternion) : Vector3

{

     var finalPos : Vector3 = point - pivot;

     //Center the point around the origin

     finalPos = angle * finalPos;

     //Rotate the point.

     finalPos += pivot;

     //Move the point back to its original offset.

     return finalPos;

}

var newPos = SomeClass.RotateAroundPoint(Vector3.forward, Vector3.zero, Quaternion.Euler(0, 90, 0) );

Debug.Log(newPos);

//Prints (1, 0, 0) or is it (-1, 0, 0 )?  Something like 2.html

四元数其他网址

http://hi.baidu.com/czx_main/blog/item/71468e3690f438390a55a992.html

http://oulehui.blog.163.com/blog/static/796146982011273184592/

http://www.cnblogs.com/softimagewht/archive/2010/11/16.html

一般来说,三维空间上没有顺时针或者逆时针的概念。只有当我们确定了一个视线方向时,才区分顺时针和逆时针。大部分四元数规定都是,从旋转轴的反方向看过去,逆时针的角度为正。比如你的旋转轴为z轴正方向(例如从屏幕伸出来的那个轴),那么我向屏幕望去(也就是z轴反方向)看去,逆时针旋转的角度为正,顺时针为负。除非你用的四元数做了特殊规定,否则大部分都是这样的。

2,Gimbal Lock万向锁和EulerAngle欧拉角

Gimbal Lock 视频:http://www.cnblogs.com/mavaL/articles/1860354.html

 

2011-03-24

Transform

1,.transform.forward 物体在世界坐标系下z轴的方向,如下图所示

transform.forward = (1,0,0)而Vector3.forward则表示世界坐标系z轴的方向,即:(0,0,1)

测试实例:

transform.Translate(transform.forward*Time.deltaTime);//

物体每秒钟将相对自身坐标系移动(1,0,0)个单位,即:向下移动。

transform.Translate(Vector3.forward*Time.deltaTime);//

物体每秒钟将相对自身坐标系移动(0,0,1)个单位,即:向右移动。

2,transform. Rotate (eulerAngles : Vector3, relativeTo : Space = Space.Self)

       注意eulerAngle的旋转是有顺序的。Unity中是按照z-x-y的顺序旋转的。

3,transform.RoateAround (point : Vector3, axis : Vector3, angle : float)

可以实现一个物体绕另一个物体旋转. 例:

transform.RotateAround(_target.transform.position,Vector3.up,Time.deltaTime*_speed);// 绕_target物体旋转,旋转轴为y轴

(Vector3.up, Time.deltaTime*_speed);//绕y轴旋转,注意区分:

transform.RotateAround(transform.up, Time.deltaTime*_speed);

4,function TransformDirection (direction : Vector3) : Vector3

//将局部坐标转换为世界坐标,例:

Vector3 vecRet = transform.TransformDirection(Vector3.right);//将局部坐标系下(1,0,0)点转换为世界坐标系下的点。vecRet的值为(0,0,-1).该值和transform.right的值等价,

5,InverseTransformDirection(Vector3.right)返回值为(0,0,-1),如下图所示:

例:

relativePosition = cameraTransform.InverseTransformDirection(transform.position - cameraTransform.position);

6,transform.TransformPoint(2,0,0);//

Let's say that if you hold your right arm straight out to the side, the local-space coordinate (2, 0, 0) is at the tips of your fingers. No matter where you go or how you orient yourself, as long as you keep your arm straight out, the tips of your fingers will still be at exactly the same location in local space.

However, as you move and re-orient yourself, the world-space position of the tips of your fingers is constantly changing. This is what TransformPoint() does; it takes the local-space coordinate ((2, 0, 0) in this case) and converts it to a world-space coordinate based on the current transform for the object (the object being you in our example).

例:在距离当前物体的右侧2个单位的距离的位置处创建一个_target物体。

Instantiate(_target, transform.TransformPoint(Vector3.right*2), transform.rotation);

.InverseTransformPoint()将世界坐标系下的点转化成相对坐标系下的点,如:在Inspector面板上显示的物体的坐标为世界坐标系下的点,如果调用该函数的话,则返回值为(0,0,0)

transform.InverseTransformPoint(new Vector3(2630.192f, 763.5179f,1161.068f));

7, static function FromToRotation (fromDirection : Vector3, toDirection : Vector3)

将物体的fromDirection方向旋转到toDirection方向,例:将物体x、z的角平分线的方向(1,0,1)旋转至世纪坐标系z轴的方向。

public Vector3 _fromDir;

public Vector3 _toDir;

Quaternion q = Quaternion.identity;

q.SetFromToRotation(_fromDir, _toDir);

//等价于 q = Quaternion.FromToRotation(_fromDir, _toDir);

transform.rotation = q;

print(transform.right.ToString());

旋 转 前

旋 转 后

8,Quaternion.()

         建立一个旋转使z轴朝向view  y轴朝向up.其功能与LookRotation一样

9,Quaternion.AngleAxis(angle : float, axis:Vector3)

Create a rotation which rotates angle degrees around axis

创建一个绕着axis旋转angle度的旋转,例:

transform.rotation = Quaternion.AngleAxis(30.0f, Vector3.up);//将物体的旋转角度设置成绕Y轴旋转30度。无论物体以前的旋转如何,最终的旋转就是上面代码所设置的旋转。如图所示:

转 前

旋 转 后

10,Quaternion.Angle()

计算两个旋转之间的夹角。跟Vector3.Angle(a : Quaternion, b : Quaternion) 作用一样。

参考网址:http://www.cnblogs.com/softimagewht/archive/2010/11/16.html

 

11,Unity3D 使用的是左手坐标系,即:Z轴方向),握向右方(X轴方向),

大拇指的方向即为Y轴方向。

 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值