Sehaj Shergill
Sehaj Shergill

Reputation: 23

Rotating a Camera in Microsoft XNA Framework (MonoGame)

I'm trying to create a First Person Controller in XNA (MonoGame). However, I'm unsure on how I can rotate my 3D Camera

I've been told to make a Rotation Matrix but I'm unsure on how I can implement that into my Camera.

I've tried the following:

Matrix rotation = Matrix.CreateRotationX(MathHelper.ToRadians(1f));

I'm not sure on how I would add this to my Camera so I can rotate my Camera Left and Right.

Any Suggestions?

Upvotes: 2

Views: 93

Answers (1)

Rafloka
Rafloka

Reputation: 366

Usually, you'll send a projection, a view and sometimes a model matrix to your shader(s) (Effect/BasicEffect) before rendering anything. The view matrix is basically representing your camera. I assume, you're using BasicEffect and have already created a projection matrix.

You can use:

var Rotation = Quaternion.CreateFromYawPitchRoll(XAxisAngleHere, YAxisAngleHere, 0);

Where X (Yaw) is up and down rotation and Y (Pitch) is left and right rotation (if y is your upwards axis in your projection matrix). These would then be controlled with your mouse movement (mouse X controls Y, mouse Y controls X) for a first person controller.

For a 'complete' camera representing view matrix you can do something like:

var Rotation = Quaternion.CreateFromYawPitchRoll(XRotation, YRotation, 0);
var CameraWorld = Matrix.CreateWorld(CameraPositionInWorld, Vector3.Transform(Vector3.Forward, Rotation), Vector3.Transform(Vector3.Up, Rotation));
var View = Matrix.Invert(CameraWorld);

Here, we first calculate a world matrix representing the camera's position and rotation in our 3D world. For a correct view matrix, we then have to invert that camera world matrix before assigning it to a shader's (BasicEffect) View parameter.

Let me know if you need more information!

Upvotes: 0

Related Questions