#### Archived

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

# Implementing Mouse Look

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

## Recommended Posts

Hi, Does any one have suggestions for implementing a fast, platform-independent "mouse look" for a 3D engine? ie. the mouse can be used to look around the environment (6 degrees of freedom). I''m currently using C and GLUT to code my engine. I have keyboard implemented but have been thinking about best way to do mouse look. appreciate your help. thanks.

##### Share on other sites
Here''s a fraction of code for how I do it:

void display( void )
{
targetX = eyeX + sin(facing*PI/180.0);
targetY = eyeY + tan(facing2*PI/180.0f);
targetZ = eyeZ + cos(facing*PI/180.0);

gluLookAt( eyeX, eyeY, eyeZ,
targetX, targetY, targetZ,
0.0, 1.0, 0.0);

...
} /* end display */

void mouseFunc( int x, int y )
{
double mouseTurnX = 0.0;
double mouseTurnY = 0.0;

/* make mouse turn variable on how fast mouse is turned */
mouseTurnX = abs((prev_mouse_x - x)*0.5f);
mouseTurnY = abs((prev_mouse_y - y)*0.5f);

if ( mouseTurnX > 40.0 )
mouseTurnX = 40.0; /* don''t let user turn too fast */
if ( mouseTurnY > 40.0 )
mouseTurnY = 40.0; /* don''t let user turn too fast */

if ( x > prev_mouse_x )
{
facing -= mouseTurnX;
if (facing < 0.0) facing += 360.0;
}
if ( x < prev_mouse_x )
{
facing += mouseTurnX;
if (facing > 360.0) facing -= 360.0;
}
if ( y > prev_mouse_y )
{
facing2 -= mouseTurnY;

/* limit user''s range of head movement */
if ( facing2 < 120.0f ) /* 120.0 */
facing2 = 120.0;
}
if ( y < prev_mouse_y )
{
facing2 += mouseTurnY;

/* limit user''s range of head movement */
if ( facing2 > 240.0f )
facing2 = 240.0f;
}

prev_mouse_x = x;
prev_mouse_y = y;
} /* end mouseFunc */

void main()
{
...
glutPassiveMotionFunc(mouseFunc);
glutMotionFunc(mouseFunc);
glutIdleFunc(display);
glutDisplayFunc(display);
...
}

I''ll e-mail you my entire code of a simple 3D environment with mouse movement if you like.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631710
• Total Posts
3001846
×