PDA

View Full Version : Moving the camera view with the mouse



11-16-2004, 04:19 PM
How can I move the camera view with the mouse?

I've already got the player moving forwards backward etc with the arrow keys, but I want to be able to look around by moving the mouse, can anyone help??

Me6
11-17-2004, 04:42 PM
case WM_MOUSEMOVE:
// save old mouse coordinates
oldMouseX = mouseX;
oldMouseY = mouseY;

// get mouse coordinates from Windows
mouseX = LOWORD(lParam);
mouseY = HIWORD(lParam);

// these lines limit the camera's range
if (mouseY < 60)
mouseY = 60;
if (mouseY > 450)
mouseY = 450;

if ((mouseX - oldMouseX) > 0) // mouse moved to the right
angle += 3.0f;
else if ((mouseX - oldMouseX) < 0) // mouse moved to the left
angle -= 3.0f;

return 0;
break; then you use the gluLookAt function:



cameraY = lookY + mouseY / 2.0f;

gluLookAt(cameraX, cameraY, cameraZ, lookX, lookY, lookZ, 0.0, 1.0, 0.0);

The lookx looky and lookz are were the camera is pointed at, if you want it to always be pointing at the model or watever then you make lookx y ad z equal the x y and z positions of the model.... hope this helps...

iznogoud
11-18-2004, 08:26 AM
I would like to say something with words instead of
code, so you can understand. I think it will be useful.

I code on GNU/Linux X11 (this is important). I trap
mouse motion events at every frame. Based on the
difference (in pixels) of the pointer position, I
rotate the camera. I typically reset the pointer
position after every frame also, so that it never
runs out of the bounds of the window. This gives an
artifact: the pointer appears nearly stationary in the
middle of the screen, but you can get rid of it by
loading up your own (blank) cursos. The other big
problem is that you cannot by any means drag the
pointer outside of the window. Therefore, you need
to have some sort of "escape key" to revert to the
classical behaviour if you have to.

The code posted by Me6 should be helpful.

Finally, the best way to go is to have a model of
how the camera behaves, like using equations of
motion, quaternions, etc, and make the pointer motion
simple inputs that affect the rate of change. Then
you integrate the camera rotations as you would the
motion of a player in the game, a plane in a simulation,
etc. But this might be an advaaanced topic.

Me6
11-18-2004, 09:50 AM
you might also want to try this link for OpenGL tuorials http://www.morrowland.com/apron/tut_gl.php , if you scroll down on the page you will get to the camera tutorials, tutorial 3 has camera movement with the mouse... Hopw that helps