本页内容

quaternion QML Value Type

四元数类型具有标量、x、y 和 z 属性。更多内容...

Import Statement: import QtQuick

详细说明

quaternion 类型具有scalar 、x 、y 和z 属性。

要创建一个quaternion 值,可以将其指定为“scalar,x,y,z”字符串,也可以单独定义各分量,或使用Qt.quaternion()函数进行组合。

四元数类型具有以下可在 QML 中调用的幂等函数:

函数签名描述示例
real dotProduct(quaternion other)返回四元数this 与四元数other 的点积结果(实数标量)
var a = Qt.quaternion(1,2,3,4);
var b = Qt.quaternion(5,6,7,8);
var c = a.dotProduct(b);
console.log(c); // 70
四元数 times(四元数 other)返回四元数this 与四元数other 相乘的结果,这相当于同时应用这两个旋转
var a = Qt.quaternion(1 / Math.sqrt(2), 1 / Math.sqrt(2), 0, 0);
var b = Qt.quaternion(1 / Math.sqrt(2), 0, 1 / Math.sqrt(2), 0);
var c = b.times(a);
console.log(c.toString()); // QQuaternion(0.5, 0.5, 0.5, -0.5)
vector3d times(vector3d vector)返回将vector 旋转为this 四元数后的vector3d结果
var a = Qt.quaternion(0.5,0.5,0.5,-0.5);
var b = Qt.vector3d(4,5,6);
var c = a.times(b);
console.log(c.toString()); // QVector3D(5, -6, -4)
四元数 times(实数因子)返回将四元数this 与标量相乘所得的四元数factor
var a = Qt.quaternion(1,2,3,4);
var b = 4.48;
var c = a.times(b);
console.log(c.toString()); // QQuaternion(4.48, 8.96, 13.44, 17.92)
四元数 plus(四元数 other)返回将this 四元数与other 四元数相加所得的四元数结果
var a = Qt.quaternion(1,2,3,4);
var b = Qt.quaternion(5,6,7,8);
var c = a.plus(b);
console.log(c.toString()); // QQuaternion(6, 8, 10, 12)
四元数减法(四元数 other)返回从四元数 `this ` 中减去四元数 `other ` 所得的四元数结果
var a = Qt.quaternion(1,2,3,4);
var b = Qt.quaternion(5,6,7,8);
var c = a.minus(b);
console.log(c.toString()); // QQuaternion(-4, -4, -4, -4)
四元数 normalized()返回四元数this 的归一化单位形式
var a = Qt.quaternion(1,2,3,4);
var b = a.normalized();
console.log(b.toString()); // QQuaternion(0.182574, 0.365148, 0.547723, 0.730297)
四元数 逆(四元数)返回this 四元数的逆元
var a = Qt.quaternion(0.5,0.5,0.5,-0.5);
var b = a.inverted();
console.log(b.toString()); // QQuaternion(0.5, -0.5, -0.5, 0.5)
四元数共轭()返回this 四元数的共轭
var a = Qt.quaternion(1,2,3,4);
var b = a.conjugated()
console.log(b.toString()); // QQuaternion(1, -2, -3, -4)
real length()返回this 四元数长度的实数标量值
var a = Qt.quaternion(1,2,3,4);
var b = a.length();
console.log(b.toString()); // 5.477225575051661
vector3d toEulerAngles()返回与四元数this 对应的vector3d欧拉角(单位为度)
var a = Qt.quaternion(0.933012,0.25,-0.25,0.066987);
var b = a.toEulerAngles();
console.log(b.toString()); // QVector3D(30, -30, -4.28846e-05)
vector4d toVector4d()返回将this 四元数转换为vector4d后的结果
var a = Qt.quaternion(1,2,3,4);
var b = a.toVector4d();
console.log(b.toString()); // QVector4D(2, 3, 4, 1)
bool fuzzyEquals(quaternion other, real epsilon)other如果四元数this 与四元数other 近似相等,则返回true。当this 的每个属性均在epsilon 范围内时,该近似关系成立。请注意,epsilon 是可选参数,其默认值epsilon 为0.00001。
var a = Qt.quaternion(1,2,3,4);
var b = Qt.quaternion(1.0001, 1.9998, 2.0001, 3.9999);
var c = a.fuzzyEquals(b);        // default epsilon
var d = a.fuzzyEquals(b, 0.005); // supplied epsilon
console.log(c + " " + d); // false true

类型为quaternion 的属性其默认值为Qt.quaternion(1, 0, 0, 0) 。

此值类型由QtQuick 导入提供。

另请参阅 QML 值类型。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.