Quaternions, rotate a model and align with a direc

2019-08-03 23:02发布

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

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.

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();

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

Any help? Thanks ;-)

3条回答
时光不老,我们不散
2楼-- · 2019-08-03 23:25

Your code's a bit funky.

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

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.

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. Quaternions are not commutative. I've assumed that your quaternions are rotating column vectors; so I put deltaQ on the left. It's not clear what your *= operator is doing.
  2. It's important to regularly normalize your quaternions after multiplication. Otherwise small errors accumulate and they drift away from unit length causing all manner of grief.
查看更多
We Are One
3楼-- · 2019-08-03 23:33

This is a shortcut I've used to get the quaternion for lock-on-target rotation:

Matrix rot = Matrix.CreateLookAt(_arrow.Position, _cube.Position, Vector3.Down);
_arrow.Rotation = Quaternion.CreateFromRotationMatrix(rot);

For this example, I'm rendering an arrow and a cube, where the cube is moving around in a circle, and with the above code the arrow is always pointing at the cube. (Though I imagine there are some edge cases when cube is exactly above or below).

enter image description here

Once you get this quaternion (from spaceship to target), you can use Quaternion.Lerp() to interpolate between current ship rotation and the aligned one. This will give your rotation a smooth transition (not just snap to target).


Btw, might be that your rotation gets reduced to zero because you're using *= when assigning to it.

查看更多
Rolldiameter
4楼-- · 2019-08-03 23:33

OMG! It worked!!!

            Vector3 targetRay = Vector3.Normalize(_target._ray.Position - _obj._ray.Position);
        Vector3 objectRay = Vector3.Normalize(_obj._ray.Direction);
        float angle = (float)Math.Acos(Vector3.Dot(targetRay, objectRay));

        if (angle!=0)
        {
            Vector3 ortho = Vector3.Normalize(Vector3.Cross(objectRay, targetRay));
            _obj._rotationQuaternion = Quaternion.CreateFromAxisAngle(ortho, angle) * _obj._rotationQuaternion;
            _obj._rotationQuaternion.Normalize();
        }
        _obj.UpdateMatrix();

Thank you very much JCooper!!!

And niko I like the idea of Lerp ;-)

查看更多
登录 后发表回答