Jump to content
  • Advertisement
Sign in to follow this  

Unity (view)Matrix questions

This topic is 3146 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

Hey, i'm reading tutorials and learning from codes on how to setup different sorts of cameras in XNA. Like Forst Person, Flight Cam, Free Orbit etc. But the example i'm following is using matrices in a way that i really don't understand. In this case he makes changes to the viewMatrix. I know how a matrix looks like and that its a 4x4 matrix. But i have no idea what the elements in a veiwMatrix represent. Take this code for example:
//cam setup
            eye = Vector3.Zero;
            target = Vector3.Zero;
            targetYAxis = Vector3.Up;

            xAxis = Vector3.UnitX; //1,0,0
            yAxis = Vector3.UnitY; //0,1,0
            zAxis = Vector3.UnitZ; //0,0,1

            viewMatrix.M11 = xAxis.X;
            viewMatrix.M21 = xAxis.Y;
            viewMatrix.M31 = xAxis.Z;
            Vector3.Dot(ref xAxis, ref eye, out viewMatrix.M41);
            viewMatrix.M41 = -viewMatrix.M41;

            viewMatrix.M12 = yAxis.X;
            viewMatrix.M22 = yAxis.Y;
            viewMatrix.M32 = yAxis.Z;
            Vector3.Dot(ref yAxis, ref eye, out viewMatrix.M42);
            viewMatrix.M42 = -viewMatrix.M42;

            viewMatrix.M13 = zAxis.X;
            viewMatrix.M23 = zAxis.Y;
            viewMatrix.M33 = zAxis.Z;
            Vector3.Dot(ref zAxis, ref eye, out viewMatrix.M43);
            viewMatrix.M43 = -viewMatrix.M43;

            viewMatrix.M14 = 0.0f;
            viewMatrix.M24 = 0.0f;
            viewMatrix.M34 = 0.0f;
            viewMatrix.M44 = 1.0f;

Can someone explain what is happening here? I can see he's changing the viewMatrix to this: 1 0 0 0 0 1 0 0 0 0 1 0 ? ? ? 1 Then he does the DOT product for each axis, x,y,z to see how much degrees it has moved?? And WHY is he altering the M41, M42 and M43 of the matrix?? Does it really matters which part he stores that data in? What would've happend if he stored it in M14, M24, 34, wouldn't that be the same? Hoping someone can clearify this for me. Thanks for any help.

Share this post

Link to post
Share on other sites
Well, what ways of using matrics do you currently understand?

Are you familiar with the idea of using matrices to represent transformations?

Share this post

Link to post
Share on other sites
I know how it can be done in XNA. Well sort of. But i'm not really sure how to transformation is exactly done. Like i said in my example, he's doing a transformation on the fields 'M41, M42 and M43' in the matrix. Why there..??

Well guess i don't know enough about them yet. I'll try to read more about this. But if you guys want to give me a good explanation aswell, feel free to do so :)

I also just ordered this book:
3D Math Primer for Graphics and Game Development

The reviews say that it's a pretty good book to give a good basic understanding about matrices and quaternions etc.

Share this post

Link to post
Share on other sites
honestly basic matrix math is very simple.

there are many ways of visualizing what a matrix times a vector is. The two most useful to me are a change of basis, and another which tells you basically how much your vector is along each row vector in the matrix.

[c1x c2x c3x vx
c1y c2y c3y * vy
c1z c2z c3z] vz
vx*[c1] + vy*[c2] + vz*[c3]

the column vectors of the matrix become the new basis vectors. So if you want to rotate a vector about an axis, you rotate its coordinate frame and put its column vectors into the matrix.

v = [vx, vy, vz]

[r1x r1y r1z vx
r2x r2y r2z * vy
r3x r3y r3z] vz

= [ dot(v, r1),
dot(v, r2),
dot(v, r3)]

in this case we see you can view the matrix multiplication by dot products of the row vectors of the matrix and the vector you are multiplying. If your rows form an orthonormal basis, what you have done is calculated an inverse for the matrix formed by making the rows into columns.


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!