   Rotate cube to one of its sides.
  Hello. I'm trying to rotate a 3d cube to one of its sides so that the side faces up. Say I have a dice and its sitting on a floor with the number 1 facing upward. If I want to show the number 6 face on top instead, how do I rotate the dice to that angle?I don't need any smooth rotation, it just needs to snap.I'm not sure how to go about building a rotational matrix to do this.Thanks for any help.
  Make a look-at matrix.Assume the center of your die is at 0,0,0. Make a unit vector pointing to each side (ie 6 face is straigt down, so <0,-1,0>). Then, to make a rotation matrix, pick the vector of the side you want to see, that is your 'up'. Take the vector of any perpendicular side (if your face is odd, pick an odd side. or even pick an even side). That is your 'at' vector. Then your left vector is (up cross at).You now have the 3 axis of your matrix (left=X,up=Y,at=Z ). Add in a position vector for the last column of the matrix and you are done.;  can't seem to get this to work. I've tried doing what you said like this://dice face normals (x, y, z)1 - Vector3D(0, 1, 0)2 - Vector3D(0, 0, 1)3 - Vector3D(-1, 0, 0)4 - Vector3D(1, 0, 0)5 - Vector3D(0, -1, 0)6 - Vector3D(0, 0, -1)//the face index we want (index 5 = 6 on the dice)index = 5;//f1 is the face we want moved to the top//f2 is the face perpendicular to face 1var f1:DiceFace = diceFaces[index];var f2:DiceFace = diceFaces[(index + 1) % diceFaces.length];//get the normalsvar up:Vector3D = f1.normal;var at:Vector3D = f2.normal;var left:Vector3D = up.crossProduct(at);//modify the transform matrixdata[ 0] = 1.0;data[ 1] = left.x;data[ 2] = left.y;data[ 3] = left.z;data[ 4] = up.x;data[ 5] = 1.0;data[ 6] = up.y;data[ 7] = up.z;data[ 8] = at.x;data[ 9] = at.y;data[10] = 1.0;data[11] = at.z;data[12] = 0.0;data[13] = 100;data[14] = 0.0;data[15] = 1.0;matrix.rawData = data;//apply the transformtransform = matrix;the result is doing something funny to the object making it disappear. Am I setting the values correctly in the matrix? I know there's two versions of a matrix, this one uses columns first.[ 0][ 4][ 8][12][ 1][ 5][ 9][13][ 2][ 6][10][14][ 3][ 7][11][15]also, why do I need to know the perpendicular angle to the required normal? shouldn't I need to use the positive y axis since thats where I want it to face?Sorry, I'm very new to all this stuff.   ;  Yeah, you got those in the wrong spots on the matrix. And you have erroneous 1.0s in there that should be 0.0s.Maybe this could make more sense of it for you?data[ [0]] = left.x;data[ [1]] = left.y;data[ [2]] = left.z;data[ [3]] = [0].[0];data[ [4]] = up.x;data[ [5]] = up.y;data[ [6]] = up.z;data[ [7]] = [0].[0];data[ [8]] = at.x;data[ [9]] = at.y;data[[10]] = at.z;data[[11]] = [0].[0]data[[12]] = [0].[0];data[[13]] = [100].[0];data[[14]] = [0].[0];data[[15]] = [1].[0];Quote:also, why do I need to know the perpendicular angle to the required normal? shouldn't I need to use the positive y axis since thats where I want it to face?The basics of it are that a transform matrix should have 3 perpendicular unit vectors as the 3 columns: "at" "up" and "left".  If you point any one of them in a particular direction, and keep the others perpendicular then you've pointed the object somewhere. I'ma think on this a min.edit: yeah. i think you may need to invert the values on the 4 horizontal faces for it to not rotate "backwards" from what you expect. But then it should work.[Edited by - KulSeran on October 7, 2010 11:57:32 PM];  ah great. I was getting thrown off by this image:because I didn't want my object to scale I was leaving those values as 1. I still don't really understand how this all works, but I'm trying to :)it's still not rotating in the correct direction though. I think it must be determining the perpendicular normal.Is there a way to find the perpendicular normal from another normal? So I could use the normal of the target face then find the perpendicular normal of that.   ;  Yeah... sorry if this seems like a wild goose chase. I got myself lost. The up/at/left matrix building is a solid idea. But I also got lost on a good way to pick the axis you need. You for sure need 2 axis, and the third is just the cross of the first two. So, yeah, it looks like you need a lookup table with 2 axis, not just one.You could also just make a lookup table of pitch and roll. And just build up a rotation matrix. (equivelent idea).Either way, you just have to work out by hand the lookup for the rotation you want.Maybe some other more awake poster could come by and give a more algorithmic solution.Quote:because I didn't want my object to scale I was leaving those values as 1. I still don't really understand how this all works, but I'm trying to :)The top 3x3 is the rotation/scale matrix. The 4th column is your translation. The 4th row is usually only used for skew tricks like projection matrices.  Many games shorten their regular object matrix to a 4x3 matrix since the W row has minimal use for normal transforms on world objects. If you take your right hand and pretend your thumb(up), index(left) and pointer(at) fingers are the 3 axis of the 3x3 rotation matrix, then you can visualize how your object is oriented.;  well thanks for your help.I'm having a real hard time trying to visualize how this rotation should happen. Its like I need the target face to become the cube's orientation, find the rotational distance from that orientation to a top orientation.I like 2D much better :(As a dirty hack I've manually gone through and recorded down rotations for every side, now I'm using that for orientating the cube. It works, so problem solved.[Edited by - mr_malee on October 8, 2010 12:00:01 AM]