Setting mouse coordinates works wrong in rare cases

I made a constraint windows inside my viewport for mouse. E.g. viewport size is 1920x1080, and allowed size is 0.6 of each dimension in both X and Y. The logic is if mouse coord > end or < start, we set it to end or start accordingly. And here is the deal. If mouse on some tick is below bottom border of the window (mouse.Y > end_Y), I set mouse.Y to end_Y and mouse.X is untouched. But after this my mouse moves one pixel left! It happens only on the bottom border. Other sides behaves well. So, if I move my mouse physically vertically bottom, my mouse moves to the left on the windows border!

The workaround for me is simply to add +1 to mouse.X when on the bottom border. The repro code is simple as hell. Put this inside your actor’s tick function when controlling this actor.

class AMyPawn : public APawn { ... };

void AMyPawn::Tick(float DeltaTime)
{

Super::Tick(DeltaTime);

FVector2D Position;
if (PlayerController->GetMousePosition(Position.X, Position.Y))
{
    auto Viewport = GetWorld()->GetGameViewport();
    FIntPoint ViewSize = Viewport->Viewport->GetSizeXY();
    int end = (int)(ViewSize.Y * 0.6);
    if (Position.Y > end)
        Position.Y = end;
    Viewport->Viewport->SetMouse(Position.X, Position.Y);
    return;
}

}

Hey -

What class was this code added to for your repro case? When I tried adding the code to a class it caused the editor to crash on PIE when I added an instance of the class to the level. Please elaborate on your reproduction steps to provide more information.

Cheers

Hi, I edited my code. Does it look better now for you?

Adding the code to a pawn class still caused the editor to crash on PIE. If you have a simple project that reproduces the issue could you post the project here as a zip file for me to test directly?

This is getting better and better. I even see mouse moving left when GetPos(), SetThisPos(). I prepared a test project for you. Just run project in editor and press play (PIE). Code is located in the MouseBugGameMode.cpp.link text

I don’t think I’m seeing the result you’re reporting. When I repeatedly move the mouse up/down over the bottom boarder of the viewport I don’t see the mouse moving to the left. After un-commenting the lines of code in the GameMode.cpp the mouse still seemed to roam the full viewport (rather than stopping .6 of the Y)

69288-mousedrift.png

This was the result in the output when when I tried moving the mouse out of the viewport. If you are seeing a major difference can you take a short video to upload so I can see exactly what you are seeing?

Ok, here is the video. Before/after output for me stays equal. But somewhere somehow between frames the mouse pos.x sets to pos.x-1.

In the video it appears that the mouse is jumping vertically as you move it horizontally, is that correct? I don’t see this behavior in the sample project you provided. Do you have the same effect happen if you use this code in a 4.9 project as well?

lol, jumping means I move mouse up and down.

MY MOUSE MOVES LEFT ON ITS OWN!!!

And I did not try it on 4.9 and I’m not able to test it. Do not want downgrade and test. The code was written already on 4.10.

Ahh, okay then. My understanding was that the mouse was drifting in the x only when it was being moved against the border in the y direction. Does the mouse drift even when there is no input (when you’re not touching the mouse). If so can you check if you have any other peripherals plugged in that could be providing input (such as a controller)?

Just checked on 4.10.1, all is fine in both projects (main and test). Probably we can close this Q.