SFML 3D MouseLook

时间:2011-07-23 07:54:16

标签: c# opengl mouse sfml

EDIT2: 我弄清了大部分问题,但我有一个烦恼。当光标到达屏幕边缘并被拉到另一侧时,相机会抖动,这将无法工作。有人能看出如何阻止它吗?

    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;

    }

2 个答案:

答案 0 :(得分:1)

通常,您将鼠标位置设置为每帧的窗口中心。以前您读取鼠标位置并减去窗口的中心。通过这种方式,您可以轻松地获得每帧的鼠标移动,而无需担心窗口边框。

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

答案 1 :(得分:0)

我还在努力加速自己,所以请带上一粒盐。 (我正在努力!)

我认为你的鼠标移动是以像素为单位测量的。这转化为相机的完整旋转。除以0.4,(MOUSE_MOVEMENT),你正在影响“0.4完整转数”的某个倍数,(例如152像素/0.44 = 380转,让你面向你开始时的方向。)

尝试除以256而不是0.4&amp;看看它是否更好。

相关问题