Jump to content
  • Advertisement
Sign in to follow this  

Camera transformation not working

This topic is 2239 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I hope this is the correct place to post this question. I've done a decent amount of searching and havn't been able to identify what my problem is and the many different solutions/issues I've read about online don't seem to apply, or maybe I just don't understand the problem well enough to see the solutions.

So I have objects and each object has it's own 4x4 matrix I use to keep track of it's current translation and orientation.

I compute all my transformations using matrices. So say I have a cube on the screen and I want to rotate it 90 degrees I do the following:

[source lang="cpp"]
void Transform::RotateLocalY(float radians)
matrix4x4 curRot;
curRot.m11 = _transform.m11;
curRot.m21 = _transform.m21;
curRot.m31 = _transform.m31;
curRot.m12 = _transform.m12;
curRot.m22 = _transform.m22;
curRot.m32 = _transform.m32;
curRot.m13 = _transform.m13;
curRot.m23 = _transform.m23;
curRot.m33 = _transform.m33;

matrix4x4 rotation = RotationMatrixY(radians);
rotation = rotation * curRot;

vector3 trans = GetTranslation();
_transform = rotation;
_transform.m14 = trans.x;
_transform.m24 = trans.y;
_transform.m34 = trans.z;

I'm confident in my math in so far that everything seems to work.

I can create a cube rotate it freely along all axis and move it 'forward' along either of it's axis. Everything looks fine and great.

The problem comes in when I try to do the same thing to my camera. My camera inherits from a transform, so it has it's own _transform matrix it uses and I believe I should be able to do all the same types of transformations.

Each time I render an object, I set a uniform on my shader which is supposed to be combination of the camera's view matrix and the objects current transformation matrix.

[source lang="cpp"]modelViewMatrix = modelViewMatrix * renderable->GetTransformMatrix();[/source]
This value changes in the shader for each object being rendered.

All of this appears to work great like I said, until I start rotating my camera. Camera translation appears to be fine too.

Here is how I compute my camera's view matrix from it's Transformation matrix (being manipulated exactly like any object in the scene)

[source lang="cpp"] matrix4x4 view;

view.m11 = _transform.m11;
view.m12 = _transform.m21;
view.m13 = _transform.m31;
view.m14 = 0.0f;

view.m21 = _transform.m12;
view.m22 = _transform.m22;
view.m23 = _transform.m32;
view.m24 = 0.0f;

view.m31 = _transform.m13;
view.m32 = _transform.m23;
view.m33 = _transform.m33;
view.m43 = 0.0f;

view.m14 = -_transform.m14;
view.m24 = -_transform.m24;
view.m34 = -_transform.m34;
view.m44 = 1.0f;

return view;[/source]

Basically, I transpose the 3x3 orientation portion of the Transform and set a negative translation (not transposed). Also, all my matrices are represented in column order, including the variable names of each place. So m32 means column 3 row 2.

What's not working, exactly is when I rotate it appears that all the objects in the scene rotate around 0,0,0. So If i translate my camera way far out and start rotating my camera, I can see the cube rotating around the center of the scene. Up close it's hard to tell and even appears to be correct.

After going through numerous resources, I can't tell what I'm doing wrong. I don't understand why my translation appears to work but rotations don't.

I have a feeling it might be me confusing something between what I've read and how I implement my matrices, but everything except this one thing seems to work and I'm not sure I understand enough to determine why or what is wrong.

Any help or hints in the right direction would be much appreciated. If you'd like and have visual studio 10 you can download the source and run to see what I mean from:


(If you do, be sure to set your working directory in project settings to $(OutDir) and set startup project to the test project)

Share this post

Link to post
Share on other sites
Ok I found the problem(s), in case anyone else runs into something similar here's what happened:

My matrix multiplication function was broken. There was a copy paste error and it only showed up when translating the camera in the Y. Objects worked fine because of the error, it only showed up depending on the order of multiplication, so my objects translated in the Y fine, but the camera never did. When the view was multiplied against the object's transform is when I noticed that something was wrong.

After fixing that, my matrix multiplication was also backwards, this all stemmed from when I first wrote it and didn't have any practical way to test it. Long story short, figure out your colomn/row ordering and what it means for the rest of your program and stick to it. Otherwise you can screw things up or get confused trying to translate between the two and explanations you read online.

My camera still didn't work after that, even after I got all the matrix manipulations sorted out.

For a standard fps camera, you want to rotate your camera object around it's local X axis for looking up and down, but for looking right and left you want to rotate the camera AROUND the world center and not around it's own Y axis, If you just rotate around your local Y axis, you're twisting the orientation of the world and you quickly see it doesn't look right, you end up distorting what's up and down. If you play w/ driving a cube around on it's local axis as if it were the camera, you can see how the cube will twist which makes sense. It's easier to visualize that way anyways.

The rest of my assumptions, about creating the view and everything else was correct it seems. Just be careful when translating what you need to do from online resources into how you're actually representing things. I think that's where my problems came from initially.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!