在一个轴上旋转一个四元数? [英] rotating a quaternion on 1 axis?

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

问题描述

我有一个由四元数旋转的模型。我只能设置旋转,我不能从任何东西中加或减。我需要得到一个轴的值​​,并添加一个角度(可能是度数或弧度?),然后重新添加修改的四元数。



我可以这样做吗? (在每个轴上的答案)。

解决方案

您可以将两个四元数相乘以产生第三个四元数,旋转。请注意,四元数乘法不是可交换的,这意味着顺序很重要(如果你在脑子里做了几次,你可以看到为什么)。

你可以生成一个四元数代表一个围绕特定轴的给定角度的旋转,像这样的东西(原谅它是c ++,而不是java):

  Quaternion Quaternion :: create_from_axis_angle(const double& xx,const double& yy,const double& zz,const double& a)
{
//这里我们计算sin(theta / 2)一次优化
double factor = sin(a / 2.0);

//计算四元数的x,y和z
double x = xx * factor;
double y = yy * factor;
double z = zz * factor;

//通过cos(theta / 2)计算w值w
double w = cos(a / 2.0);

返回Quaternion(x,y,z,w).normalize();

$ / code>

例如,要围绕x轴旋转,可以创建一个四元数 createFromAxisAngle(1,0,0,M_PI / 2)并将其乘以模型的当前四元数。

I have a model rotated by a quaternion. I can only set the rotation, I can't add or subtract from anything. I need to get the value of an axis, and than add an angle to it (maybe a degree or radian?) and than re-add the modified quaternion.

How can I do this? (answer on each axis).

解决方案

You can multiply two quaternions together to produce a third quaternion that is the result of the two rotations. Note that quaternion multiplication is not commutative, meaning order matters (if you do this in your head a few times, you can see why).

You can produce a quaternion that represents a rotation by a given angle around a particular axis with something like this (excuse the fact that it is c++, not java):

Quaternion Quaternion::create_from_axis_angle(const double &xx, const double &yy, const double &zz, const double &a)
{
    // Here we calculate the sin( theta / 2) once for optimization
    double factor = sin( a / 2.0 );

    // Calculate the x, y and z of the quaternion
    double x = xx * factor;
    double y = yy * factor;
    double z = zz * factor;

    // Calcualte the w value by cos( theta / 2 )
    double w = cos( a / 2.0 );

    return Quaternion(x, y, z, w).normalize();
}

So to rotate around the x axis for example, you could create a quaternion with createFromAxisAngle(1, 0, 0, M_PI/2) and multiply it by the current rotation quaternion of your model.

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

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