Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm calculating matrices by hand. Translations are fine:

 void Translate (float x, float y, float z, float[4][4] m) {
     Identity (m);
     m[3][0] = x;
     m[3][1] = y;
     m[3][2] = z;

If I multiply a vector with this matrix, I get the correct transformation. My problem now is Rotations. I copied the definition from the OpenGL reference on glRotation, but I can't get it right. Can you spot my mistake?

void Rotate (float angle, float x, float y, float z, float[4][4] m) {   
    float c = cos (angle);
    float s = sin (angle);
    m[0][0] = x*x*(1-c)+c;
    m[0][1] = y*x*(1-c)+z*s;
    m[0][2] = x*z*(1-c)-y*s;
    m[0][3] = 0;
    m[1][0] = x*y*(1-c)-z*s;
    m[1][1] = y*y*(1-c)+c;
    m[1][2] = y*z*(1-c)+x*s;
    m[1][3] = 0;
    m[2][0] = x*z*(1-c)+y*s;
    m[2][1] = y*z*(1-c)-x*s;
    m[2][2] = z*z*(1-c)+c;
    m[2][3] = 0;
    m[3][0] = 0;
    m[3][1] = 0;
    m[3][2] = 0;
    m[3][3] = 1;

I don't know what else is relevant, so, if you're kind enough to lend me a hand on this and if I just don't present enough info, just say it.

Thank you for taking the time to read this question.

EDIT The trouble I'm having is the following: If I do Rotate (180,0,0,0), the vertexes are inverted as intended but the resulting triangle (in this case) is smaller.



share|improve this question
Are your matrices column major or row major? – sarahm Apr 29 '12 at 12:46
@sarahm: Column major; at least, that is what I think was doing. – AndrewM Apr 29 '12 at 14:31
Are you really intending to rotate around the nonsense axis denoted by the null vector? – Lars Viklund Apr 29 '12 at 18:26
up vote 2 down vote accepted

The coefficient computations are correct. I can think of two things:

  • you do not appear to ensure (x,y,z) is a unit vector; if the caller does not take care of this, you should divide each component by sqrt(x²+y²+z²).
  • the OpenGL convention is to have angles in degrees, and you are using cos() and sin() directly.

If none of these are your actual problem, you should describe what is going wrong exactly, maybe with a little example.

share|improve this answer
+1 -I agree that forgetting to convert from degrees to radians could be the problem here. – Stephan van den Heuvel Apr 29 '12 at 14:48
That was it! I forgot to convert to radians! Superb. Thank you community. – AndrewM Apr 29 '12 at 14:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.