Question

So I want to use quaternions and angles to control my camera using my mouse.

I accumulate the vertical/horizontal angles like this:

void Camera::RotateCamera(const float offsetHorizontalAngle, const float offsetVerticalAngle)
{
    mHorizontalAngle += offsetHorizontalAngle;
    mHorizontalAngle = std::fmod(mHorizontalAngle, 360.0f);

    mVerticalAngle += offsetVerticalAngle;
    mVerticalAngle = std::fmod(mVerticalAngle, 360.0f);
}

and compute my orientation like this:

Mat4 Camera::Orientation() const
{
    Quaternion rotation;
    rotation = glm::angleAxis(mVerticalAngle, Vec3(1.0f, 0.0f, 0.0f));
    rotation = rotation * glm::angleAxis(mHorizontalAngle, Vec3(0.0f, 1.0f, 0.0f));

    return glm::toMat4(rotation);
}

and the forward vector, which I need for glm::lookAt, like this:

Vec3 Camera::Forward() const
{
    return Vec3(glm::inverse(Orientation()) * Vec4(0.0f, 0.0f, -1.0f, 0.0f));
}

I think that should do the trick, but I do not know how in my example game to get actual angles? All I have is the current and previous mouse location in window coordinates.. how can I get proper angles from that?

EDIT: on a second thought.. my "RotateCamera()" cant be right; I am experiencing rubber-banding effect due to the angles reseting after reaching 360 deegres... so how do I accumulate angles properly? I can just sum them up endlessly

Was it helpful?

Solution

Take a cross section of the viewing frustum (the blue circle is your mouse position):

enter image description here

  • Theta is half of your FOV
  • p is your projection plane distance (don't worry - it will cancel out)

From simple ratios it is clear that:

enter image description here

enter image description here

enter image description here

But from simple trignometry

enter image description here

So ...

enter image description here

Just calculate the angle psi for each of your mouse positions and subtract to get the difference.

A similar formula can be found for the vertical angle:

enter image description here

Where A is your aspect ratio (width / height)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top