title: Matrix transformations in OpenGL
body: <p>I'm building a 2D physics engine in Java using OpenGL (from <a href="http://lwjgl.org/" rel="nofollow">LWJGL</a>) to display the objects. The problem I am having is that the transformation matrices I apply to the frame seem to be getting applied in a different order to what it says that are.</p>
  
  <pre><code>/**
   * Render the current frame
   */
  private void render() {
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glLoadIdentity();
      GL11.glOrtho(0, Display.getDisplayMode().getWidth(), 0, Display
              .getDisplayMode().getHeight(), -1, 1);
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
  
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
  
      GL11.glPushMatrix();
      GL11.glTranslatef(Display.getDisplayMode().getWidth() / 2, Display
              .getDisplayMode().getHeight() / 2, 0.0f);
  
      renderFrameObjects();
  
      GL11.glPopMatrix();
  }
  
  public void renderFrameObjects() {
      Vector&lt;ReactiveObject&gt; objects = frame.getObjects();
      for (int i = 0; i &lt; objects.size(); i++) {
          ReactiveObject currentObject = objects.get(i);
          Mesh2D mesh = currentObject.getMesh();
  
          GL11.glRotatef((float)(currentObject.getR() / Math.PI * 180), 0, 0, 1.0f);
          GL11.glTranslated(currentObject.getX(), currentObject.getY(), 0);
          GL11.glBegin(GL11.GL_POLYGON);
          for (int j = 0; j &lt; mesh.size(); j++) {
              GL11.glVertex2d(mesh.get(j).x, mesh.get(j).y);
          }
          GL11.glEnd();
          GL11.glTranslated(-currentObject.getX(), -currentObject.getY(), 0);
          GL11.glRotatef((float)(-currentObject.getR() / Math.PI * 180), 0, 0, 1.0f);
      }
  }
  </code></pre>
  
  <p>In renderFrameObjects() I apply a rotation, a translation, draw the object (mesh coordinates are relative to the object's x, y), reverse the translation, and reverse the rotation. Yet the effect it gives when an object rotates (on collision) is similar to when one would apply a translation then a rotation (ie. rotates around a point at a radius). I can't seem to be able to figure this one out having tried various combinations of transformations.</p>
  
  <p>Any help would be appreciated.</p>