-
Mouse problems
Hi folks;
I am making a 3D application in OpenGL (in C) where the camera view is controlled by the mouse. Here is a snippit of the code:
void aim_camera(int x, int y) {
if (lastPos[0] == x && lastPos[1] == y)
return;
if (x < lastPos[0])
yrot += 1.5f;
if (x > lastPos[0])
yrot -= 1.5f;
if (y < lastPos[1]) {
z -= 0.2f;
lookupdown -= 1.0f;
}
if (y > lastPos[1]) {
z += 0.2f;
lookupdown += 1.0f;
}
lastPos[0] = x;
lastPos[1] = y;
}
My question is this; when I get to the edge of the screen I have the obvious problem where I cannot rotate the view any more. I want to restore the mouse position to the middile of the screen when this happens. I understand there are functions out there that allow a person to do that but thus far I've not come up with anything. Can anyone out there help?
-
Two useful functions in the Win32 API are GetCursorPos and SetCursorPos.
Why not try this: Get the x diff and y diff from the current cursor position and the center of the screen. Then use this to determine how much to rotate about the two axis. Then at the end of the function set the cursor to the center of the screen. This way you will never reach the problem you described because every time the function is called the distance from center is measured and then it is reset back to center. Give it a try I think you will be happy with it.
-
All you need to do is make your camera movement's offsets on x and y directly proportional to how far the mouse is from the center of the screen.
Code:
//Get current position
Mouse.Update();
//Clamp in value only - does not actually move the mouse cursor
if (Mouse.x>=WIDTH) Mouse.x=WIDTH;
if (Mouse.y>=HEIGHT) Mouse.y=HEIGHT;
if (Mouse.x<=0) Mouse.x=0;
if (Mouse.y<=0) Mouse.y=0;
//Compute speed based on distance from center of screen, frame time, and modifier
CameraSpeed.x=(Mouse.x-ScreenCenter.x)*frameTime*SomeCoefficient;
CameraSpeed.y=(Mouse.y-ScreenCenter.y)*frameTime*SomeCoefficient;
::SetCursorPos(Mouse.x,Mouse.y);