Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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);
share|improve this answer
    
Thanks! Worked out –  bufferz Oct 31 '10 at 18:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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