【问题标题】:SFML 3D MouseLookSFML 3D 鼠标外观
【发布时间】:2011-10-11 13:28:26
【问题描述】:

编辑2: 我发现了大部分问题,但我有一个烦恼。当光标到达屏幕边缘并被拉到另一侧时,相机会抖动,这将不起作用。有人可以看看如何阻止这种情况吗?

    bool attention = true;
    Vector2 p, mousePos;
    private float MOUSE_SENSITIVITY = 4.0f;

    private void OnMouseMove(object sender, MouseMoveEventArgs e)
    {
        float DeltX = 0, DeltY = 0;
        int border = 2;
        Console.WriteLine(attention + "");

        if (attention == true)
        {
            p.X = e.X;
            p.Y = e.Y;

            DeltX = (float)(mousePos.X - e.X) / MOUSE_SENSITIVITY;
            DeltY = (float)(mousePos.Y - e.Y) / MOUSE_SENSITIVITY;
        }
        else
        {
            mousePos = p;
        }

        attention = true;

        if (e.X > App.Width - border)
        {
            attention = false;
            App.SetCursorPosition((uint)border, (uint)e.Y);
            DeltX = 0;
            DeltY = 0;

        }
        else if (e.X < border)
        {
            attention = false;
            App.SetCursorPosition((uint)(App.Width - border), (uint)e.Y);
            DeltX = 0;
            DeltY = 0;

        }

        if (e.Y > App.Height - border)
        {
            attention = false;
            App.SetCursorPosition((uint)e.X, (uint)border);
            DeltX = 0;
            DeltY = 0;

        }
        else if (e.Y < border)
        {
            attention = false;
            App.SetCursorPosition((uint)e.X, (uint)(App.Height - border));
            DeltX = 0;
            DeltY = 0;

        }



        Cam.RotateY(DeltX);
        Cam.RotateX(DeltY);


        mousePos = p;

    }

【问题讨论】:

    标签: c# opengl mouse sfml


    【解决方案1】:

    我还在加快速度,所以请谨慎对待。 (我在努力!)

    我认为您的鼠标移动是以像素为单位测量的,这意味着相机的完整旋转。通过除以 0.4,(MOUSE_MOVEMENT,),您将影响“0.4 完整转”的一些倍数,(例如 152 像素 / .04 = 380 转,让您面对与开始时相同的方向。)

    尝试除以 256 而不是 0.4,看看效果是否更好。

    【讨论】:

    • 这是个好主意,虽然我已经想通了,但我只是使用了一段环绕代码,所以当鼠标靠近边缘时,它会回到另一个侧面,它似乎工作得很好。
    • 我在发布这篇文章后意识到我在考虑旧引擎(从 90 年代开始)的工作方式。我忘记了 OpenGL 使用度数进行旋转。
    • 一定有一种方法可以在比屏幕坐标更低的级别轮询鼠标,你不觉得吗?如果是这样,你就不能检查鼠标移动,不管光标是否在屏幕边缘?
    • 我修复了它,我只是检查了跳跃是否大于 150 或 200 像素它忽略了它,所以当它从屏幕的一侧跳到另一侧时它会忽略它。
    【解决方案2】:

    通常您将鼠标位置设置为每一帧的窗口中心。以前您读取到鼠标位置并减去窗口的中心。这样您就可以轻松地在每一帧中获得鼠标移动,而不必担心窗口边框。

    Vector2i center(window->getSize().x / 2, window->getSize().y / 2);
    Vector2i delta = Mouse::getPosition(*window) - center;
    Mouse::setPosition(center, *window);
    

    【讨论】:

      猜你喜欢
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      • 1970-01-01
      相关资源
      最近更新 更多