Sign in to follow this  

help window cursor problem

This topic is 3044 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

I have a problem about windows cursor position. I am trying to simulate first person shooter camera. i keep the cursor on the center of window always and cursor never outside of window, every time the mouse move will turn the camera. below is my code, however, when i run it, come up a black screen and it is not working. if it is successful there is a box with color in front and background color is gray, so i think something wrong with my code can anyone help me? the code here is doing the mouse movement and camera rotation if i change SetCursorPos(cursorpos.x, cursorpos.y) to SetCursorPos(cursorpos.x, 0) it can work but only for x axis why but it come up a black screen with both x and y together.

POINT cursorpos;

	GetCursorPos(&cursorpos);
	ScreenToClient(myframework->GetHWND(), &cursorpos);
	
	if(cursorpos.x>800/2)
	{
	  mycamera->Yaw((float)(cursorpos.x-800/2)*myframework->GetMainTimer()->GetTimeElapsed()*cursorspeed);
	}

	if(cursorpos.x<800/2)
	{
		mycamera->Yaw(-(float)(800/2-cursorpos.x)*myframework->GetMainTimer()->GetTimeElapsed()*cursorspeed);
	}

	if(cursorpos.y>600/2)
	{
		mycamera->Pitch((float)(cursorpos.y-600/2)*myframework->GetMainTimer()->GetTimeElapsed()*cursorspeed);
	}

	if(cursorpos.y<600/2)
	{
		mycamera->Pitch(-(float)(600/2-cursorpos.y)*myframework->GetMainTimer()->GetTimeElapsed()*cursorspeed);
	}

	cursorpos.x=800/2;
	cursorpos.y=600/2;
	ClientToScreen(myframework->GetHWND(), &cursorpos);
	SetCursorPos(cursorpos.x, cursorpos.y);


Share this post


Link to post
Share on other sites
I'm not a getTime guru, but I think that should be called only once per frame, because you get correct result from the first getTimeEllapsed, than just zeros, since no time (or just few CPU cycles) have passed between the other getTimeEllapsed's.

Share this post


Link to post
Share on other sites
Quote:
Original post by szecs
I'm not a getTime guru, but I think that should be called only once per frame, because you get correct result from the first getTimeEllapsed, than just zeros, since no time (or just few CPU cycles) have passed between the other getTimeEllapsed's.


well this code is in the member function call MouseMoveEvent trigger when mouse move i do this way because i don't want it to be checked every time.

ok is there anyway to limit mouse cursor in a windows area, also keep it on the center of the window so every time my mouse move then it will check how much my mouse cursor move on x and y and then rotate camera?

Share this post


Link to post
Share on other sites
Just in case GetTimeElapsed() returns zero, why not make a local variable? It'll save (a very small amount of) time and both the x & y functions will use the same value.

float delta = myframework->GetMainTimer()->GetTimeElapsed()*cursorspeed;
if(cursorpos.x>800/2)
{
mycamera->Yaw((float)(cursorpos.x-800/2)*delta);
}
// etc.

Also, why not forget the "if" statements and just use:

mycamera->Yaw((float)(cursorpos.x-800/2)*delta);
mycamera->Pitch((float)(cursorpos.y-600/2)*delta);

Share this post


Link to post
Share on other sites

This topic is 3044 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this