Jump to content
  • Advertisement
Sign in to follow this  
TommyThree

OpenGL keeping mouse in center but moving the view

This topic is 406 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 this code below. Notice at the bottom the last line is the warp pointer function. This moves the mouse where you want it. The function as a whole moves a fps camera around like in a fps game. The only thing is that when i reach the edge of my screen and the mouse moves out of the program, it stops rotating around. I tried to center the cursor to the screen with warppointer but then it stays stuck when you try to rotate the view around. I kinda see why...but I was wondering if there was some code someone could give to solve my problem or modify this to work.

bool firstMouse = true;
GLfloat yaw = -90.0f;
GLfloat pitch = 0.0f;
GLfloat lastx = 800.0f / 2.0f;
GLfloat lasty = 600.0f / 2.0f;
...

void MyGLCanvas::onMouseMove(wxMouseEvent& event)
{
	if (firstMouse)
	{
		lastx = event.GetX();
		lasty = event.GetY();
		firstMouse = false;
	}

	GLfloat offsetx = event.GetX() - lastx;
	GLfloat offsety = lasty - event.GetY();
	lastx = event.GetX();
	lasty = event.GetY();

	GLfloat sens = 0.1f;

	offsetx *= sens;
	offsety *= sens;

	yaw += offsetx;
	pitch += offsety;

	if (pitch > 89.0f)
		pitch = 89.0f;
	if (pitch < -89.0f)
		pitch = -89.0f;

	glm::vec3 front;

	front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
	front.y = sin(glm::radians(pitch));
	front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
	aCam->cameraAt = glm::normalize(front);

	parentFrame->WarpPointer(lastx, lasty);
}

 

Share this post


Link to post
Share on other sites
Advertisement

Even if this is not a graphics related question, you think too complicated. It would be enougth to set your cursor floats once because you want to reset them to the center of your window anyways so why mess arround with last position? This

GLfloat cursor_x = 800.0f / 2.0f;
GLfloat cursor_y = 600.0f / 2.0f;

//call this when you are ready to receive input before any mouse event
void MyGLCanvas::Initialize()
{
   parentFrame->WarpPointer(cursor_x, cursor_y); 
}

void MyGLCanvas::onMouseMove(wxMouseEvent& event)
{
	GLfloat offset_x = event.GetX() - cursor_x;
	GLfloat offset_y = cursor_y - event.GetY();
  
  	...
  
	parentFrame->WarpPointer(cursor_x, cursor_y);
}

should be enougth working where I might not be sure if WarpPointer would not cause the event to trigger again so you need to investigate this on yourself. You should anyway catch input deltas that are out of certain threshold to do not over-sense your camera, this would also catchup mouse events caused by WarpPointer (if it does cause a trigger).

I would suggest not to use an offset rather than capture the direction cursor was moved, otherwise your camera would be incontrollable on certain framerates

Share this post


Link to post
Share on other sites

Thx for the reply. Sorry if this is in the wrong section. WarpPointer does indeed generate another mouse event so it cancels out what I do. I modified the code to be like this:

 

void MyGLCanvas::onMouseMove(wxMouseEvent& event)
{
	GLfloat xoffset = event.GetX() - lastx;
	GLfloat yoffset = lasty - event.GetY();

	if (xoffset > 0)
		xoffset = 0.5f;
	if (xoffset < 0)
		xoffset = -0.5f;

	if (yoffset > 0)
		yoffset = 0.5f;
	if (yoffset < 0)
		yoffset = -0.5f;

	yaw += xoffset;
	pitch += yoffset;

	if (pitch > 89.0f)
		pitch = 89.0f;
	if (pitch < -89.0f)
		pitch = -89.0f;

	glm::vec3 front;
	front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
	front.y = sin(glm::radians(pitch));
	front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
	aCam->cameraAt = glm::normalize(front);

	lastx = event.GetX();
	lasty = event.GetY();
}

and I called warppointer in my render function. Then I can move the camera while keeping the cursor centered. The only thing is that its choppy. It's looks pretty bad. How can I fix the choppiness?

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!