Smooth OpenGL mouse look

Started by
7 comments, last by BlueSpud 10 years, 8 months ago

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.

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.

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.

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.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

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

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.

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?

yep

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.

This topic is closed to new replies.

Advertisement