Sign in to follow this  

Mouse Rotated Camera [Solved]

Recommended Posts

I am trying to make a camera that rotates when the mouse's left button is clicked and when they move the mouse but I am having some trouble with it, for some reason it won't update whether I have my mouse buttons clicked or not clicked. Here's my code:
/*These variables are global and declared at the top of the file*/
Camera Cam; /*This is my own class that basically just holds 2 vectors for rotation and position*/
int xLast = 0, yLast = 0;
bool LMB = false;
void mouseClick(int Button, int State, int x, int y){
	if(Button == GLUT_LEFT_BUTTON && State == GLUT_DOWN)
		LMB = true;
	if(Button == GLUT_LEFT_BUTTON && State == GLUT_UP)
		LMB = false;

void mouseMovement(int x, int y){
		int xDiff, yDiff;
		xDiff = x - xLast;
		yDiff = y - yLast;
		xLast = x;
		yLast = y;
		Cam.Rotation.x += yDiff*0.3;
		Cam.Rotation.y += xDiff*0.3;

And here's how I am assigning the functions to glut.

[Edited by - MrPickle on May 10, 2008 7:04:43 AM]

Share this post

Link to post
Share on other sites
Sure :)

void drawScene(){

glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);


void Camera::Update(void){

void Camera::UpdateRotation(void){
glRotatef(Rotation.x, 1.0f, 0.0f, 0.0f);
glRotatef(Rotation.y, 0.0f, 1.0f, 0.0f);

void Camera::UpdatePosition(void){
glTranslatef(-Position.x, -Position.y, -Position.z);

Share this post

Link to post
Share on other sites
I don't know so much about glut, but I'd guess that this might be the problem:

Is there some "active motion func" too? Just a hunch.
You can try this just by removing that "if (LMB)" line from your mouseMovement() function.

Also this might be the problem:
int xDiff,yDiff;
Cam.Rotation.x += yDiff*0.3;
Cam.Rotation.y += xDiff*0.3;

xDiff and yDiff are integers and you multiply them by 0.3.
What datatype Cam.Rotation.x and Cam.Rotation.y are? Hopefully they are floats, otherwise those lines above won't do anything.

Share this post

Link to post
Share on other sites
Yes, there is a active motion function. That was the problem :)

Thank you, I guess this is what you get when you don't read what the differences are between things :P

And yes, Rotation.x and Rotation.y are floats.

Share this post

Link to post
Share on other sites

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