关于pitch、roll、 yaw的图解和Laya.Quaternion.createFromYawPitchRoll用法
首先说明一下:
transform.rotation
获取的是四元数,是从欧拉角转换过的,所以要想直接设置transform.rotation
用欧拉角是不行的,得转换一下
createFromYawPitchRoll用法
最近学laya 3D,看到一个API Laya.Quaternion.createFromYawPitchRoll
,看下代码,可以依靠轴角到四元数的公式进行推导,用法就很清楚了:
/**
* 从欧拉角生成四元数(顺序为Yaw、Pitch、Roll)
* @param yaw yaw值
* @param pitch pitch值
* @param roll roll值
* @param out 输出四元数
*/
static createFromYawPitchRoll(yaw: number, pitch: number, roll: number, out: Quaternion): void {
var halfRoll: number = roll * 0.5;
var halfPitch: number = pitch * 0.5;
var halfYaw: number = yaw * 0.5;
var sinRoll: number = Math.sin(halfRoll);
var cosRoll: number = Math.cos(halfRoll);
var sinPitch: number = Math.sin(halfPitch);
var cosPitch: number = Math.cos(halfPitch);
var sinYaw: number = Math.sin(halfYaw);
var cosYaw: number = Math.cos(halfYaw);
out.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
out.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
out.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
out.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
}
用法:
let tempLocalRotation: Laya.Quaternion = new Laya.Quaternion();
Laya.Quaternion.createFromYawPitchRoll(this.yawPitchRoll.x, this.yawPitchRoll.y, this.yawPitchRoll.z, tempLocalRotation);
this.camera.transform.localRotation = tempLocalRotation;
关于pitch、 roll、 yaw的示意图
ψ、θ、φ
分别为绕Z轴、Y轴、X轴的旋转角度,如果用Tait-Bryan angle表示,分别为Yaw、Pitch、Roll
后面我找了张图,描述不太一样,但你知道就好了:
- yaw是围绕Y轴旋转
- pitch是围绕X轴旋转
- roll是围绕Z轴旋转
下面这张,黄色的是y轴: