Can't limit the camera pitch angle between [-90º, 90º] in OpenGL using vectors!

后端 未结 3 771
终归单人心
终归单人心 2021-01-15 14:19

I\'m having a big problem limiting the camera pitch angle (between -90º and 90º) with the code below. This is somewhat a follow up to this question.

The problem, it

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-15 14:46

    After while I come up with a solution which is pretty simple and straightforward. Like it was said many times in the answers and comments, the problem lies when the forward vector (my Reference) is looking straight up or down, which means this vector is parallel to WORLD_SKY_VECTOR and that's reason it gets all messy.

    My thought behind this solution is that when I'm looking straight down (or up) and I want to rotate left or right, this is actually a roll rotation around the forward vector. Why not execute a roll movement when the pitch angle is at -90º or 90º instead?

    Putting that together, I solved the problem by simply replacing the yaw/heading rotation with the following code:

    if(angle.y != 0.0f) {
        if(abs(accumPitchAngle) == 90.0f) {
            RightVector = RotateArbitraryAxis(RightVector, Reference, -angle.y);
            UpVector = Vector3D::CrossProduct(RightVector, Reference);
        } else {
            Reference = RotateArbitraryAxis(Reference, WORLD_SKY_VECTOR, angle.y);
            RightVector = Vector3D::CrossProduct(Reference, WORLD_SKY_VECTOR);
            UpVector = Vector3D::CrossProduct(RightVector, Reference);
        }
    }
    

    This seems a fairly simple and straightforward working solution, I'll probably be marking my answer as accepted unless someone points out any strong problem against this implementation that I may not be considering. I'll wait a few hours before doing it, so that the previous answers reach some sort of conclusion.

提交回复
热议问题