Jump to content
  • Advertisement
Sign in to follow this  
BlueSpud

OpenGL Smooth OpenGL mouse look

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I want to make the mouse looking smooth for my game. I have the mouselook working, but in comparison to games like hl2 or battlefield, its terribly choppy. I'm coding it natively on a mac, and I want to make it cross platform, so help in that regard would be appreciated. This is my current code with the choppiness:

if (giveMouse == false)
    {
    int speed = 1;
    glutWarpPointer(640, 400);
    
    int diffy = y - oldy;
    int diff = x - oldx;
    oldx =  x;
    oldy = y;
    if (y != 400)
        {
        pitch += (diffy/4)*speed;
        if (pitch < -180)
            pitch = -180;
        if (pitch > 180)
            pitch = 180;
        }
    if (x != 640)
        {
        yaw += (diff/8)*speed;
        if (yaw > 360)
            yaw = 0;
        if (yaw < 0)
            yaw = 360;
        }
        Projectile.yaw = yaw;
        Projectile.pitch = pitch;
        Client.dir = yaw;
    }

And thats being called in the glutPassiveMotionFunc. Any help would be appreciated, thanks.

Share this post


Link to post
Share on other sites
Advertisement

I don't remember how GLUT deals with mouse cursor coordinates going outside of the window, but I never warp the mouse back to the middle at the end of a frame. Instead I just grab the change in mouse position after a frame (Or average it over two or three frames if you want mouse smoothing) and use that to change the view angle. I don't know what the reason is particularly, but whenever I've warped the mouse back afterwards it's made the movement jittery.

Share this post


Link to post
Share on other sites

I don't remember how GLUT deals with mouse cursor coordinates going outside of the window, but I never warp the mouse back to the middle at the end of a frame. Instead I just grab the change in mouse position after a frame (Or average it over two or three frames if you want mouse smoothing) and use that to change the view angle. I don't know what the reason is particularly, but whenever I've warped the mouse back afterwards it's made the movement jittery.


But that would eventually lead to the user moving the mode out of the window.

Share this post


Link to post
Share on other sites

yaw += (diff/8)*speed;
if (yaw > 360)
yaw = 0;

 

yaw = yaw - 360;

if yaw is 380, the end result would be 20 degrees past 360 or 20 degrees. (380 - 360).

Not a solution but a bug in your code.

 

Share this post


Link to post
Share on other sites

i sue this code for moving my mouse:

void __fastcall  TFCOpenGL::move_mouse()
{

if (FCOpenGL->Visible == false) usem = false;

if (usem == true) {
GetCursorPos(&P);
P.x = P.x;// - FAboutGL->Left;
P.y = P.y;// - FAboutGL->Top;
TPoint LastPoint;
LastPoint.x = P.x - cx;
LastPoint.y = P.y - cy;
cx = RoundTo(Left+Width / 2.0,0);
cy = RoundTo(Top+Height / 2.0,0);
SetCursorPos( cx,cy);

MouseSpeed = MouseSpeed + 0.0f;

glop = glop - (cx - P.x)/100.0f * MouseSpeed;
heading = heading - (cy - P.y)/100.0f * MouseSpeed;

 // if (abs(heading) > 90.0 ) mouse_GIMBL = -1.0f; else mouse_GIMBL = 1.0f;
//  if (abs(360.0 - heading) > 90.0 ) mouse_GIMBL = -1.0f; else mouse_GIMBL = 1.0f;

//	if (abs(heading) > 90.0 ) mouse_GIMBL = -1.0f; else mouse_GIMBL = 1.0f;

if (heading > 90.0   ) { heading = 90.0;}
if (heading < -90.0  ) { heading = -90.0; }

if (glop    > 360  )    glop = 0;
if (glop    < -360 )    glop = 0;


}
}

Its something like that see what is the position of the cursor calculate difference (actual mouse cursor, center of the drawing window) then move cursor to the center of the drawing window

 

 

 

but you could use time in your move routine

Share this post


Link to post
Share on other sites

yaw += (diff/8)*speed;
if (yaw > 360)
yaw = 0;

 

yaw = yaw - 360;

if yaw is 380, the end result would be 20 degrees past 360 or 20 degrees. (380 - 360).

Not a solution but a bug in your code.
 

You're right, that is a problem. Thanks for pointing that out.

Share this post


Link to post
Share on other sites

i sue this code for moving my mouse:

void __fastcall  TFCOpenGL::move_mouse()
{

if (FCOpenGL->Visible == false) usem = false;

if (usem == true) {
GetCursorPos(&P);
P.x = P.x;// - FAboutGL->Left;
P.y = P.y;// - FAboutGL->Top;
TPoint LastPoint;
LastPoint.x = P.x - cx;
LastPoint.y = P.y - cy;
cx = RoundTo(Left+Width / 2.0,0);
cy = RoundTo(Top+Height / 2.0,0);
SetCursorPos( cx,cy);

MouseSpeed = MouseSpeed + 0.0f;

glop = glop - (cx - P.x)/100.0f * MouseSpeed;
heading = heading - (cy - P.y)/100.0f * MouseSpeed;

 // if (abs(heading) > 90.0 ) mouse_GIMBL = -1.0f; else mouse_GIMBL = 1.0f;
//  if (abs(360.0 - heading) > 90.0 ) mouse_GIMBL = -1.0f; else mouse_GIMBL = 1.0f;

//	if (abs(heading) > 90.0 ) mouse_GIMBL = -1.0f; else mouse_GIMBL = 1.0f;

if (heading > 90.0   ) { heading = 90.0;}
if (heading < -90.0  ) { heading = -90.0; }

if (glop    > 360  )    glop = 0;
if (glop    < -360 )    glop = 0;


}
}

Its something like that see what is the position of the cursor calculate difference (actual mouse cursor, center of the drawing window) then move cursor to the center of the drawing window

 

 

 

but you could use time in your move routine

So I assume you're calling this in you're idle function?

Share this post


Link to post
Share on other sites

yep

Unfortunately, OSX doesn't have a GetCursorPos, but I can look into an alternative, but if anyone did know it, that would be great.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!