Public Group

# Problem rotating camera with mouse

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

## Recommended Posts

I have this working with the arrow keys but the mouse is just crazy. can someone look at this code and tell me what I need to do to get the camera to rotate correctly on the x axis and on the y axis using the mouse with glut passive motion fuction. Also is there a way for me to be able to move forward or backward and turn at the same time with the arrow keys? Here is the code I have Much thanks, L.J. Bush
 #include &lt;math.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;GL/glut.h&gt;
#include &lt;iostream&gt;
//#define pi = 3.14159;
using namespace std;
bool lbuttonDown;
float angle=0.0,ratio;
float x=0.0f,y=0.0f,z=0.5f;
float lx=0.0f,ly=0.0f,lz=-0.1f;
float upx=0.0f,upy=0.0f,upz=0.0f;
float _angle = 30.0f;
float _cameraAngle = 0.0f;
//GLfloat LookUpDown = 0.0f;
void RotY(float ang) {

lx = sin(ang);
lz = -cos(ang);
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}

void moveFB(int direction) {
x = x + direction*(lx)*0.1;
z = z + direction*(lz)*0.1;
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}

void LookUpDown(float ang) {
ly = sin(ang);
lz = -cos(ang);
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0,1,0);
if (angle &gt; 90) {
angle = 90;
}
else if(angle &lt; -90) {
angle = -90;
}

}

void initRendering() {
glEnable(GL_DEPTH_TEST);
}

void handleKeypress(unsigned char key,
int x, int y) {
switch (key) {
case 27:
exit(0);

}
}

void handleSpecialKey(int key, int x, int y) {

switch (key) {
case GLUT_KEY_LEFT :
angle -= 0.1f;
RotY(angle);
break;
case GLUT_KEY_RIGHT :
angle +=0.1f;
RotY(angle);
break;
case GLUT_KEY_UP :
moveFB(1);
break;
case GLUT_KEY_DOWN :
moveFB(-1);
break;
/*case GLUT_KEY_UP && GLUT_KEY_RIGHT:
angle+=0.1f
((moveFB(1), RotY(angle));
break;
case GLUT_KEY_UP && GLUT_KEY_LEFT:
angle-=0.1f
(moveFB(1), RotY(angle));
break;
case GLUT_KEY_DOWN && GLUT_KEY_RIGHT:
angle+=0.1f
(moveFB(-1), RotY(angle));
break;
case GLUT_KEY_DOWN && GLUT_KEY_LEFT:
angle-=0.1f
(moveFB(-1), RotY(angle));
break;*/
case GLUT_KEY_PAGE_UP :
angle +=0.01f;
LookUpDown(angle);
break;
case GLUT_KEY_PAGE_DOWN :
angle -=0.01f;
LookUpDown(angle);
break;

}
}
void motionPassive(int x, int y)
{
glutSetCursor(GLUT_CURSOR_FULL_CROSSHAIR);

cout &lt;&lt; "Mouse moved at "
&lt;&lt; "(" &lt;&lt; x &lt;&lt; "," &lt;&lt; y &lt;&lt; ")" &lt;&lt; endl;

if(x &gt; ((GLUT_WINDOW_WIDTH)/2)){
angle += 0.1f;
RotY(angle);
}
else if(x &lt; ((GLUT_WINDOW_WIDTH)/2)){
angle -= 0.1f;
RotY(angle);
}

if(y &gt; ((GLUT_WINDOW_HEIGHT)/2)){
angle += 0.1f;
LookUpDown(angle);
}
else if(y &lt; ((GLUT_WINDOW_HEIGHT)/2)){
angle -= 0.1f;
LookUpDown(angle);
}

}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_RIGHT_BUTTON)
{
if (state == GLUT_DOWN)
glScalef(2.0,2.0,2.0);
else
cout &lt;&lt; "right button lifted at (" &lt;&lt;  x &lt;&lt; "," &lt;&lt; y &lt;&lt; ")" &lt;&lt; endl;
}
else if (button == GLUT_LEFT_BUTTON)
{
if (state == GLUT_DOWN)
cout &lt;&lt; "you shot me" &lt;&lt; endl;
else
lbuttonDown = false;
}
}

void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0,
(double)w / (double)h,
0.25,
200.0);
}

void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_COLOR_MATERIAL);
glMatrixMode(GL_MODELVIEW);
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);

//glRotatef(LookUpDown,1.0,0,0);
glPushMatrix();

glTranslatef(0.0f, 0.0f, 0.0f); //Move to the center of the trapezoid
glRotatef(_angle, 0.0f, 1.0f, 0.0f);
glScalef(0.01f, 0.01f, 0.01f);
//Cross back
glColor4f(1.0f,1.0f, 0.0f, 0.9f);
glVertex3f(-0.5, 2.5, -0.5);
glVertex3f(-0.5, -3.0, -0.5);
glVertex3f(0.5, -3.0, -0.5);
glVertex3f(0.5, 2.5, -0.5);

glVertex3f(-2.0, 1.0, -0.5);
glVertex3f(-2.0, 0.0, -0.5);
glVertex3f(2.0, 0.0, -0.5);
glVertex3f(2.0, 1.0, -0.5);

