用四元数绕轴旋转矢量 [英] Rotating a vector around an axis with quaternion

查看:82
本文介绍了用四元数绕轴旋转矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习 3d 编程,现在我正在尝试了解如何使用四元数围绕轴旋转向量.

I am trying to learn 3d programming, and right now I am trying to understand how to use quaternions to rotate a vector around an axis.

据我所知,要绕轴 a 旋转向量 v,将两个向量都转换为四元数后,我们将 v 乘以 a,然后乘以 a 的共轭乘积.

As far as I understand, to rotate a vector v around an axis a, after converting both vectors to quaternions, we multiply v by a, then the product by the conjugate of a.

我想将 v(0,1,0) 围绕 a(1,0,0) 旋转 90 度,我应该得到一个结果向量 v(0,0,1)(或 0,0,-1,取决于旋转的方向).

I want to rotate v(0,1,0) around a(1,0,0) by 90 degrees, and I should get a resulting vector v(0,0,1) (or 0,0,-1, depending on the direction of the rotation).

我没有得到我期望的输出.代码如下:

I am not getting the output I am expecting. Here is the code:

    int main()
    {
        //I want to rotate this vector about the x axis by PI/2 radians:
        Quaternion v(0, 1, 0, 0);
        v.normalize();


        float angle = PI / 2.0f;
        float cos = math::cos(angle / 2.0f);
        float sin = math::sin(angle / 2.0f);

        Quaternion q(1.0f*sin, 0.0f*sin, 0.0f*sin, cos);

        std::cout << "q not normalized = " <<"\t"<< q.x << " " << q.y << " "      << q.z << " " << q.w << std::endl;

        q.normalize();

        std::cout << "q normalized = " <<"\t\t"<< q.x << " " << q.y << " " << q.z << " " << q.w << std::endl;
        std::cout << std::endl;

        Quaternion r;


       //I multiply the vector v by the quaternion v, then I multiply by the    conjugate.
        r = q * v;
        //do I need to normalize here?
        r = r * q.conjugate();
        //and here?


        //shouldn't the resulting vector be 0,0,1? 

        std::cout << "r not normalized = " << "\t" << r.x << " " << r.y << " " << r.z << " " << r.w << std::endl;
        r.normalize();

        std::cout << "r normalized = " << "\t\t" << r.x << " " << r.y << " " << r.z << " " << r.w << std::endl;
        std::cout << std::endl;

        system("pause");
        return 0;
    }

这是输出:

q 未归一化,与 q 归一化相同:x = 0.707107,y = 0,z = 0,w = 0.707107

q not normalized, which is same as q normalized: x = 0.707107, y = 0, z = 0, w = 0.707107

r 未标准化:x = 0.707107,y = 0,z = 1,w = -2.12132r 标准化:x = 0.288675,y = 0,z = 0.408248,w = -0.866025

r not normalized: x = 0.707107, y = 0, z = 1, w = -2.12132 r normalized: x = 0.288675, y = 0, z = 0.408248, w = -0.866025

我做错了什么?我什至从这个过程中明白了什么吗?

what am I doing wrong? did I even understand anything from this process?

推荐答案

基本上是沿 x 轴 (1,0,0) 旋转一个向量,角度为 90 度,使用下面的方法,这对 Euler 和四元数都有效

Basically to rotate an vector along x axis (1,0,0) with angle 90 deg, use below method, this works for both Euler and quaternion

| 1    0           0 |   | 0 |     | 0 |
| 0   cos90   -sin90 | * | 1 |  =  | 0 |
| 0   sin90    cos90 |   | 0 |     | 1 |

阅读关于旋转矩阵http://en.wikipedia.org/wiki/Rotation_matrix

这篇关于用四元数绕轴旋转矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