Mouse Sensitivity Slider

Started by Dualnames, Wed 07/01/2015 09:06:46

Previous topic - Next topic

Dualnames

Right so I basically took a c++ script and moded it to AGS. The issue is that on higher sensitivites the movement is weird, it doesn't jitter in any value of sensitivity, but the movement appears to be "laggy" even though the game runs at 40 fps constant. However increasing the fps to 60 by using SetGameSpeed seems to fix the lag yet there are, regardless of game speed, some weird unexpected jumps. Any ways to make this better?  I really don't wanna re-arrange the entire game at 60fps.

Code: ags

float sensitivity = 2.0;

float ox;
float oy;

function repeatedly_execute_always()
{

   if(mouse.x != FloatToInt(ox, eRoundNearest) || mouse.y != FloatToInt(oy, eRoundNearest))
   {
    // Mouse has moved
    // Work out the mouse movementinteger    
    if (Game.DoOnceOnly("set_fx"))
    {
      ox= IntToFloat(mouse.x);
      oy= IntToFloat(mouse.y);
      mouse.Visible=false;
      return;
    }    
    float x = IntToFloat(mouse.x);
    float y = IntToFloat(mouse.y);    
    float nx = ox + ((x - ox) * sensitivity);
    float ny = oy + ((y - oy) * sensitivity);
    
     Mouse.SetPosition(FloatToInt(nx, eRoundNearest), FloatToInt(ny, eRoundNearest));
     mouse.Update();
 
     bmouse.Visible=true;
     bmouse.NormalGraphic = mouse.GetModeGraphic(mouse.Mode);
     bmouse.SetPosition(mouse.x, mouse.y);
     // Set old location ready for next time
     ox = nx;
     oy = ny;    
  }  
  
  
  game.debug_mode = true;
  Debug(4, 1);

  
}
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

monkey0506

It seems to me that perhaps the issue is that you are trying to set coordinates for the mouse that are out-of-bounds and so the engine is (silently) forcing it back into bounds which your code isn't accounting for. I slightly modified your code to the following, and both the visible feedback and the log I'm writing look fine:

Code: ags
float sensitivity = 2.0;
float ox;
float oy;

bool IsNullOrError(static File, File *file)
{
  return ((file == null) || (file.Error));
}

void LogCoordinates(this Mouse*)
{
  String fileName = "mouse.log";
  File *f;
  if (Game.DoOnceOnly("Create mouse log"))
  {
    f = File.Open(fileName, eFileWrite);
  }
  else f = File.Open(fileName, eFileAppend);
  if (File.IsNullOrError(f)) AbortGame("Error opening mouse log!");
  f.WriteRawLine(String.Format("Mouse: (%d, %d)", this.x, this.y));
  f.Close();
}

function repeatedly_execute_always()
{
  if ((mouse.x != FloatToInt(ox, eRoundNearest)) || (mouse.y != FloatToInt(oy, eRoundNearest)))
  {
    // Mouse has moved
    // Work out the mouse movementinteger
    if (Game.DoOnceOnly("set_fx"))
    {
      ox = IntToFloat(mouse.x);
      oy = IntToFloat(mouse.y);
      mouse.Visible = false;
      return;
    }
    float x = IntToFloat(mouse.x);
    float y = IntToFloat(mouse.y);
    float nx = ox + ((x - ox) * sensitivity);
    float ny = oy + ((y - oy) * sensitivity);
    int xx = FloatToInt(nx, eRoundNearest);
    int yy = FloatToInt(ny, eRoundNearest);
    if (xx < 0)
    {
      xx = 0;
      nx = 0.0;
    }
    if (xx > System.ViewportWidth)
    {
      xx = System.ViewportWidth;
      nx = IntToFloat(System.ViewportWidth);
    }
    if (yy < 0)
    {
      yy = 0;
      ny = 0.0;
    }
    if (yy > System.ViewportHeight)
    {
      yy = System.ViewportHeight;
      ny = IntToFloat(System.ViewportHeight);
    }
    mouse.SetPosition(xx, yy);
    mouse.Update();
    mouse.LogCoordinates();
    bmouse.Visible = true;
    bmouse.NormalGraphic = mouse.GetModeGraphic(mouse.Mode);
    bmouse.SetPosition(mouse.x, mouse.y);
    // Set old location ready for next time
    ox = nx;
    oy = ny;
  }
  game.debug_mode = true;
  Debug(4, 1);
}


In particular, note that I have checked that the results of IntToFloat(nx, eRoundNearest) and IntToFloat(ny, eRoundNearest) fall within the bounds of 0..System.ViewportWidth and 0..System.ViewportHeight, respectively, and if not that I have forced nx and ny into the proper bounds.

Dualnames

Unfortunately the issue remains, try making a circle or a rectangle with your mouse, better yet try to follow a rectangle or a circle, and see that it will jump around. :(
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Dualnames

Should I upload the game to point out what I'm saying?
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Dualnames

Also concerning this whole sensitivity issue, why are we not using "void set_mouse_speed(int xspeed, int yspeed);" from Allegro?
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

SMF spam blocked by CleanTalk