//Cross front
glColor4f(1.0f, 1.0f, 0.0f, 0.5f);
glVertex3f(-0.5, 2.5, 0.5);
glVertex3f(-0.5, -3.0, 0.5);
glVertex3f(0.5, -3.0, 0.5);
glVertex3f(0.5, 2.5, 0.5);

glVertex3f(-2.0, 1.0, 0.5);
glVertex3f(-2.0, 0.0, 0.5);
glVertex3f(2.0, 0.0, 0.5);
glVertex3f(2.0, 1.0, 0.5);

//Cross top
glVertex3f(0.5, 2.5, 0.5);
glVertex3f(-0.5, 2.5, 0.5);
glVertex3f(-0.5, 2.5, -0.5);
glVertex3f(0.5, 2.5, -0.5);
//Cross left top side
glVertex3f(-0.5, 2.5, 0.5);
glVertex3f(-0.5, 2.5, -0.5);
glVertex3f(-0.5, 1.0, -0.5);
glVertex3f(-0.5, 1.0, 0.5);
//Cross left side  top
glVertex3f(-0.5, 1.0, 0.5);
glVertex3f(-0.5, 1.0, -0.5);
glVertex3f(-2.0, 1.0, -0.5);
glVertex3f(-2.0, 1.0, 0.5);

//Cross left side  end
glVertex3f(-2.0, 1.0, 0.5);
glVertex3f(-2.0, 1.0, -0.5);
glVertex3f(-2.0, 0.0, -0.5);
glVertex3f(-2.0, 0.0, 0.5);

//Cross left side bottom
glVertex3f(-2.0, 0.0, 0.5);
glVertex3f(-0.5, 0.0, 0.5);
glVertex3f(-0.5, 0.0, -0.5);
glVertex3f(-2.0, 0.0, -0.5);
//Cross left bottom side
glVertex3f(-0.5, 0.0, 0.5);
glVertex3f(-0.5, 0.0, -0.5);
glVertex3f(-0.5, -3.0, -0.5);
glVertex3f(-0.5, -3.0, 0.5);
//Cross  bottom
glVertex3f(0.5, -3.0, 0.5);
glVertex3f(0.5, -3.0, -0.5);
glVertex3f(-0.5, -3.0, -0.5);
glVertex3f(-0.5, -3.0, 0.5);

//Cross right bottom side
glVertex3f(0.5, 0.0, 0.5);
glVertex3f(0.5, -3.0, 0.5);
glVertex3f(0.5, -3.0, -0.5);
glVertex3f(0.5, 0.0, -0.5);

//Cross right side bottom
glVertex3f(0.5, 0.0, 0.5);
glVertex3f(0.5, 0.0, -0.5);
glVertex3f(2.0, 0.0, -0.5);
glVertex3f(2.0, 0.0, 0.5);
//Cross right side end
glVertex3f(2.0, 1.0, 0.5);
glVertex3f(2.0, 0.0, 0.5);
glVertex3f(2.0, 0.0, -0.5);
glVertex3f(2.0, 1.0, -0.5);

//Cross right side top
glVertex3f(0.5, 1.0, 0.5);
glVertex3f(2.0, 1.0, 0.5);
glVertex3f(2.0, 1.0, -0.5);
glVertex3f(0.5, 1.0, -0.5);
//Cross right side top
glVertex3f(0.5, 1.0, 0.5);
glVertex3f(0.5, 1.0, -0.5);
glVertex3f(0.5, 2.5, -0.5);
glVertex3f(0.5, 2.5, 0.5);
glEnd();
glPopMatrix();

glPushMatrix();
glTranslatef(0.0f, -1.5f, 0.0f);
glRotatef(_angle,0.0,0.0,0.0);
//floor
glColor3f(0.5, 1.0, 0.0);
glVertex3f(-10.0,-0.5,10);
glVertex3f(-10.0,-0.5,-10);
glVertex3f(10.0,-0.5,-10);
glVertex3f(10.0,-0.5,10);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void update(int value) {
_angle += 4.0f;
if (_angle &gt; 360) {
_angle -= 360;
}

glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("Rotaion Test");
initRendering();

//glutFullScreen();
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutSpecialFunc(handleSpecialKey);
glutReshapeFunc(handleResize);
glutTimerFunc(25,update,0);
glutMouseFunc(mouse);
glutPassiveMotionFunc(motionPassive);
glutMainLoop();
return 0;
}


[Edited by - bushimports on August 14, 2008 10:32:39 PM]

##### Share on other sites
When posting large amounts of code, use [source] tags. You can edit your post using the 'edit' button in the upper right.

##### Share on other sites
It looks like you turn the camera _every time_ the mouse moves even a little. And the camera movement does not even depend on the direction of the mouse, just the mouse cursor position.

I'm not that familiar with glut, but I think it handles keyboard messages in the usual way. It means that you get about 10-20 keyboard events per second, when keeping the key down. Mouse messages, on the other hand, come much more rapidly. (Ten times faster? A hundred? I'm not really sure about the numbers.)

So, by looking at the code, I'd say that you end up spinning wildly in unexpected directions when using the mouse.

My suggestion is: In your motionPassive() function, calculate _how much_ the mouse has moved from the previous position, and use that as the turning angle.

##### Share on other sites
You will want to save the previous cursor position so you can compare how much the cursor has moved since the last time you were there.
Then you need to find the difference and scale it down to slow it.
And before leaving, save the new position into the old position for next time.

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002286
×

## Important Information

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!