Skip to content

feat: weird value rejection#5

Merged
zheeeng merged 2 commits intomainfrom
bump
Dec 24, 2024
Merged

feat: weird value rejection#5
zheeeng merged 2 commits intomainfrom
bump

Conversation

@zheeeng
Copy link
Owner

@zheeeng zheeeng commented Dec 24, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced pointer lock movement functionality for smoother and more controlled pointer movements.
    • Added statistical validation for movement data to improve responsiveness.
  • Version Updates

    • Updated the version of the pointer-lock-movement package from 0.1.9 to 0.2.0.

@coderabbitai
Copy link

coderabbitai bot commented Dec 24, 2024

Walkthrough

The pull request introduces improvements to the pointer lock movement functionality by adding statistical validation for movement data. The changes include defining constants for history length and confidence threshold, updating the MoveContext type to track movement history, and implementing a mechanism to smooth out erratic movements by calculating z-scores and adjusting movement values when they exceed a predefined threshold.

Changes

File Change Summary
packages/pointer-lock-movement/package.json Version bumped from 0.1.9 to 0.2.0
packages/pointer-lock-movement/src/pointer-lock-movement.ts - Added HISTORY_LENGTH and CONFIDENCE_THRESHOLD constants
- Updated MoveContext type with historyX and historyY
- Enhanced move function with movement validation logic

Sequence Diagram

sequenceDiagram
    participant User
    participant PointerLock
    participant MovementValidator

    User->>PointerLock: Activate Pointer Lock
    PointerLock->>MovementValidator: Initialize History Arrays
    User->>PointerLock: Move Pointer
    PointerLock->>MovementValidator: Capture Movement Data
    MovementValidator->>MovementValidator: Calculate Z-Scores
    alt Movement Exceeds Threshold
        MovementValidator->>PointerLock: Adjust Movement
    else Movement Within Threshold
        MovementValidator->>PointerLock: Pass Original Movement
    end
Loading

Poem

🐰 A Rabbit's Ode to Smooth Moves 🖱️

With history's lens and z-scores bright,
Our pointer dances, smooth and light
No jittery paths or wild terrain
Statistical magic breaks the chain
Precision leaps from chaos' might!

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (4)
packages/pointer-lock-movement/src/pointer-lock-movement.ts (4)

7-8: Consider making these constants configurable.

By hardcoding HISTORY_LENGTH and CONFIDENCE_THRESHOLD, you limit runtime flexibility. If different environments or specific user requirements call for varying thresholds, consider exposing these constants as configuration parameters.


90-94: Possible performance concern with slicing arrays repeatedly.

Slicing and spreading arrays ([...context.historyX, movementX].slice(-HISTORY_LENGTH)) is not necessarily a big overhead at your current scale, but under high-frequency pointer events, a more efficient data structure (e.g., a fixed-size ring buffer) might be beneficial.


95-117: Z-score approach is straightforward, but consider alternative smoothing.

Clamping the movement to the average upon crossing CONFIDENCE_THRESHOLD can effectively reduce noise, yet it might feel jarring if the pointer “snaps” to the average. Consider applying a softer correction, like exponential smoothing, to gradually converge unusual spikes toward a target value instead of abruptly forcing them all the way to the average.


230-231: Initialization of history arrays is correct, but verify usage upon re-locking.

Clearing the movement history on each new lock is sensible for a fresh start. If the application requires persistent smoothing across multiple lock sessions, consider preserving or merging the prior history.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 747f32a and 6d2a514.

📒 Files selected for processing (2)
  • packages/pointer-lock-movement/package.json (1 hunks)
  • packages/pointer-lock-movement/src/pointer-lock-movement.ts (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/pointer-lock-movement/package.json
🔇 Additional comments (1)
packages/pointer-lock-movement/src/pointer-lock-movement.ts (1)

81-82: Context extension looks good.

Adding historyX and historyY to persist the movement history is a straightforward way to track outliers. This extension of the context seems coherent with the new smoothing logic.

Comment on lines +119 to +126
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,
Copy link

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,

@zheeeng zheeeng merged commit c92f420 into main Dec 24, 2024
2 checks passed
@zheeeng zheeeng deleted the bump branch December 24, 2024 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant