Alex
Alex

Reputation: 632

How do I convert these 3D camera trig equations to work on a new axis

The following function calculates the target vector of my FPS camera to put in the OpenGL LookAt method. Camera orientation (in radians) (0,0,0) means the camera is parallel to the z axis looking in the negative direction and the camera right vector is parallel to the x axis in the positive direction.

    static Matrix4 GetViewMatrix()
    {
        Vector3 cameraup = Vector3.Transform(Vector3.UnitY,(Quaternion.FromAxisAngle(LineOfSightVector, Orientation.Z)));
        LineOfSightVector.X = (float)(Math.Sin((float)Orientation.X) * Math.Cos((float)Orientation.Y));
        LineOfSightVector.Y = (float)Math.Sin((float)Orientation.Y);
        LineOfSightVector.Z = (float)(Math.Cos((float)Orientation.X) * Math.Cos((float)Orientation.Y));
        return Matrix4.LookAt(Position, Position + LineOfSightVector, cameraup) * View;  //View = createperspectivefield of view matrix4
    }

It works fine when the camera y axis is (0,1,0). However I have introduced a Z value to my camera orientation (roll). I use that to get the "cameraup" vector. I now need to adjust the 3 trig equations for the LineOfSightVector to take into account the change in the "up" vector so the camera controls go in the right direction. Can someone please advise me on this.

Thanks

Upvotes: 0

Views: 93

Answers (1)

Ramil Kudashev
Ramil Kudashev

Reputation: 1175

Having

lineOfSight = vec3(sin(phi)*cos(ksi), sin(ksi), cos(phi)*cos(ksi));

you could compute right and up directions as follows:

right = vec3(cos(phi)*cos(ksi), 0, -sin(phi)*cos(ksi));
up = cross(lineOfSight, right);
up = normalize(up);

Notice that in such model cases of cos(ksi) == 0 should be handled separately.

Upvotes: 1

Related Questions