#### Archived

This topic is now archived and is closed to further replies.

# GLUT Mouse Question

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

## Recommended Posts

Hi, I use the following code in glutMouseFunc to get the cursor position as a float: case GLUT_LEFT_BUTTON: //code for moving character p.moveto[0] = (float)x; p.moveto[1] = (float)y; break; and then the following code to change the orientation of a player object, (where p is the player), however, when using this the player simply moves eratically, most often on a 45 degree-ish angle in the first Quadrant (where I''m working), the movement code works fine if I give it specific values, but values taken from the mouse cause this error, please examine the code above and below, and if you can help, please do! <**NOTE** This is run every frame...> float len; p.dir[0] = p.moveto[0] - p.pos[0]; p.dir[1] = p.moveto[1] - p.pos[1]; len = sqrtf(p.dir[0] * p.dir[0] + p.dir[1] * p.dir[1]); p.dir[0] /= len; p.dir[1] /= len; p.pos[0] += (p.dir[0] * p.speed); p.pos[1] += (p.dir[1] * p.speed);

##### Share on other sites
The mouse pos is in screen coordinate, you must convert it in world coordinate...

Here is a little sample to move the camera with mouse:

static void Mouse(int button, int state, int x, int y)
{
if(state == GLUT_DOWN)
{
Old_Xmouse = x;
Old_Ymouse = y;

mouseButton = button;
}//if(state == GLUT_DOWN)
else if (state == GLUT_UP)
mouseButton = -1;
}//static void Mouse(int button, int state, int x, int y)

static void Motion(int x, int y)
{

if( mouseButton == GLUT_LEFT_BUTTON )
{
camera.Rotate(SIDE_AXIS, ((Old_Ymouse - y) * 2.0f) / 10000.0f);
camera.Rotate(UP_AXIS, ((Old_Xmouse - x) * 2.0f) / 10000.0f);
}

}//static void Motion(int x, int y)

##### Share on other sites
How do I convert a screen co-ord into a world co-ord in glut?

##### Share on other sites
How do I convert a screen co-ord into a world co-ord in glut?

##### Share on other sites
It depends of your projection and modelview matrix... A real conversion is complex and slow, just divide the mouse coord by an arbitrary number until you get good results.