Quaternion四元数

Quaternion 四元数

四元数用于表示旋转。

、Quaternion属性与方法

一,属性:

xyz就不说了,只看一个eulerAngles,代码如下:

  1. public Quaternion rotation = Quaternion.identity;  
  2. void Start()  
  3. {  
  4. rotation.eulerAngles = new Vector3(0, 30, 0);  
  5. transform.rotation = rotation;  
  6. print(rotation.eulerAngles.y);  
  7. }  

效果:将物体绕y轴顺时针转30°,效果与transform.eulerAngles相同。物体rotation = (0,30,0)。但是不能直接为transform.rotation.eulerAngle赋值。


二,方法

1function ToAngleAxis (out angle :float, out axis :Vector3) :void

  1. public float angle = 0.0F;  
  2. public Vector3 axis = Vector3.zero;  
  3. void Start()  
  4. {  
  5. transform.eulerAngles = new Vector3(0, 90, 0);  
  6. transform.rotation.ToAngleAxis(out angle, out axis);  
  7. print(angle + " " + axis);  
  8. }  

效果:将物体的旋转角度返回到anglesaxis里(物体的z轴和世界坐标z轴的夹角)。
输入:transform.localEulerAngles=(0,0,0);
输出:angle=0, axis=(1,0,0);
输入:transform.localEulerAngles=(0,90,0);
输出:angle=90,axis=(0,1,0);
输入:transform.localEulerAngles=(270,0,0);
输出:angle=90,axis=(-1,0,0)

2function SetFromToRotation (fromDirection :Vector3, toDirection :Vector3) :void

  1. private Vector3 _from = Vector3.one;  
  2. private Vector3 _to = new Vector3(100f, 200f, 100f);  
  3. private Quaternion q;  
  4. private Vector3 headUpDir;  
  5. void Start()  
  6. {  
  7. q.SetFromToRotation(_from, _to);  
  8. transform.rotation=q;  
  9. headUpDir=transform.TransformDirection(Vector3.forward);  
  10. }  

效果:把物体的fromDirection旋转到toDirection
输入:a=Vector3(0,0,1); b=Vector3(0,1,0)//z轴朝向y
输出: q=(-0.7,0,0,0.7); headUpDir=(0,1,0)
输入:a=Vector3(0,0,1); b=Vector3(1,0,0)//z轴朝向x
输出: q=(0,0.7,0,0.7); headUpDir=(1,0,0)
输入:a=Vector3(0,1,0); b=Vector3(1,0,0)//y轴朝向x
输出: q=(0,0,-0.7,0.7); headUpDir=(0,0,1)

3function SetLookRotation (view : Vector3, up : Vector3 = Vector3.up) : void

  1. public Transform obj1;  
  2. public Transform obj2;  
  3. Quaternion q;  
  4. void Update()  
  5. {  
  6. q.SetLookRotation(obj1.position, obj2.position);  
  7. transform.rotation = q;  
  8. }  

效果:类似于LookAt()方法,使物体1始终注视着物体2

4, static operator * (lhs : Quaternion, rhs : Quaternion) : Quaternion

[csharp] view plaincopy

  1. public Transform extraRotation;  
  2. void Update()  
  3. {  
  4. transform.rotation *= extraRotation.rotation;  
  5. }  

效果:组合旋转lhsrhs。旋转一个点,首先用lhs,然后用rhs旋转,与使用组合旋转相同。注意旋转不可交换: lhs*rhs不等于rhs*lhs。 我理解旋转的角度就是将*号两边的角度加起来。

5static operator * (rotation : Quaternion, point : Vector3) : Vector3

  1. Vector3 relativeDirection = Vector3.forward;  
  2. Vector3 absoluteDirection;  
  3. void Update () {  
  4. absoluteDirection = transform.rotation * relativeDirection;  
  5. transform.position += absoluteDirection * Time.deltaTime;  
  6. }  

效果:将一个向量根据rotation旋转到另一个向量。


6static function Dot (a : Quaternion, b : Quaternion) : float
效果:将两个Quaternion点乘,返回一个float值,意义待补充。

7static function AngleAxis (angle : float, axis : Vector3) : Quaternion 

  1. void Start(){  
  2. transform.rotation = Quaternion.AngleAxis(30, Vector3.up);  
  3. }  

效果:将物体绕axis旋转angle度。

 

8static function FromToRotation (fromDirection :Vector3, toDirection :Vector3) : Quaternion 

  1. transform.rotation = Quaternion.FromToRotation(Vector3.up, transform.forward);  

效果:跟SetFromToRotation差不多,区别是可以返回一个Quaternion。通常用来让transform的一个轴向(例如 y)toDirection在世界坐标中同步。

 

9static function LookRotation (forward :Vector3, upwards :Vector3 =Vector3.up) : Quaternion

效果:跟SetLootRotation差不多,区别是可以返回一个Quaternion

 

10static function Slerp (from : Quaternion, to : Quaternion, t :float) : Quaternion 

  1. public Transform from;  
  2. public Transform to;  
  3. public float speed = 0.1F;  
  4. void Update() {  
  5. transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, Time.time * speed);  
  6. }  

效果:球面插值,插值不是等角速度的,而是变速的。 会有越来越慢的感觉。

 

11static function Lerp (from : Quaternion, to : Quaternion, t :float) : Quaternion 

效果:与Slerp效果差不多,效率比Slerp高但是如果fromto相差过大效果会不好,会返回一个标准化的Quaternion

 

12static function RotateTowards (from : Quaternion, to : Quaternion, maxDegreesDelta :float) : Quaternion

效果:旋转一个角度从fromto。与Slerp基本相同,但这个函数的角速度永远不会超过maxDegreesDelta。负的maxDegreesDelta值将使旋转远离to

 

13static function Inverse (rotation : Quaternion) : Quaternion 

效果:返回反向的旋转。

 

14static function Angle (a : Quaternion, b : Quaternion) :float

  1. public Transform target;  
  2. void Update(){  
  3. float angle = Quaternion.Angle(transform.rotation, target.rotation);  
  4. }  

 

效果:返回ab两者之间的角度。

 

15static function Euler (x :float, y :float, z :float) : Quaternion .

[csharp] view plaincopy

  1. public Quaternion rotation = Quaternion.Euler(0, 30, 0);  

效果:把旋转角度变成对应的Quaternion

 

三,实例

  1. var target : Transform;  
  2. var edgeBorder = 0.1;  
  3. var horizontalSpeed = 360.0;  
  4. var verticalSpeed = 120.0;  
  5. var minVertical = 20.0;  
  6. var maxVertical = 85.0;  
  7. private var x = 0.0;  
  8. private var y = 0.0;  
  9. private var distance = 0.0;  
  10. function Start()  
  11. {  
  12. x = transform.eulerAngles.y;  
  13. y = transform.eulerAngles.x;  
  14. distance = (transform.position - target.position).magnitude;  
  15. }  
  16. function LateUpdate()  
  17. {  
  18. var dt = Time.deltaTime;  
  19. x -= Input.GetAxis("Horizontal") * horizontalSpeed * dt;  
  20. y += Input.GetAxis("Vertical") * verticalSpeed * dt;  
  21. y = ClampAngle(y, minVertical, maxVertical);  
  22. var rotation = Quaternion.Euler(y, x, 0);  
  23. var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;  
  24. //print(rotation + " && " + rotation * Vector3(0.0, 0.0, -distance));  
  25. transform.rotation = rotation;  
  26. transform.position = position;  
  27. }  
  28. static function ClampAngle (angle : float, min : float, max : float) {  
  29. if (angle < -360)  
  30. angle += 360;  
  31. if (angle > 360)  
  32. angle -= 360;  
  33. return Mathf.Clamp (angle, min, max);  
  34. }  

 

  效果是可以通过键盘使摄像机围绕一点自由旋转,类似于在一个球面上运动。

  1. var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;  

 

posted @ 2015-01-05 21:33  cvtv  阅读(222)  评论(0)    收藏  举报