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);
GLUT Mouse Question
Started by shalrath, Jul 23 2001 01:56 AM
5 replies to this topic
Sponsor:
#2 Members - Reputation: 122
Posted 23 July 2001 - 09:53 AM
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)
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)






