   3D Orientation of an Object
  Hi,I need to store the orientation of an object in 3D space - the rotation and the position of, for example, a cube.I was wondering what the most efficient method of doing this would be. Is there a way to store the rotation and position of an object in a single 4x4 matrix such that when it is multiplied by a 4x4 transformation matrix will update to represent the new orientation?For example, an object for which "forward" is along the negative z-axis, when multiplied by a 4x4 matrix of rotation by 90 degrees around the y-axis, the "forward" vector is now along the negative x-axis.Is this possible? If so, what is the form of such a matrix?If not, what other methods of storing and manipulating this orientation data are there?Thanks in advance.- sixfoottallrabbit[Edited by - sixfoottallrabbit on May 30, 2009 8:48:03 AM]
  Yes, it can be done. The transformation you want to make is a particular case of affine transformation, and affine transformations can be represented with 4x4 matrices, as explained here. This is a very standard technique that I am sure is explained in any text about Math for computer graphics. It's also how OpenGL and DirectX deal with transformations, so I am sure it's really really easy to find information about this on Google. If your previous attempts (because you did search before you asked here, didn't you?) failed, now you have a few more keywords to work with.   ;Quote:Original post by alvaroYes, it can be done. The transformation you want to make is a particular case of affine transformation, and affine transformations can be represented with 4x4 matrices, as explained here. This is a very standard technique that I am sure is explained in any text about Math for computer graphics. It's also how OpenGL and DirectX deal with transformations, so I am sure it's really really easy to find information about this on Google. If your previous attempts (because you did search before you asked here, didn't you?) failed, now you have a few more keywords to work with.Yes, I have searched and I already know about affine transformations.Are you saying my object should have an orientation property which is just the affine transformation needed to move it to the correct position in the world?So if I had a cube rotated by 45 degrees around the x-axis, the value of orientation should be:Matrix[  1   ][  0   ][  0   ][  0   ][  0   ][ cos45][-sin45][  0   ][  0   ][ sin45][ cos45][  0   ][  0   ][  0   ][  0   ][  1   ]And then when the world positions of the vertices of the cube are calculated, I should just multiply each local vertex position by this matrix? And if I want to know which direction is forward, I just multiply [0,0,-1,1] by that matrix?I was just trying to find out the best method to do this. Is this it? Makes sense to do it like that, but often the thing that makes most sense isn't the most efficient method to do something. I'm trying to be as efficient as possible.Thank you.EDIT: corrected matrix[Edited by - sixfoottallrabbit on May 30, 2009 10:31:07 AM];Quote:Original post by sixfoottallrabbitAre you saying my object should have an orientation property which is just the affine transformation needed to move it to the correct position in the world?Basically, yes.Quote:So if I had a cube rotated by 45 degrees around the x-axis, the value of orientation should be:Matrix[  1   ][  0   ][  0   ][  0   ][  0   ][ cos45][-sin45][  0   ][  0   ][ sin45][ cos45][  0   ][  0   ][  0   ][  0   ][  1   ]Yes, that is correct.Quote:And then when the world positions of the vertices of the cube are calculated, I should just multiply each local vertex position by this matrix? And if I want to know which direction is forward, I just multiply [0,0,-1,1] by that matrix?That last part is not right. When you want to apply the transformation to a direction, you want to make the last coordinate 0, not 1, so the translation part is not applied.Quote:I was just trying to find out the best method to do this. Is this it?Some people prefer to use an object that contains a rotation and a translation. The rotation can then be represented using quaternions, which is often a better representation for rotations (they use less memory, they might be faster on some hardware, they are easier to renormalize and they make it much easier to interpolate).Quote:Makes sense to do it like that, but often the thing that makes most sense isn't the most efficient method to do something. I'm trying to be as efficient as possible.Don't be too obsessive about efficiency. Make your code clear, use good algorithms and you'll be a happier, better programmer. If your code turns out to be too slow, profile it and then optimize the part that is taking up the most time.   