关于四元数的使用

(1)当一个物体和世界坐标的角度不一样时,想要绕自己本身的方向旋转

乘法顺序决定了绕世界还是自己

绕本地轴进行旋转:transform.rotation = transform.rotation * Quaternion;
绕世界轴进行旋转:transform.rotation = Quaternion * transform.rotation;

(2)四元数 * 四元数

两个旋转量的叠加 相当于旋转

 Quaternion r = Quaternion.AngleAxis(20, Vector3.up);
 this.transform.rotation = this.transform.rotation * r;

设置一个四元数 以y轴方向旋转20度 我们想让绕本地轴进行旋转 就乘在前面 这样就相当于 在本地轴的基础上绕y轴旋转了20度

(3)四元数 * 向量 (还没用明白)

返回一个新向量 相当于旋转向量

(4)Quaternion.AngleAxis(角度,轴)

注意:轴 Vector3.right是以自己的坐标系右侧 并不是世界坐标系右侧

(5)实现飞机的左右旋转 并自动归位

注意:当归位时 本地角度不能与初始角度相乘 四元数*四元数=叠加角度 假设本地角度为30度 初始角度也为30度 30度叠加30度 无限旋转

但是当旋转时 是可以相乘 因为 本地角度*旋转角度 相当于在本地角度的基础上进行旋转角度

//记录初始角度
Quaternion start;
Quaternion rotU;
    void Start()
    {
        start = this.transform.rotation;
    }

    void Update()
    {
        
        float h = Input.GetAxis("Horizontal");
        //当AD键没有按下的时候 我们就让当前的角度缓慢变为初始角度 实现了归位
        if (h == 0)
        {
            this.transform.rotation = Quaternion.Slerp(this.transform.rotation, start, Time.deltaTime);
        }
        //当AD键盘按下的时候 我们就判断是左边还是右边
        else
        {
            rotU = h > 0 ? Quaternion.AngleAxis(-20 * Time.deltaTime, Vector3.forward) : Quaternion.AngleAxis(20 * Time.deltaTime, Vector3.forward);
            //让飞机本地角度 * 旋转角度 就实现了飞机旋转
            this.transform.rotation = this.transform.rotation * rotU;
        }
    }

Eigen库是一个C++的线性代数库,它提供了对四元数的支持。在Eigen中,四元数是以Quaternion类的形式表示的。下面是使用Eigen库进行四元数操作的示例代码: ```c++ #include <Eigen/Dense> #include <iostream> using namespace Eigen; int main() { // 创建一个四元数对象 Quaterniond q(1, 0, 0, 0); // w, x, y, z // 输出四元数对象的实部和虚部 std::cout << "Real part: " << q.w() << std::endl; std::cout << "Imaginary part: " << q.vec().transpose() << std::endl; // 旋转一个向量 Vector3d v(1, 0, 0); Vector3d v_rotated = q * v * q.inverse(); std::cout << "Rotated vector: " << v_rotated.transpose() << std::endl; // 将旋转轴和旋转角度转换为四元数 Vector3d axis(0, 0, 1); double angle = M_PI / 2; Quaterniond q_axis_angle(cos(angle / 2), sin(angle / 2) * axis.x(), sin(angle / 2) * axis.y(), sin(angle / 2) * axis.z()); // 两个四元数的乘积等于它们对应的旋转的复合 Quaterniond q_combined = q * q_axis_angle; std::cout << "Combined quaternion: " << q_combined.w() << " " << q_combined.vec().transpose() << std::endl; // 将四元数转换为旋转矩阵 Matrix3d R = q.toRotationMatrix(); std::cout << "Rotation matrix: " << std::endl << R << std::endl; return 0; } ``` 这个示例代码中,我们创建了一个四元数对象q,并输出了它的实部和虚部。然后我们将一个向量v绕q旋转,并输出旋转后的向量。接着我们将一个旋转轴和一个旋转角度转换为一个四元数对象q_axis_angle,并将两个四元数的乘积q_combined输出。最后,我们将四元数对象q转换为一个旋转矩阵R,并输出这个矩阵。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值