Jump to content
  • Advertisement
Sign in to follow this  
JimmyDeemo

Mouse movement confusion.

This topic is 3781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The book I’m reading uses DirectInput to return the difference in movements on the mouse. Having read the forums on here I’ve tried to use windows functions for my mouse input. I've got a cube in the centre of the screen, i want the camera to rotate around the block as i move the mouse left and right. I've for this working using the following method.
POINT cursorPos;
GetCursorPos(&cursorPos);
ScreenToClient( m_hWnd, &cursorPos );
m_pInput->UpdateMouseInfo( cursorPos );

m_cameraHeight += m_pInput->MouseDY()*fDelta;
m_cameraRotationY += m_pInput->MouseDX()*fDelta;

//InputHandler.cpp
void CInputHandler::UpdateMouseInfo(POINT currentPos)
{
	//Check for first time
	if( m_lastMousePt.x == -1.0f && m_lastMousePt.y == -1.0f )
	{
		m_lastMousePt.x = currentPos.x;
		m_lastMousePt.y = currentPos.y;
		m_mouseDiff.x = 0.0f;
		m_mouseDiff.y = 0.0f;
	}
	else
	{
		m_mouseDiff.x = m_lastMousePt.x - currentPos.x;
		m_mouseDiff.y = m_lastMousePt.y - currentPos.y;
		m_lastMousePt.x = currentPos.x;
	}	m_lastMousePt.y = currentPos.y;
}

Now i haven’t use the WM_MOUSEMOVE message as i want to return the movement difference between this frame and last. If i used the message then i make a big movement and suddenly stop. The difference is maintained even though my mouse has stopped moving. So the above works fine, except that when i reach the edge of the screen, the movement is no longer there so stops the camera. I have tried moving the cursor back to the middle of the screen with
SetCursorPos()
but this change is detected by the input handler also (because i'm storing previous values) and results in a slow jerky movement. I'm sure my logic is flawed somewhere but i can't work it out. Any help appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Thanks for the info, but does this really help me? I can currently get movement outside of my window using the method above. But when i reach the edge of my screen the movement stops because the cursor is still in the same position (as far as windows in concerned) even though i'm moving my mouse.

If i capture the mouse and use windows messages, will this detect the fact that my mouse is moving even if it reaches the edge of my screen? if so how do i update say the x difference of the mouse to 0 when the mouse stops moving (i.e. i no longer recieved the WM_MOUSEMOVE event). Thanks.

Share this post


Link to post
Share on other sites
I once had a similar problem and I found that it's easier to fix it by not using mouse messages and instead using only GetCursorPos() and SetCursorPos().

The basic idea was to always keep the cursor at the center of the screen. Each frame, I check to see how much the mouse has moved, save that information in variables and reset the mouse's position to the center of the screen.

It should look something like this:


// Called once at startup
void init() {
// center is the center of the screen
SetCursorPos(&center);
}

// Called every frame
void update() {
POINT pos;
GetCursorPos(&pos);
float deltaX = pos.x - center.x;
float deltaY = pos.y - center.y;
SetCursorPos(&center);

// Use deltaX and deltaY for movement
}


Share this post


Link to post
Share on other sites
Quote:
Original post by JimmyDeemo
Thanks for the info, but does this really help me? I can currently get movement outside of my window using the method above. But when i reach the edge of my screen the movement stops because the cursor is still in the same position (as far as windows in concerned) even though i'm moving my mouse.

If i capture the mouse and use windows messages, will this detect the fact that my mouse is moving even if it reaches the edge of my screen? if so how do i update say the x difference of the mouse to 0 when the mouse stops moving (i.e. i no longer recieved the WM_MOUSEMOVE event). Thanks.


Ah, no, I misread. You should still capture, but it won't work at the edge of the screen. For that you need lower-level mouse IO, or to use the GetPosition()/SetPosition() method Gage64 describes.

Share this post


Link to post
Share on other sites
Thanks for the help guys. I have pretty much close to what you were decribing, but i was saving the last position. That was giving me problems. So now i've done what you said Gage64 it works like a charm. Was over complicating it a bit i think.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!