Grasshopper's picture

Mouse Navigation - set mouse pointer position


I want to use mouse movement to control the view rotation (like in a fps).
Therefore I want to set the mouse pointer to the middle of the screen after every movement.
How can I do that?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

You can use:

System.Windows.Forms.Cursor.Position = new Point(Width / 2, Height / 2);
Grasshopper's picture

Thank you - but it seems my idea will not work anyway.

I get the following mouse movements(x y):

0.02816901 0.02087683
-0.02816901 -0.02087683       set back to middle
0 0
0.04381847 0
0.03755869 -0.004175365
-0.08137715 0.004175365       set back to middle
0 0
0.01877934 0
-0.009389671 0        set back to middle
-0.009389671 0        set back to middle

The first one would be easy to ignore but second and third...

Any ideas on how to do that? Maybe a different approach?

Code looks like this:

      float z = -(float)Math.Sqrt(1.0f - deltaX * deltaX - deltaY * deltaY);
      _helper.X = deltaX;
      _helper.Y = deltaY;
      _helper.Z = z;
      float angle = (float)Math.Acos(Vector3.Dot(_helper, _viewDirection));
      if (angle > 0.00001f)
        Vector3.Cross(ref _helper, ref _viewDirection, out _axis);
        _viewMatrix = Matrix4.Mult(_viewMatrix, Matrix4.Rotate(_axis, angle));
the Fiddler's picture

I hit the same problem a few days ago and I'm still searching how to solve it inside OpenTK.

Until then, try the following:

Vector2 mouse_delta = new Vector2();
public override void OnUpdateFrame(UpdateFrameEventArgs e)
    mouse_delta += new Vector2(
        System.Windows.Forms.Cursor.Position.X - Width / 2,
        System.Windows.Forms.Cursor.Position.Y - Height / 2);
    System.Windows.Forms.Cursor.Position = new Point(Width / 2, Height / 2);

This seems to work fine on both runtimes and should serve as a temporary workaround.

Grasshopper's picture

ah that works :)

How do you hide the cursor?


Does not do anything?

the Fiddler's picture

Are you on Linux, by any chance? This call works here, but only on Windows (probably because GameWindow does not build on the standard WinForms infrastructure). Try setting an empty cursor icon instead.

Grasshopper's picture

You're right I'm on linux, I'll try your suggestion...