1

I'm trying to get a lookAt function to work, and I've had some success:

    public virtual void LookAt(Vector3 point)
    {
        Vector3 fw = Forward;
        Vector3 delta = (point - Position).Normalized();
        if (delta.LengthSquared > 0)
        {
            Vector3 axis = Vector3.Cross(delta, fw);
            float angle = Vector3.CalculateAngle(delta, fw);
            if (angle > 0)
            {
                angle *= -Math.Sign(Vector3.Dot(Vector3.Cross(axis, fw), delta));
                Rotation *= Quaternion.FromAxisAngle(axis.Normalized(), angle);
            }
        }
    }

This works, but it can't take into account that I don't want any roll. So the direction is correct, but I need to fix the roll of my camera. Now, I know I can rotate it by creating a Quaternion from AxisAngle, but I don't know how to get the angle between the current and upright position!?

edit: Using matrix.lookAt now and converting to quaternion.

pixartist
  • 1,137
  • 2
  • 18
  • 40

1 Answers1

2

Such kind of operation better to perform with matrixes Calculating a LookAt matrix

Than convert back to quaternion.

If you want to perform it with quaternions, it can be done using 2 call to "shortest_arc" described http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors

First call to rotate by DIR vector and second call to compensate roll from rotated UP vector to desired "up".

Community
  • 1
  • 1
minorlogic
  • 1,872
  • 1
  • 17
  • 24