# Is something wrong with my matrix math?

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

## Recommended Posts

Hello, Before I begin, I should mention that I'm using the OpenGL-based OpenSceneGraph. I don't know if it will have any bearing on this question, but OpenSceneGraph conventionally regards the z-axis as the vertical one. My problem is that when I fetch my camera's view matrix from OpenSceneGraph, the matrix that I get is very odd compared with what I expect. I tend to doubt myself rather than doubt osg, though, because a) it's a popular library, b) my camera seems to be working fine -- it's just that when I set a breakpoint and look at the view matrix, it looks wrong to me, c) when I call their getLookAt() method on said view matrix, I get the correct, expected vectors back (eye, center, up). So, I'm guessing that the problem is with me, and I'm hoping that one of you can help. Basically, at the beginning of my program, I set the camera's default position using the setLookAt() method. I pass in [0,500,30] for the eye vector, [0,480,30] for the center vector, and [0,0,1] for the up vector. So, keep in mind that "z" is the vertical axis here. So, if I subtract the eye vector from the center vector, I get [0,-20,0]. So, the camera should be looking down the negative y axis. Since we're in a right-handed coordinate system, we normally look down the positive y axis. So, setting this default position should be equivalent to applying a 180-degree y-axis rotation, followed by a translation to [0,500,30]. Figuring this out on paper, my 4x4 rotation matrix would be:
-1    0    0    0
0   -1    0    0
0    0    1    0
0    0    0    1

And my 4x4 translation matrix would be:
 1    0    0    0
0    1    0    0
0    0    1    0
0    500  30   1

Concatenated:
-1    0    0    0
0   -1    0    0
0    0    1    0
0    500  30   1

Now, just to do some tests with this, suppose I wanted to determine the world-space position of a vector, whose eye-space position is [1,1,1]. To do so, I would multiply the vector by the above matrix. When I do so, I get [0,499,31], which seems correct when I visualize it in my head or with a diagram. Now, I want to convert that world-space vector back into eye-space. I already know the answer, of course, but if I didn't, I would need the inverse of the camera's world matrix above. That inverse is:
-1    0    0    0
0   -1    0    0
0    0    1    0
0    500  30   1

When I multiply the [0,499,31] vector by that, I get my original [1,1,1] vector. So, at this point I'm satisfied that the above matrix is the correct view matrix for my camera. Yet, when I pull the view matrix from the camera in OpenSceneGraph, it's a bit different:
-1    0    0    0
0    0    1    0
0    1    0    0
0   -30  -500  1

I'm really frustrated, because I've tried to calculate the view matrix on paper every which way I can -- using different rotations, trying different multiplication orders, trying row-major vs. column-major, etc. But no matter what I do, I can't seem to arrive at the view matrix that OSG is giving me. What's worse is that the view matrix that OSG gives me seems to be producing correct results. The camera works fine, and when I try to decompose the matrix into its eye/center/up vectors using the getLookAt() method, the vectors are exactly correct. What am I doing wrong?

##### Share on other sites
Nevermind, I think I've got it.

My confusion stemmed from the fact that, in the OSG documentation, it says that the "z" axis is up. I thought this meant that, using an identity view matrix, I would be staring down the +y axis, with the +z above me, and the +x to my right. It turns out, that's not the case. Using an identity view matrix, I would still be staring down the -z axis, just like OpenGL. It's just that, in OSG world, I would be pointing down.

Therefore, my assumptions about what rotations I would need in order to end up at the aforementioned orientation were incorrect. Instead of needing a single 180-degree z-rotation, I need a 90-degree y-rotation and a 180-degree x-rotation (or some equivalent combination of rotations).

When I work out the proper x/y rotations on paper, I get the same matrix that OSG is giving me. So I'm good.

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632984
• Total Posts
3009716
• ### Who's Online (See full list)

There are no registered users currently online

×