title: Rotation of object around World Axis OpenGL
body: <p>I'm trying to make a controllable ball in OpenGL. I'm using my own matrix class to transform the object matrix, but I can't seem to get the Rotation right. I always end up with the ball rotating around the local axis.
  This is how it looks right now <a href="https://gfycat.com/LongKindBassethound" rel="nofollow">https://gfycat.com/LongKindBassethound</a> . The long line are the local axis.</p>
  
  <p>So when the ball moves forward the next side movement will be wrong. Theres a function in the matrix class that allows rotation around any axis:</p>
  
  <pre><code>Matrix&amp; Matrix::rotationAxis(const Vector&amp; Axis, float Angle) {
  const float Si = sin(Angle);
  const float Co = cos(Angle);
  const float OMCo = 1 - Co;
  Vector Ax = Axis;
  Ax.normalize();
  
  m00= (Ax.X * Ax.X) * OMCo + Co;
  m01= (Ax.X * Ax.Y) * OMCo - (Ax.Z * Si);
  m02= (Ax.X * Ax.Z) * OMCo + (Ax.Y * Si);
  m03= 0;
  
  m10= (Ax.Y * Ax.X) * OMCo + (Ax.Z * Si);
  m11= (Ax.Y * Ax.Y) * OMCo + Co;
  m12= (Ax.Y * Ax.Z) * OMCo - (Ax.X * Si);
  m13= 0;
  
  m20= (Ax.Z * Ax.X) * OMCo - (Ax.Y * Si);
  m21= (Ax.Z * Ax.Y) * OMCo + (Ax.X * Si);
  m22= (Ax.Z * Ax.Z) * OMCo + Co;
  m23= 0;
  
  m30= 0;
  m31= 0;
  m32= 0;
  m33= 1;
  
  return *this;
  }
  </code></pre>
  
  <p>I think with this I can take the world direction vectors and transform them to the local space of the object and then rotate around the result. I don't really know how to do that though (matrix of the ball * world vector? That doesn't work). I would really like to avoid quaternions, but if I can't do that I would appreciate suggestions in that direction too.</p>
  
  <p>EDIT: More Info</p>
  
  <p>The transforamtion Code. As you can see I tried different methods that all do the same... So no surprise there that it doesnt work.</p>
  
  <pre><code>Matrix transM, rotX, rotZ;
  rotationX = straight;
  rotationZ = side;
  if (velocity != Vector(0, 0, 0)) {
      velocity.X = -0.0005 * DeltaTime;
      velocity.X = clamp(velocity.X, 0, FLT_MAX);
  
      velocity.Z = -0.0005 * DeltaTime;
      velocity.Z = clamp(velocity.Z, 0, FLT_MAX);
  }
  
  velocity.X += speed * -side * DeltaTime;
  velocity.Z += speed * straight * DeltaTime;
  transM.translation(velocity.X, 0, velocity.Z);
  
  if (velocity.Z != 0 || velocity.X != 0) {
      //http://mathworld.wolfram.com/EulerAngles.html
      //http://gamedev.stackexchange.com/questions/67199/how-to-rotate-an-object-around-world-aligned-axes
  
      Vector localAxisX =  m_Ball * Vector(1, 0, 0);
      Vector localAxisZ = m_Ball * Vector(0, 0, 1);
      rotX.rotationAxis(Vector(1, 0, 0), 0.5* M_PI * straight * DeltaTime);
      rotZ.rotationAxis(Vector(0, 0, 1), 0.5* M_PI * side * DeltaTime);
      //rotX.rotationX(0.5* M_PI * straight * DeltaTime * 3);
      //rotZ.rotationZ(0.5* M_PI * side * DeltaTime * 3);
      //Matrix fullRotation.rotationYawPitchRoll(Vector(0,  0.5* M_PI * straight * DeltaTime, 0.5* M_PI * side * DeltaTime));
      m_Ball = transM * m_Ball * (rotX*rotZ);
  }
  else {
      m_Ball = transM * m_Ball;
  }
  </code></pre>
  
  <p>Draw code with my previous attempt trying to use glRotatef (obviously commented out right now)</p>
  
  <pre><code>void Ball::draw(float DeltaTime) {
    glPushMatrix();
    glMultMatrixf(m_Ball);
    if(rotationX)
      glRotatef(0.5* M_PI * rotationX * DeltaTime, 1.0, 0.0, 0.0);
    if(rotationZ)
      glRotatef(0.5* M_PI * rotationZ * DeltaTime, 0.0, 0.0, 1.0);
    g_Model_ball.drawTriangles();
    glPopMatrix();
    drawAxis();
  }
  </code></pre>