OpenGL camera - move the camera without disconnecting it when using SetCursorPos (x, y) ;?

I have a problem trying to update the camera.

I want to change the step and yaw of the camera (where to find it) with the mouse. But I want the mouse to stay in the center of the window.

//where MouseP.x .y is the mouse position //(which is centered to the current window) //get old position of the mouse OldP.x = MouseP.x; OldP.y = MouseP.y; //work out the distance traveled Delta.x = MouseP.x - OldP.x; Delta.y = MouseP.y - OldP.y; //update the camera(using distance traveled) Rot.Yaw -=Delta.x/5; Rot.Pitch -= Delta.y/5; //move mouse to the center of the screen SetCursorPos(CENTER_SCREEN_X,CENTER_SCREEN_Y); 

the problem is that the camera returns to a certain point when the mouse is configured to return to the origin.

I want to update the camera to a distance traveled from the origin, but not a distance to the origin.

If I choose this, it works wonderfully, but then the mouse may exit the window.

+4
source share
3 answers

I believe the problem is that your code block is probably inside the catch of the WM_MOUSEMOVE event?

When you call SetCursorPos , it itself generates another WM_MOUSEMOVE event, so you process this block of code once when you move the mouse, and again when you call SetCursorPos , and this does the opposite.

You probably don't want to place SetCursorPos inside the SetCursorPos event event, otherwise you will generate an infinite loop of messages (each SetCursorPos creates a different one).

Perhaps you can move this code outside the message pump and just run it once for each frame in the update cycle: request the current mouse position, transform the camera, and then position the cursor at the origin.

+1
source

You need to be careful with the movement of the cursor and the three-dimensional mouse. People tend to think that this is connected, but in fact it is not. read the article from msdn: http://msdn.microsoft.com/en-us/library/windows/desktop/ee418864%28v=vs.85%29.aspx "Using high-definition mouse movement" is how to get input mouse in a 3D application. The cursor must be hidden.

If you try to restart it, it will create a terrible uneven feeling when the cursor tries to leave the center, when the user moves the mouse, but is held in place by an invisible spring. Which does not look very professional. You cannot deal with this because your application is not planned before displaying the mouse pointer.

+2
source
 if(g_States::Instance().MouseLook()) { //Test the mouse input POINT mousePos; GetCursorPos(&mousePos); mouseX = mousePos.x; //g_InputEngine::Instance().GetX(); mouseY = mousePos.y; //g_InputEngine::Instance().GetY(); mouseX = mouseX - m_HalfWidth; mouseY = mouseY - m_HalfHeight; mouseFloat = mouseX * C_MOUSESMOOTHINGFACTOR; g_Scene::Instance().GetCamera()->RotateYaw(-mouseFloat); mouseFloat = mouseY * C_MOUSESMOOTHINGFACTOR; g_Scene::Instance().GetCamera()->RotatePitch(mouseFloat); //Reset mouse to center on the screen SetCursorPos(m_HalfWidth,m_HalfHeight); } 

So, this is the Mouselook function of the prototype spacegame, which I developed for fun, and back, what I did was change it instead of GetCursorPos(&mousePos); . This will get the current cursor position no matter when your input code updates the location of the mouse cursor. The rest of the math in the function is for sensitivity and actual camera rotation only. Hope this code helps you work a bit.

Let me know if you need more explanation.

Edit: I just remembered the reason I did this. This is because the screen flickered and it moved, but the input mechanism will be updated by calling SetCursorPos() because I used WM_MOUSEMOVE to update the input mechanism. I'm not sure how you get your input, but it will still help you.

0
source

Source: https://habr.com/ru/post/1411176/


All Articles