Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
ckoeber's avatar
ckoeber
Honored Guest
11 years ago

Simple Rotation Question...

Hello,

Thanks to various members of the forum here I got 90% of what I need. Thank you.

I am just trying to resolve one last bit and I hope someone can point me to any easy errors I am making but not seeing.

My code works except when a person looks left the camera actually rotates right. Vice versa with looking right, camera rotates left.

Any idea what I am doing wrong here? Thank you:

Here is how I get head orientation.

OVR::Quatf OculusRiftOrientation = PredictedPose.Orientation;
glm::vec3 CurrentEulerAngles;
glm::quat CurrentOrientation;

OculusRiftOrientation.GetEulerAngles<OVR::Axis_X, OVR::Axis_Y, OVR::Axis_Z,
OVR::Rotate_CW, OVR::Handed_R>
(&CurrentEulerAngles.x, &CurrentEulerAngles.y, &CurrentEulerAngles.z);

CurrentOrientation = glm::quat(CurrentEulerAngles);


And here is how I calculate the LookAt:


/*

DirectionOfWhereCameraIsFacing is calculated from mouse and standing position so that you are not constantly rotating after you move your head.

*/

glm::vec3 DirectionOfWhereCameraIsFacing;
glm::vec3 RiftDirectionOfWhereCameraIsFacing;
glm::vec3 RiftCenterOfWhatIsBeingLookedAt;
glm::vec3 PositionOfEyesOfPerson;
glm::vec3 CenterOfWhatIsBeingLookedAt;
glm::vec3 CameraPositionDelta;

RiftDirectionOfWhereCameraIsFacing = DirectionOfWhereCameraIsFacing;
RiftDirectionOfWhereCameraIsFacing = glm::rotate(CurrentOrientation, DirectionOfWhereCameraIsFacing);
PositionOfEyesOfPerson += CameraPositionDelta;
CenterOfWhatIsBeingLookedAt = PositionOfEyesOfPerson + DirectionOfWhereCameraIsFacing * 1.0f;
RiftCenterOfWhatIsBeingLookedAt = PositionOfEyesOfPerson + RiftDirectionOfWhereCameraIsFacing * 1.0f;

RiftView = glm::lookAt(PositionOfEyesOfPerson, RiftCenterOfWhatIsBeingLookedAt, DirectionOfUpForPerson);


Thank you for any help you can provide for this last piece.

4 Replies

  • "ckoeber" wrote:
    My code works except when a person looks left the camera actually rotates right. Vice versa with looking right, camera rotates left.


    The first thing I would try is negating CurrentEulerAngles.y.

    Note that GetEulerAngles returns a set of 3 rotations that get applied in the order of its template arguments.

    "ckoeber" wrote:
    OculusRiftOrientation.GetEulerAngles<OVR::Axis_X, OVR::Axis_Y, OVR::Axis_Z,
       OVR::Rotate_CW, OVR::Handed_R>
       (&CurrentEulerAngles.x, &CurrentEulerAngles.y, &CurrentEulerAngles.z);


    This code assumes you want to rotate around the X axis by CurrentEulerAngles.x radians, then around the Y axis, and then around the Z axis. Typically you rotate about Y, then X, then Z. I believe that's the case for GLM.
  • I would avoid using Euler angles as an intermediary and convert directly from the OVR quaternion type to the GLM equivalent with glm::make_quat().


    I have a set of functions that do this here:



    static inline glm::mat4 fromOvr(const ovrFovPort & fovport, float nearPlane = 0.01f, float farPlane = 10000.0f) {
    return fromOvr(ovrMatrix4f_Projection(fovport, nearPlane, farPlane, true));
    }

    static inline glm::mat4 fromOvr(const ovrMatrix4f & om) {
    return glm::transpose(glm::make_mat4(&om.M[0][0]));
    }

    static inline glm::vec3 fromOvr(const ovrVector3f & ov) {
    return glm::make_vec3(&ov.x);
    }

    static inline glm::uvec2 fromOvr(const ovrSizei & ov) {
    return glm::uvec2(ov.h, ov.w);
    }

    static inline glm::quat fromOvr(const ovrQuatf & oq) {
    return glm::make_quat(&oq.x);
    }

    static glm::mat4 Rift::fromOvr(const ovrPosef & op) {
    return glm::mat4_cast(fromOvr(op.Orientation)) * glm::translate(glm::mat4(), Rift::fromOvr(op.Position));
    }
  • ckoeber's avatar
    ckoeber
    Honored Guest
    Thanks for all of the help. I need to brush up on math...
  • The view matrix doesn't always behave the way people think. If you see the camera as something stationary, you turn by rotating the whole world instead of turning the camera. That explains the signs of the rotations.