-1

I am trying to make a 2d game in opengl c++. I want to sroll a triangle using mouse movement. so I draw the triangle first. so far, I succeeded in making the triangle scroll when i move the mouse.

I used these 2 lines :

case WM_MOUSEMOVE:
    X += (GLfloat)LOWORD(lParam);

and then, in my drawing function, I do this:

glTranslatef(x,0.0f,0.0f);

So the triangle is moving with the mouse movement so far. But the problem is, I want it to move left when I move the mouse left and to move right when i move the mouse right.

No matter how I move the mouse, the triangle moves right and never left. Even when I move the mouse up or down,it just moves right.

How can I solve this?

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • I'd hazard a guess that the coordinate system you're using means that the += will always be adding on some positive value, as the left hand side of the screen will start from 0. Try this. Save the last position of it (say, xPrev). Move the mouse, get the new x value (say, xCurrent). xCurrent - xPrev should tell you which direction to move the triangle in, and how much by. – Muckle_ewe May 16 '13 at 10:48

1 Answers1

0

This has to do with the fact that glTranslatef translates relative to the current matrix: every time you translate, it's added to (technically, multiplied with) the current state.

You'll probably want to use = instead of += and add a glLoadIdentity in there. I usually call glLoadIdentity at the beginning of each frame. It resets the matrix to its initial state, which means translations will work like you'd expect. See this question for a better explanation of glLoadIdentity, as well as the maths behind it.

Community
  • 1
  • 1
Wander Nauta
  • 18,832
  • 1
  • 45
  • 62