Unity 四元数

来源:互联网 发布:2013年网络流行歌曲 编辑:程序博客网 时间:2024/06/05 11:37

 

2011-03-23

1,Quaterion 四元数

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

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

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

{

     var finalPos: Vector3 = point - pivot;

     //Center thepoint around the origin

     finalPos =angle * finalPos;

     //Rotate thepoint.

     finalPos +=pivot;

     //Move thepoint back to its original offset.

     returnfinalPos;

}

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.RotateAroundtransform.up,Time.deltaTime*_speed;

4,function TransformDirection(direction : Vector3) : Vector3

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

Vector3vecRet = 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 saythat if you hold your right arm straight out to the side, the local-spacecoordinate (2, 0, 0) is at the tips of your fingers. No matter where you go orhow you orient yourself, as long as you keep your arm straight out, the tips ofyour fingers will still be at exactly the same location in local space.

However, asyou move and re-orient yourself, the world-space position of thetips of your fingers is constantly changing. This is what TransformPoint()does; it takes the local-space coordinate ((2, 0, 0) in this case) and convertsit 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(newVector3(2630.192f, 763.5179f,1161.068f));

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

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

publicVector3 _fromDir;

publicVector3 _toDir;

Quaternionq = 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 whichrotates 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轴方向。

 

原创粉丝点击