outRider 852 Report post Posted February 5, 2002 I know this has been asked umpteenth times, but I've looked around and haven't found anything that has helped me to my satisfaction. I decided to create my own view matrix from scratch to represent the camera. The camera has 3 vectors, up, right, forward, representing orientation, and a position vector. Every frame I build a view matrix from those vectors and load it as the modelview matrix. It looks like this: r.x r.y r.z e.x u.x u.y u.z e.y f.x f.y f.z e.z 0 0 0 1 where e is -[r|u|f]T * p To rotate the camera up and down I build a rotation matrix from the camera's right vector, and then multiply [r|u|f] by this matrix. The rotation matrix is I + (sin(angle) * S) + ((1 - cos(angle) * S^2) where I = identity, s = 0 -r.z r.y r.z 0 -r.x -r.y r.x 0 To rotate the camera left and right I do the same, except build the rotation matrix from the camera's up vector. This does work to some extent, I do see rotation happening, but somehow it doesn't look right. Instead of using the camera's right and up vectors I've also tried building rotation matrices out of the x and y axes but although that looks right in some instances it doesn't look right in others. After combining several movements (rotate left 20, rotate up 10, rotate left 2, rotate up 1, rotate right 100, etc etc) the object I'm viewing gets totally skewed, as if the camera is being operated by a drunk with an inner ear problem. If someone can explain to me what I'm doing wrong I'd appreciate it greately. ------------ - outRider - Edited by - outRider on February 5, 2002 12:23:15 AM 0 Share this post Link to post Share on other sites

Clancy 124 Report post Posted February 5, 2002 um...heretypedef union { struct {VECTOR right; //X vectorGLfloat pad_0; VECTOR up; //Y vectorGLfloat pad_1; VECTOR dir; //Z vectorGLfloat pad_2; VECTOR pos; //pos vectorGLfloat pad_3;}; GLfloat m[16];} UMATRIX; And remember, the camera is the inverse of this. 0 Share this post Link to post Share on other sites

zedzeek 529 Report post Posted February 6, 2002 >> 0 -r.z r.y r.z 0 -r.x-r.y r.x 0<<?? are u trying to combine your rotation matirces together? u have to first do one + then do the next eg rotate first around the x axis + then around the y axis (ie u cant go rotate x+y together) http://uk.geocities.com/sloppyturds/gotterdammerung.html 0 Share this post Link to post Share on other sites

outRider 852 Report post Posted February 6, 2002 >> 0 -r.z r.yr.z 0 -r.x-r.y r.x 0<<That builds a rotation matrix for a rotation around an arbitrary axis. I use it to rotate around the camera's right vector for looking up and down, as opposed to using the x axis, because if the camera is rotated then the x axis might be running through the camera, and I'd get a roll instead.Edited by - outRider on February 6, 2002 9:25:19 AM 0 Share this post Link to post Share on other sites

outRider 852 Report post Posted February 6, 2002 Anyhow, I pulled out a piece of paper and started visualizing everything. It seems to me that to rotate the camera left/right I should use the y axis, not the camera''s up vector, since to simulate human head movement up would always be up, unless your whole body changes orientation. To rotate up/down I should use the camera''s right vector, not the x axis, since if I rotate the camera left/right 90deg the x axis would not be to the right of the camera and might be where the z axis is and I''d get a roll. Well, I''ve tried this and it doesn''t seem to work. Even on paper, rotating through the y axis left 90deg, then rotating up or down through the right vector produces a roll. I''ve worked it out on paper, and it seems to work.Here''s what I did Cam pos (0, 0, 0) R (1, 0, 0) U (0, 1, 0) F (0, 0, 1) cos90 0 sin90 1 0 0Ry = 0 1 0 Cam = [R|U|F] = 0 1 0 -sin90 0 cos90 0 0 1 0 0 1Ry * Cam = 0 1 0 -1 0 0 So that''s rotated the camera 90 degrees to its left, next, if I want to rotate up I''d rotate through the x axis, but the camera''s forward vector should be the x axis now and that should produce a roll, so intead I rotate through the camera''s right vector. To build a rotation matrix for the right vector I use the above formula, I + sin(angle)*S + (1-cos(angle))*S^2. (I realize that since the right vector is now mapped to the z axis I should get a matrix for a rotation around the z axis, which I do) 0 1 0 0 0 1Rright = -1 0 0 Cam = 0 1 0 0 0 1 -1 0 0 0 1 0Rright * Cam = 0 0-1 -1 0 0 Now the forward and up vectors has been rotated down 90deg. Though it works fine there, it doesn''t work when I actually use it in my app. Is this a correct way to do it? If so I''ll have to see if it''s something else thats screwing things up. If anyone has any insight on where the problem might be I''d appreciate it. ------------- outRider - 0 Share this post Link to post Share on other sites