Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix axis not effect when component changed && set connectedAnchor should not effect when automaticConnectedAnchor is true #2516

Merged
merged 4 commits into from
Jan 17, 2025

Conversation

luzhuang
Copy link
Contributor

@luzhuang luzhuang commented Jan 16, 2025

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

What is the current behavior? (You can also link to an open issue here)

What is the new behavior (if this is a feature change)?

Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)

Other information:

Summary by CodeRabbit

Release Notes

  • Improvements

    • Enhanced hinge joint axis handling for better consistency and normalization.
    • Streamlined management of connected anchors in joint components, improving clarity and control.
  • Bug Fixes

    • Adjusted behavior of hinge joint to accommodate non-normalized vectors, ensuring accurate calculations.
  • Tests

    • Added comprehensive checks for joint behavior regarding the automaticConnectedAnchor property.

Note: These changes primarily enhance the physics system's functionality and may not directly impact most end-users.

Copy link

coderabbitai bot commented Jan 16, 2025

Walkthrough

This pull request introduces modifications to the physics joint system, focusing on the HingeJoint and Joint classes. The changes primarily involve refining the axis handling for hinge joints and improving the management of connected anchors. A key modification is the addition of a new method _onAxisChanged in the HingeJoint class to normalize axis vectors and update the native joint's axis. The Joint class sees updates to its connected anchor handling, including a new method _handleConnectedAnchorChanged and modifications to the anchor calculation process.

Changes

File Change Summary
packages/core/src/physics/joint/HingeJoint.ts - Added _onAxisChanged private method
- Updated axis property comment
- Modified axis setter to use new method
packages/core/src/physics/joint/Joint.ts - Removed _updateConnectedActualAnchor
- Added _handleConnectedAnchorChanged method
- Updated _calculateConnectedAnchor method signature
packages/physics-physx/src/joint/PhysXHingeJoint.ts - Removed axis normalization in setAxis method
tests/src/core/physics/HingeJoint.test.ts - Changed axis setting to component-wise assignment
tests/src/core/physics/Joint.test.ts - Added assertions for automaticConnectedAnchor behavior

Possibly related PRs

Suggested reviewers

  • GuoLei1990

Poem

🐰 A Rabbit's Ode to Joint Mechanics 🔧

Hinges spinning, anchors tight,
Vectors dance with pure delight,
Normalized axis, smooth and clean,
Physics simulation's graceful scene!
Code leaps forward, physics refined! 🚀

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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.

@GuoLei1990 GuoLei1990 added bug Something isn't working physics Engine's physical system labels Jan 17, 2025
@GuoLei1990 GuoLei1990 changed the title Fix axis not effect when component changed and set connectedAnchor should not effect when set automaticConnectedAnchor Fix axis not effect when component changed && set connectedAnchor should not effect when set automaticConnectedAnchor Jan 17, 2025
@GuoLei1990 GuoLei1990 changed the title Fix axis not effect when component changed && set connectedAnchor should not effect when set automaticConnectedAnchor Fix axis not effect when component changed && set connectedAnchor should not effect when automaticConnectedAnchor is true Jan 17, 2025
@luzhuang luzhuang added the ignore for release ignore for release label Jan 17, 2025
Copy link

codecov bot commented Jan 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.03%. Comparing base (15e7b8b) to head (11201a7).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2516      +/-   ##
==========================================
+ Coverage   69.01%   69.03%   +0.01%     
==========================================
  Files         957      957              
  Lines      100052   100062      +10     
  Branches     8554     8560       +6     
==========================================
+ Hits        69054    69076      +22     
+ Misses      30742    30730      -12     
  Partials      256      256              
Flag Coverage Δ
unittests 69.03% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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: 0

🧹 Nitpick comments (3)
packages/core/src/physics/joint/Joint.ts (3)

Line range hint 238-252: Consider using a guard flag instead of handler removal

While the current implementation works, temporarily removing and reattaching event handlers could be fragile. Consider using a guard flag to prevent recursive updates instead.

 private _calculateConnectedAnchor(): void {
+  private _isCalculating = false;
   // ...
-  // @ts-ignore
-  connectedAnchor._onValueChanged = null;
+  if (this._isCalculating) return;
+  this._isCalculating = true;
   if (connectedCollider) {
     // ... calculation logic ...
   } else {
     // ... calculation logic ...
   }
-  // @ts-ignore
-  connectedAnchor._onValueChanged = this._handleConnectedAnchorChanged;
+  this._isCalculating = false;
   this._updateActualAnchor(AnchorOwner.Connected);
 }

254-260: LGTM! Clear handling of connected anchor changes

The implementation correctly prevents manual changes when automaticConnectedAnchor is true and provides appropriate feedback.

Consider enhancing the warning message to be more informative:

-      console.warn("Cannot set connectedAnchor when automaticConnectedAnchor is true.");
+      console.warn("Cannot manually set connectedAnchor when automaticConnectedAnchor is true. Set automaticConnectedAnchor to false first if you need manual control.");

Line range hint 67-74: Add JSDoc documentation for the updated behavior

The connectedAnchor property's behavior has changed significantly, but the documentation hasn't been updated to reflect these changes. Please update the JSDoc comments to:

  1. Explain the warning when setting connectedAnchor with automaticConnectedAnchor enabled
  2. Document the simplified getter behavior
 /**
  * The connected anchor position.
  * @remarks If connectedCollider is set, this anchor is relative offset, or the anchor is world position.
- * The connectedAnchor is automatically calculated, if you want to set it manually, please set automaticConnectedAnchor to false
+ * The connectedAnchor is automatically calculated when automaticConnectedAnchor is true.
+ * To manually set the connectedAnchor:
+ * 1. Set automaticConnectedAnchor to false
+ * 2. Set your desired connectedAnchor value
+ * @throws Warns if attempting to set connectedAnchor while automaticConnectedAnchor is true
  */
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 02f961a and 11201a7.

📒 Files selected for processing (2)
  • packages/core/src/physics/joint/Joint.ts (4 hunks)
  • tests/src/core/physics/Joint.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/src/core/physics/Joint.test.ts
🔇 Additional comments (2)
packages/core/src/physics/joint/Joint.ts (2)

73-73: LGTM! Simplified getter implementation

The simplified getter improves code clarity by removing unnecessary calculations and deferring the automatic anchor handling to the dedicated method.


183-185: LGTM! Proper event handler initialization

The initialization correctly sets up the connected anchor change handling with proper context binding.

@GuoLei1990 GuoLei1990 merged commit 6e3ba7e into galacean:main Jan 17, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ignore for release ignore for release physics Engine's physical system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants