#### Archived

This topic is now archived and is closed to further replies.

# Matrix rotation: My camera rotates around the world origin, not its own. Need help.

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

## Recommended Posts

Well I have my simple 3d engine going, nothing special, no lighting, shading, or zbuffer, but hey its my first. I have read all the faqs and such I can get my hands on, and most don't even touch this problem, or at least in the right way. I make a rotation matrix and do matmult to get the result. However this only gives me the relative rotation I want if the camera is at 0, 0, 0. Otherwise im tethered. I have tried making the matrix[3][0-2] the x-z and the matrix[0-2][3] the x-z, niether work right. Since this matrix needs to be multiplyed by the other objects to get the eyespace matrix anyhow. I devised my own method of rotation in place by taking someones formulas they used for rotating vectors, and applieing it to my 3 axis vectors of the camera matrix, and this works, HOWEVER is not accurate and starts warping the matrix after many calculations. _____________________ for(j= 0; j < 3; i++) { matrix[j][0] = matrix[j][0] * rw[0][0] + matrix[j][1] * rw[0][1] + matrix[j][2] * rw[0][2]; matrix[j][1] = matrix[j][0] * rw[1][0] + matrix[j][1] * rw[1][1] + matrix[j][2] * rw[1][2]; matrix[j][2] = matrix[j][0] * rw[2][0] + matrix[j][1] * rw[2][1] + matrix[j][2] * rw[2][2]; } _____________________ where matrix is my object matrix and rw is my rotation matrix((xrotmatrix * yrotmatrix) * zrotmatrix) I need help either: a. keeping the camera at the origin b. a new formula/technique c. cleaning up the math on the above code so that doesn't warp note: I have tried both doubles and floats, doubles deforming at a slower rate. Thanks Edited by - Striker222 on January 2, 2002 1:06:42 AM Edited by - Striker222 on January 2, 2002 1:07:09 AM

##### Share on other sites
The camera stays at the origin looking down the positive z axis because it is in a seperate coordinate space. A 3X3 matrix can only hold the axis vectors for a coordinate space. The standard basis vectors i, j and k are [1 0 0], [0 1 0] and [0 0 1]. You get the identity matrix whether you make those the rows or columns. With other basis vectors that isn''t true and the matrices are transposes of one another. If you make the axes rows then it is a matrix times a column vector. If you make them columns then it is a row vector times a matrix. It is always x time the x axis, y times the y axis and z times the z axis. You can only do a rotation and scaling with a 3X3 matrix. The scaling factor is the magnitude of the axis vector. Assuming you scale and then rotate. Which direction your axes run in the matrix also determines how you combine matrices. I get confused easily, but I think you always add additional transforms to the end opposite the point.

If you want to do translation you need a 4X4 matrix and homogeneous coordinates. Basically in addition to x, y and z you have w and w is always 1. If it is not one after a transform, such as projection, then you have to multiply the vector by the reciprocal of w or in other words divide through by w to normalize the vector. Either the fourth row or column then becomes your translation. If your axes are rows then the translation is the last column, i.e. x * x-axis + x-translation. Now you build your matrix to translate world coordinates into view coordinates. Your translation is the position of the worlds origin in view space, i.e. where does the point (0,0,0) in world coordinates map to. You axes are vectors within view space, i.e. what direction does the x, y and z axes of the world space point with the magnitude being length in view space corresponding to a length of one in world space. You may also have a model space which positions and orients a model within world space.

##### Share on other sites
Try using the matrix stack.

For OpenGL you would do

glPushMatrix();// rotate your objectglPopMatrix();

##### Share on other sites
As for the warping that is most likely the cumulative effect of continually multiplying matrices together, i.e. rotating the rotation matrix. Particularly since a higher precision slows the rate. You have to periodically either re-orthongonalize your axes or rebuild the matrx. Re-orthongonalizing the axes means taking the cross product of two of them, in the right order to find the direction of the third. At that point your new axis is orthogonal to the other two, but the other two may not be orthogonal to each other so you have to do it one more time using your new axis and one of the other two.

##### Share on other sites
Thanks budy. One clarification, are you saying to use a 3x3 matrix to scale and rotate? (3x3 * 4x4)

Or do I strip off the 4th row and collums of the camera matrix temporarily and do a 3x3 * 3x3?

I''ll try this out today.

For the AP - I''m not use opengl im useing a software engine im making.

##### Share on other sites
Okay, it seems I have missed the mark entirly. Your idea will work LilBudyWizer, however I was asking the wrong question. That works fine for rotating an object, but I need to do more than that, if I just rotate the camera matrix and leave its coords alone, objects its looking at just spin in place. I need objects to rotate around the camera, not the camera to rotate around the world origin. I need some way to rethink my engine so objects see their camera as the origin, at least in making the eyespace matrix.

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631777
• Total Posts
3002307
×