making camera move with mouse

This is a discussion on making camera move with mouse within the Game Programming forums, part of the General Programming Boards category; Hi, I was wondering how I can use the Windows API and OpenGL to control the Camera, so when I ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    68

    making camera move with mouse

    Hi,

    I was wondering how I can use the Windows API and OpenGL to control the Camera, so when I move the mouse the camera moves like in an FPS Game.

    with OpenGL I can use the glutMotionFunc(mouse)

    where 'mouse' is the function that controls the view port rite?

    but with Windows API what Do I do?

    thx.
    -Ti22-

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    You check for WM_MOUSEMOVE messages in your windowprocedure function.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    68
    ah,

    k thx, i'll give it a shot.
    -Ti22-

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    68
    so what will the variables be?

    case WM_MOUSEMOVE:
    {

    }
    -Ti22-

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Whatever you call them in game. read up on what WM_MOUSEMOVE sends as lParam and wParam. Then you need to setup a camera system in your game. After that you need to add code in there to move the camera depending on how you moved the mouse.

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    68
    hmm

    so the messages isn't mouseup mousedown

    lParam and wParam represents the dimentsions of the viewing volume anyways rite?

    so in the case WM_MOUSEMOVE

    I have to have gluLookAt(whatever goes in here)

    to control the camera rite? ok now I understand but how do I use wParam and lParam exactly.

    the way I understand this is that I have to create some variables to control gluLookAt()

    and then I have to use wParam and lParam to control those variables so that when the mouse moves the camera moves.

    am I right?
    Last edited by Ti22; 01-15-2005 at 01:22 PM.
    -Ti22-

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    I dont know which one but I believe either wParam or lParam contains the current mouseposition or something along those lines.

  8. #8
    Registered User
    Join Date
    Jan 2004
    Posts
    68
    hmm it doesn't seem to work

    here's my code.

    case WM_MOUSEMOVE:
    {
    switch (wParam)
    {
    camY = (-wParam) + 250;
    }
    switch (lParam)
    {
    camX = lParam + 400;
    }

    gluLookAt(camX, camY, 5, 0, 0, 0, 0, 1, 0);
    }
    -Ti22-

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    no, that is the wrong aproach, look at this page.

  10. #10
    Registered User
    Join Date
    Jan 2004
    Posts
    68
    ah so its the lParam thats controling the mouse position.

    and Whoa! MSDN is useful

    I didn't think to look on it thx alot will look into it, I am kinda hopeless at programming but I am trying ,

    thx to people like you for helping
    -Ti22-

  11. #11
    Registered User
    Join Date
    Jan 2004
    Posts
    68
    hmm I tried the GET_X_LPARAM and the GET_Y_LPARAM it said Identifier not found..
    -Ti22-

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Try using this instead:
    Code:
    int x = LOWORD(lParam);
    int y = HIWORD(lParam);
    Last edited by Shakti; 01-16-2005 at 01:39 AM.

  13. #13
    Registered User
    Join Date
    Jan 2004
    Posts
    68
    ok i got the camera and the mouse to work but i have another problem hehehehe

    now the wasd button won't work with properly, the camera won't move in the direction it is headed, it pans in the x and z axis but won't 'head' in the direction the camera is facing

    here is my code for the WASD
    Code:
    if (keys['W'])
    					{
    						camera_x -= (float)sin(direction * piover180) * 0.05f;
    						camera_z -= (float)cos(direction * piover180) * 0.05f;
    					}
    					if (keys['S'])
    					{
    						camera_x += (float)sin(direction * piover180) * 0.05f;
    						camera_z += (float)cos(direction * piover180) * 0.05f;
    					}
    					if (keys['A'])
    					{
    						camera_x += (float)cos(direction * piover180) * 0.05f;
    						camera_z += (float)sin(direction * piover180) * 0.05f;
    					}
    					if (keys['D'])
    					{
    						camera_x -= (float)cos(direction * piover180) * 0.05f;
    						camera_z -= (float)sin(direction * piover180) * 0.05f;
    					}
    where direction = mouse_y = LOWORD(lParam);

    here isthe code for how the transformation is applied

    Code:
    GLfloat position_x = -camera_x;
    	GLfloat position_z = -camera_z;
    	GLfloat world_rotate = 360.0f + mouse_y;
    			
    	glRotatef(mouse_x, 1.0f, 0.0f, 0.0f);
    	glRotatef(world_rotate, 0.0f, 1.0f, 0.0f);
    	
    	
    
    	glTranslatef(position_x, -height, position_z);
    I think I have to do some trig maths to figure this out.
    Last edited by Ti22; 01-16-2005 at 07:11 AM.
    -Ti22-

  14. #14
    Registered User
    Join Date
    Jan 2004
    Posts
    68
    YAY I figured it out

    I was just observing the behavior of those buttons and came to a conclusion that I just need to change the directions to -direction in the formula used to control the W and the S and the A and the D and they all work nicely now
    -Ti22-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a Mouse Click
    By r0ss in forum C Programming
    Replies: 1
    Last Post: 06-03-2004, 08:55 PM
  2. linux mouse troubles
    By Kinasz in forum Tech Board
    Replies: 0
    Last Post: 01-07-2004, 04:35 AM
  3. Making a mouse driver/handler..
    By Alipha in forum Windows Programming
    Replies: 3
    Last Post: 04-07-2002, 09:43 AM
  4. Mouse in 800x600 24Bit Mode?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 11-11-2001, 12:38 AM
  5. Replies: 5
    Last Post: 09-08-2001, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21