I'm trying to understand why my rectangle moves in the opposite direction I want it to.

I have a float for translation:

 glTranslatef(translate_x, 0, 0);

 if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
        translate_x += 5;

This is the rectangle I'm drawing (I did use glpush and -pop):

glColor3f(250, 0, 0);
    glVertex2i(400, 400); // Upper-left
    glVertex2i(450, 400); // Upper-right
    glVertex2i(450, 450); // Bottom-right
    glVertex2i(400, 450); // Bottom-left

Whenever I press the spacebar, the rectangle moves to the opposite direction of where I want it to move. Why is this?


2 Answers 2


glTranslatef moves the camera, and not what it is looking at. So adding 5 to the x-axis will make the camera move to the right, hence making it look like the rectangle is moving to the left. Subtracting 5 will make the rectangle go to the right.

  • \$\begingroup\$ Is this the normal way to do it? Should I just use a camera? \$\endgroup\$ May 12, 2014 at 11:09

If you want your rectangle to move on the same axis but in the opposite direction, you have to change the sign of the translation value.

glTranslatef(-translate_x, 0, 0);
  • \$\begingroup\$ A little short but to the point, you could elaborate on the Askers code and include their if statement in the answer. \$\endgroup\$ May 12, 2014 at 10:23
  • \$\begingroup\$ Ok I take notes, thank you. I wanted the changes to be easily spotted. \$\endgroup\$
    – Heckel
    May 12, 2014 at 11:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .