# Rotations in multiple axes

Hi all,

I am trying to simulate a cube rotating around a bounded box. As the cube rolls it is rotating in a given direction (r1). When the cube rebounds off the box, it’s direction and rotation axis changes (r2), relative to its previous rotation and so on until the cube stops (rn). The question I have on this is, for any given keyframe, do I need to apply all n previous rotations in order to get the correct orientation of the cube? Obviously, this can become a lot of work when there are multiple cubes rotation in any direction.

I think a nicer solution is to have a single transformation matrix for the cube that holds the result of all previous rotations, a rotation angle and rotation vertex. Then at each keyframe, I calculate a rotation matrix and apply it to the transformation matrix to get the new orientation. I am not sure how to do this though.

I want to be able to add some physics into the mix as well to simulate bouncing, collisions, etc but for now i just want to learn how to rotate the cube around the screen.

I imagine this is a regular problem for noobs like myself. By the way I am using opengl es 1.1.

I’d be grateful for any help on how to approach this problem.
Cheers,

I don’t know openGL ES, but here it’s the openGL version, I think it’s similar.

``````
cameraSetup();
pushMatrix();
pushMatrix();
apply the new rotation
multMatrix(objectMatrix);
get(modelViewMatrix, &objectMatrix);
popMatrix();
multMatrix(objectMatrix);
makeMatrixOrthonormal(objectMatrix);
drawTheObject();
popMatrix()

``````

this is slow, cause the get is very slow in standard openGL.

Or you can do the maths with your maths functions and avoid the get.
Here you can find some reference:
http://www.euclideanspace.com/maths/algebra/matrix/orthogonal/rotation/index.htm

*Remember, float error will accumulate over the time with the result that you will get a deformed cube after a while.
Remember to orthonormalize the matrix.
http://www.euclideanspace.com/maths/algebra/matrix/orthogonal/reorthogonalising/index.htm

For any kind of free rotation you should use quaternion math to make sure you going get gimbal lock. I created a video tutorials how to implement this on my website here: http://www.marek-knows.com/downloadSection.php?Topic=Math&pg=1#Math8