简体   繁体   中英

SFML 3D MouseLook

Edit2: I figured out most of the problem, but there is one annoyance that I have. When the cursor reaches the edge of the screen and is pulled to the other side, the camera jerks, which won't work. Can someone see how that could be stopped?

    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;

    }

Typically you set the mouse position to the center of the window each frame. Previously you read to mouse position and subtract the center of the window. This way you can easily get the mouse movement each frame without having to worry about window borders.

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

I'm still getting up to speed myself, so please take this with a grain of salt. (I'm trying!)

I think your mouse movement is measured in pixels & this translates to complete revolutions of the camera. By dividing by 0.4, (MOUSE_MOVEMENT,) you're affecting some multiple of "0.4 complete revolutions", (eg 152 pixels /.04 = 380 revolutions, leaving you facing the same direction as you started in.)

Try dividing by 256 instead of 0.4 & see if that works better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM