Skip to content

[FIX] Fix XrInputSource crash when grip unavailable#8389

Merged
willeastcott merged 1 commit into
mainfrom
grip-fix
Jan 20, 2026
Merged

[FIX] Fix XrInputSource crash when grip unavailable#8389
willeastcott merged 1 commit into
mainfrom
grip-fix

Conversation

@willeastcott

@willeastcott willeastcott commented Jan 20, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fixed TypeError: Cannot read properties of null (reading 'setTRS') crash in VR mode
  • getPosition() and getRotation() now correctly return null when grip data is not yet available, as documented
  • Added defensive null checks in XrControllers script

Technical Details

The getPosition() and getRotation() methods had incorrect null guards:

// Before (broken)
getPosition() {
    if (!this._position) return null;  // _position is always initialized, never null
    this._updateTransforms();          // crashes when _localTransform is null
}

// After (fixed)
getPosition() {
    if (!this._grip) return null;      // correctly checks if grip data exists
    this._updateTransforms();
}

The crash occurred when getPosition() was called before the first valid gripPose was received from WebXR. The _localTransform matrix is only created when _grip becomes true, but the old guard checked _position which is always a valid Vec3.

This aligns the implementation with the existing JSDoc contract: "Get the world space position of input source if it is handheld (grip is true). Otherwise it will return null."

Checklist

  • I have read the contributing guidelines
  • My code follows the project's coding standards
  • This PR focuses on a single change

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a crash that occurred when getPosition() or getRotation() was called on an XrInputSource before grip data became available from WebXR. The issue was caused by incorrect null guards that checked always-initialized member variables instead of the grip availability flag.

Changes:

  • Fixed null guards in getPosition() and getRotation() to check _grip instead of _position/_rotation
  • Added defensive null checks in XrControllers script before calling setPosition() and setRotation()

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/framework/xr/xr-input-source.js Fixed null guards to properly check grip availability before accessing transforms
scripts/esm/xr-controllers.mjs Added defensive null checks when applying position and rotation to controller entities

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Maksims

Maksims commented Jan 20, 2026

Copy link
Copy Markdown
Collaborator

Good catch! 🙏

@willeastcott willeastcott merged commit ca18e88 into main Jan 20, 2026
13 checks passed
@willeastcott willeastcott deleted the grip-fix branch January 20, 2026 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: xr XR related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants