Fix mouse movement calculation to be pointer-lock aware#8681
Draft
4ian wants to merge 2 commits into
Draft
Conversation
The browser's MouseEvent.movementX/Y are only reliable while the pointer is locked. When not locked, they are inconsistent across browsers: notably Firefox adds the game canvas offset to them, so a first-person camera keeps drifting/sticking (e.g. stuck looking up) unless the game runs fullscreen. Now the movement deltas from the browser are only used when the pointer is locked (where the cursor position is frozen). Otherwise the InputManager derives the movement from the change of the cursor position, which is consistent across browsers and already expressed in game resolution units. The reference position is invalidated when the pointer lock state changes or when the mouse leaves/enters the canvas, to avoid reporting a spurious jump. Relates to #6338.
Now that InputManager.getMouseMovementX/Y is itself pointer-lock aware (browser deltas when locked, cursor position delta otherwise), the in-game editor no longer needs to branch on the pointer lock state or track the previous cursor position itself. This removes the duplicated ternary in both editor camera controllers, the now-unused renderer locals and the _lastCursorX/_lastCursorY bookkeeping.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes mouse movement tracking in the InputManager to properly handle both locked and unlocked pointer states. Previously, the code relied on browser-provided
movementX/Yvalues unconditionally, which are unreliable across browsers when the pointer is not locked. Now the InputManager intelligently switches between using browser movement deltas (when pointer is locked) and computing movement from cursor position deltas (when pointer is not locked).This was done previously only in the 3D editor (which was simplified).
To check: