4
\$\begingroup\$

I'm using XNA to create an FPS camera that uses a Direction vector instead of a Target vector control the camera's orientation.

I'm having trouble with the math for looking up and down when the camera is rotated about the Y axis. No problems looking left and right.

Here's my code:

Vector3 LookDirection {get;set;}

public void LookLeft(float scale)
{
    LookDirection = Vector3.Transform(LookDirection, Matrix.CreateRotationY(MathHelper.ToRadians(scale)));
}

public void LookRight(float scale)
{
    LookDirection = Vector3.Transform(LookDirection, Matrix.CreateRotationY(MathHelper.ToRadians(-scale)));
}

public void LookUp(float scale)
{
    //doesn't work when camera is rotated into the X axis
    LookDirection = Vector3.Transform(LookDirection, Matrix.CreateRotationZ(MathHelper.ToRadians(scale)));
}

public void LookDown(float scale)
{
    //doesn't work when camera is rotated into the X axis
    LookDirection = Vector3.Transform(LookDirection, Matrix.CreateRotationZ(MathHelper.ToRadians(-scale)));
}

I know I need to manipulate both the X and Z components when looking up and down, I just can't figure out exactly what to do. Thanks

\$\endgroup\$
3
\$\begingroup\$

I think you would be better off having separate variables to store the amount of up/down and left/right rotation. These variables should be adjusted by your functions. Then you can calculate your rotation matrix in one place on each frame as in the code below and use that matrix to rotate your LookDirection.

Matrix rotation = Matrix.CreateRotationX(upDownRotation) * Matrix.CreateRotationY(leftRightRotation);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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