Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/pointer-lock-movement/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pointer-lock-movement",
"version": "0.1.9",
"version": "0.2.0",
"author": "Zheeeng <hi@zheeeng.me>",
"description": "A pointer lock movement manager for customizing your own creative UI.",
"keywords": [
Expand Down
44 changes: 41 additions & 3 deletions packages/pointer-lock-movement/src/pointer-lock-movement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import { requestScreen, clearScreen } from './utils/requestScreen'
import { requestCursor, clearCursor } from './utils/requestCursor'

const HISTORY_LENGTH = 8
const CONFIDENCE_THRESHOLD = 2.0

export type MoveState = {
status: 'moving' | 'stopped',
movementX: number,
Expand Down Expand Up @@ -75,19 +78,52 @@ export const pointerLockMovement = (
startY: number,
x: number,
y: number,
historyX: number[],
historyY: number[],
movementX: number,
movementY: number,
maxWidth: number,
maxHeight: number
}

const move: CoData<MoveContext, PointerEvent> = (context, effect) => payload => {
const contextPatch: Pick<MoveContext, 'event' | 'movementX' | 'movementY' | 'x' | 'y' | 'status'> = {
let { movementX, movementY } = payload

const historyX = [...context.historyX, movementX].slice(-HISTORY_LENGTH)
const historyY = [...context.historyY, movementY].slice(-HISTORY_LENGTH)

const shouldValidate = historyX.length === HISTORY_LENGTH && historyY.length === HISTORY_LENGTH

if (shouldValidate) {
const averageX = historyX.reduce((sum, value) => sum + value, 0) / historyX.length
const averageY = historyY.reduce((sum, value) => sum + value, 0) / historyY.length

const standardDeviationX = Math.sqrt(historyX.reduce((sum, value) => sum + Math.pow(value - averageX, 2), 0) / historyX.length)
const standardDeviationY = Math.sqrt(historyY.reduce((sum, value) => sum + Math.pow(value - averageY, 2), 0) / historyY.length)

if (standardDeviationX > 0) {
const zScoreX = Math.abs(movementX - averageX) / standardDeviationX
if (zScoreX > CONFIDENCE_THRESHOLD) {
movementX = averageX
}
}

if (standardDeviationY > 0) {
const zScoreY = Math.abs(movementY - averageY) / standardDeviationY
if (zScoreY > CONFIDENCE_THRESHOLD) {
movementY = averageY
}
}
}

const contextPatch: Pick<MoveContext, 'event' | 'movementX' | 'movementY' | 'x' | 'y' | 'status' | 'historyX' | 'historyY'> = {
event: payload,
movementX: payload.movementX,
movementY: payload.movementY,
movementX,
movementY,
x: context.x + context.movementX,
y: context.y + context.movementY,
historyX,
historyY,
Comment on lines +119 to +126
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Likely logic bug when computing the new position.

You use context.x + context.movementX and context.y + context.movementY instead of the newly computed (possibly clamped) movementX and movementY. This discrepancy may ignore the corrected values meant to smooth out erratic spikes. To fix, reference the updated local variables in the position calculation:

-  x: context.x + context.movementX,
-  y: context.y + context.movementY,
+  x: context.x + movementX,
+  y: context.y + movementY,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const contextPatch: Pick<MoveContext, 'event' | 'movementX' | 'movementY' | 'x' | 'y' | 'status' | 'historyX' | 'historyY'> = {
event: payload,
movementX: payload.movementX,
movementY: payload.movementY,
movementX,
movementY,
x: context.x + context.movementX,
y: context.y + context.movementY,
historyX,
historyY,
const contextPatch: Pick<MoveContext, 'event' | 'movementX' | 'movementY' | 'x' | 'y' | 'status' | 'historyX' | 'historyY'> = {
event: payload,
movementX,
movementY,
x: context.x + movementX,
y: context.y + movementY,
historyX,
historyY,

status: 'moving',
}

Expand Down Expand Up @@ -191,6 +227,8 @@ export const pointerLockMovement = (
startY: pointerEvent.clientY,
movementX: 0,
movementY: 0,
historyX: [],
historyY: [],
x: pointerEvent.clientX - virtualScreen.x,
y: pointerEvent.clientY - virtualScreen.y,
maxWidth: virtualScreen.width,
Expand Down