3

I am trying to write a lookat function that uses glm::quat to represent rotations, based of off this answer. I am running into trouble getting a correct angle however. This is my lookat function:

void Camera::LookAt(float x, float y, float z) {
    glm::vec3 lookVector = glm::vec3(x, y, z);
    assert(lookVector != position);

    glm::vec3 direction = glm::normalize(lookVector-position);
    float dot = glm::dot(glm::vec3(0, 0, -1), direction);
    if (fabs(dot - (-1.0f)) < 0.000001f)
        rotation = glm::quat(RadiansToDegrees(M_PI), 0.0f, 1.0f, 0.0f);
    if (fabs(dot - (1.0f)) < 0.000001f)
        rotation = glm::quat();

    float angle = RadiansToDegrees(acosf(dot));

    glm::vec3 cross = (glm::cross(glm::vec3(0, 0, -1), direction));
    rotation = glm::normalize(glm::angleAxis(angle, cross));

    std::cout << glm::eulerAngles(rotation).x  << " " << glm::eulerAngles(rotation).y << " " << glm::eulerAngles(rotation).z << "\n";
}

When I call LookAt(0.0f, 0.0f, 0.0f) when my camera is at (0.0f, 0.0f, -10.0f), this outputs a correct rotation of 0,0,0. However if I translate my camera to (0.0f, -0.01f, -10.0f) or more I get a rotation of about 124,0,0. This goes down if I continue to translate y by -0.01f. If I do not normalize the quaternion I do not get this problem. The rotation is still 124 about the x axis, but the appearance is fine. If however I normalize the quaternion later it once again appears to rotate to about 124. I can not normalize cross, because doing so throws an assert. What would cause me to get euler angles of 124 about x from my lookat function, and how can I fix it?

7
  • As the GLM documentation says, the axis for glm::angleAxis must be normalized, which it is not in your case. What kind of assert is thrown when you try to normalize cross? As long as direction is not parallel to (0, 0, -1) there should be no problem. Aug 11, 2013 at 14:00
  • Debugging the code, I just realized that cross is (0.0f, 0.0f, 0.0f) which explains the assert which is thrown from inversesrt.
    – jbills
    Aug 11, 2013 at 14:05
  • Ah.. In this case dot == -1 and rotation is already set. However, you try to calculate it again, which makes no sense. So just add an else block. Aug 11, 2013 at 14:09
  • Now rotation is about (180, 0, 0). This is still not looking at (0,0,0)
    – jbills
    Aug 11, 2013 at 14:19
  • That's what you defined in the first case. You probably wanted to use angleAxis() again instead of the pure constructor. Aug 11, 2013 at 14:37

2 Answers 2

8

Since version 0.9.9.0 there is a function in <glm/gtc/quaternion.hpp> doing mostly what you want:

template<typename T, qualifier Q>
tquat<T, Q> quatLookAt(vec<3, T, Q> const& direction, vec<3, T, Q> const& up);

It was added by this pull request and has been merged into master July 24, 2017.

But:

  1. direction has to be a normalized vector!
  2. direction can't be parallel to up!

So you may want to write a safer wrapper around the function:

glm::quat safeQuatLookAt(
    glm::vec3 const& lookFrom,
    glm::vec3 const& lookTo,
    glm::vec3 const& up,
    glm::vec3 const& alternativeUp)
{
    glm::vec3  direction       = lookTo - lookFrom;
    float      directionLength = glm::length(direction);

    // Check if the direction is valid; Also deals with NaN
    if(!(directionLength > 0.0001))
        return glm::quat(1, 0, 0, 0); // Just return identity

    // Normalize direction
    direction /= directionLength;

    // Is the normal up (nearly) parallel to direction?
    if(glm::abs(glm::dot(direction, up)) > .9999f) {
        // Use alternative up
        return glm::quatLookAt(direction, alternativeUp);
    }
    else {
        return glm::quatLookAt(direction, up);
    }
}
2
  • If the direction and up vector are parallel, then what should alternative up be?
    – Tom
    Oct 26, 2021 at 5:31
  • 1
    @TomH Literally any vector that isn't parallel to up. I often just use a vector along the X or Z-Axis if I think "up" and "direction" will rarely become parallel. For game characters, a vector pointing to the back of the character is a good choice, since "alternativeUp" is basically what the top of their head will point to when they look straight upwards. Oct 28, 2021 at 20:40
2

I have fixed the problem with the following code:

void Camera::LookAt(float x, float y, float z) {
    glm::vec3 lookVector = glm::vec3(x, y, z);
    assert(lookVector != position);

    glm::vec3 direction = glm::normalize(lookVector-position);
    float dot = glm::dot(glm::vec3(0, 0, 1), direction);
    if (fabs(dot - (-1.0f)) < 0.000001f) {
        rotation = glm::angleAxis(RadiansToDegrees(M_PI), glm::vec3(0, 1, 0));
        return;
    }
    else if (fabs(dot - (1.0f)) < 0.000001f) {
        rotation = glm::quat();
        return;
    }

    float angle = -RadiansToDegrees(acosf(dot));

    glm::vec3 cross = glm::normalize(glm::cross(glm::vec3(0, 0, 1), direction));
    rotation = glm::normalize(glm::angleAxis(angle, cross));
}

I do not however understand the necessity of the negative on angle. It fixed the last of my problems, and an explanation of the math of why would be helpful.

1
  • That's because your axis points in the wrong direction. You could also do cross = glm::normalize(glm::cross(direction, glm::vec3(0, 0, 1))). Btw, glm::angleAxis() probably already returns a normalized quaternion. Aug 12, 2013 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.