四元数,旋转模型,并配合方向 [英] Quaternions, rotate a model and align with a direction

查看:299
本文介绍了四元数,旋转模型,并配合方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有四元数,描述了一个三维模型的旋转。

Suppose you have quaternion that describes the rotation of a 3D Model.

我想要做的是,给定一个对象(与rotationQuaternion,方矢量...)什么,我想,将其调整到目标点。

What I want to do is, given an Object (with rotationQuaternion, side vector...), I want to align it to a target point.

有关宇宙飞船,我想驾驶舱指向一个目标。

For a spaceship, I want the cockpit to point to a target.

下面是一些code我有......这不是在做什么,我想,我不知道为什么...

Here is some code I have ... It's not doing what I want and I don't know why...

        if (_target._ray.Position != _obj._ray.Position)
        {
            Vector3 vec = Vector3.Normalize(_target._ray.Position - _obj._ray.Position);
            float angle = (float)Math.Acos(Vector3.Dot(vec, _obj._ray.Direction));
            Vector3 cross = Vector3.Cross(vec, _obj._ray.Direction);

            if (cross == Vector3.Zero)
                cross = _obj._side;

            _obj._rotationQuaternion *= Quaternion.CreateFromAxisAngle(cross,angle);
        }
        // Updates direction, up, side vectors and model Matrix
        _obj.UpdateMatrix();

一段时间后的rotationQuaternion填充有几乎零处的X,Y,Z和W

after some time the rotationQuaternion is filled with almost Zero at X,Y,Z and W

任何帮助吗? 感谢; - )

Any help? Thanks ;-)

推荐答案

您code是一个有点古怪。

Your code's a bit funky.

if (_target._ray.Position != _obj._ray.Position)
{

这可能会或可能不正确。很显然,你已经覆盖了equals比较。正确的事情是在这里做将确保这两个(单位长度)射线之间的点积接近于1。如果光有相同的起源,那么presumably有平等的立场是指他们重相同。

This may or may not be correct. Clearly, you've overridden the equals comparator. The correct thing be be doing here would be to ensure that the dot-product between the two (unit-length) rays is close to 1. If the rays have the same origin, then presumably have equal 'positions' means they're the same.

  Vector3 vec = Vector3.Normalize(_target._ray.Position - _obj._ray.Position);

这似乎特别错误的。除非减去运营商在陌生的路上被覆盖,减去这样就没有意义了。

This seems particularly wrong. Unless the minus operator has been overridden in a strange way, subtracting this way doesn't make sense.

下面是伪$ C $下我的建议:

Here's pseudocode for what I recommend:

normalize3(targetRay);
normalize3(objectRay);
angleDif = acos(dotProduct(targetRay,objectRay));
if (angleDif!=0) {
  orthoRay = crossProduct(objectRay,targetRay);
  normalize3(orthoRay);
  deltaQ = quaternionFromAxisAngle(orthoRay,angleDif);
  rotationQuaternion = deltaQ*rotationQuaternion;
  normalize4(rotationQuaternion);
}

两件事情需要注意:

Two things to note here:

  1. 在四元数是不可交换的。我认为你的四元数旋转列向量;所以我把deltaQ左边。目前还不清楚你的* =运算符是做。
  2. 重要的是乘法后定期正常化的四元数。否则,小的误差积累,他们从单位长度的渐行渐远造成的悲痛的所有方式。

这篇关于四元数,旋转模型,并配合方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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