正交基的四元数 [英] Quaternion from Orthogonal Basis

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

问题描述

我有一个沿速度矢量移动的弹丸对象.我需要确保对象始终面向速度矢量的方向.此外,我使用四元数而不是矩阵来表示对象旋转.

I have a projectile object that is moving along a velocity vector. I need to ensure that the object is always facing in the direction of the velocity vector. Furthermore, I am representing object rotation using quaternions, not matrices.

我知道第一步是找到正交基:

I know that the first step is to find an orthogonal basis:

forward = direction of velocity vector
up = vector.new(0, 1, 0)
right = cross(up, forward) 
up = cross(forward, right)

如何将基础转换为旋转四元数?

How might I convert the basis into a rotation quaternion?

解决方案

请注意,我想感谢Noel Hughes提供的答案,但我想以我自己的经验来澄清.伪代码如下:

Note, I'd like to give credit to Noel Hughes for providing the answer, but I want to clarify with my own experiences. Pseudocode follows:

   vec3 vel = direction of velocity vector
   vec3 forward = (1, 0, 0)  // Depends on direction your model faces. See below.
   vec3 axis = cross(forward, vel)
   if (axis == 0) then quit // Already facing the right direction!
   axis = normalize(axis)
   float theta = acos(vel.x/sqrt(vel.x^2, vel.y^2, vel.z^2))
   quat result = (0, axis.y * sin(theta/2), axis.z * sin(theta/2), cos(theta/2)

四元数的最后一个元素是标量部分,前三个元素是虚部.同样,以上伪代码假定您的模型空间"中的对象指向正x轴.就我而言,该对象实际上指向正y轴,在这种情况下,我进行了以下更改:

The last element of the quaternion is the scalar part, the first three elements are the imaginary parts. Also, the above pseudocode assumes that your object in "model space" points down the positive x-axis. In my case, the object actually pointed down the positive y-axis, in which case I made the following changes:

   vec3 vel = direction of velocity vector
   vec3 forward = (0, 1, 0)  // Note that y-component is now 1
   vec3 axis = cross(forward, vel)
   if (axis == 0) then quit 
   axis = normalize(axis)
   float theta = acos(vel.x/sqrt(vel.x^2, vel.y^2, vel.z^2))
   quat result = (axis.x * sin(theta/2), 0, axis.z * sin(theta/2), cos(theta/2)
   // Note that SECOND component above is now 0

推荐答案

我假设您不关心射弹的方向,除了使纵轴与速度矢量对齐之外,并且纵轴是(1,0,0)的x轴.

I assume you don't care about the orientation of your projectile, other than having the longitudinal axis aligned with the velocity vector, and that the longitudinal axis is the x axis of (1, 0, 0).

您处在正确的轨道上.归一化速度矢量(vx,vy,vz)/sqrt(vx ^ 2 + vy ^ 2 + vz ^ 2)与x轴交叉并归一化结果-(0,yn,zn)-这是旋转四元数的轴.旋转角度只是theta = vx/sqrt的反余弦值(vx ^ 2 + vy ^ 2 + vz ^ 2).然后生成的四元数为

You are on the right track. Normalize the velocity vector, (vx, vy, vz)/sqrt(vx^2 + vy^2 + vz^2) cross the x axis with it and normalize the result - (0, yn, zn) - this is the rotation axis for the quaternion. The rotation angle is simply theta = inverse cosine of vx/sqrt(vx^2 + vy^2 + vz^2). The resultant quaternion is then

(0,yn,zn)sn(theta/2)cos(theta/2)

(0, yn, zn)sn(theta/2) cos(theta/2)

如果您有任何疑问,请告诉我.

Let me know if you have any questions.

诺埃尔·休斯(Noel Hughes) nhughes1ster@gmail.com

Noel Hughes nhughes1ster@gmail.com

这篇关于正交基的四元数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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