Skip to content

fix(physics-2d): account for scale in Box2D joint anchors#138

Open
orchid-world wants to merge 1 commit into
cocos:v4.0.0from
orchid-world:v4.0.0-fix/box2d-joint-anchor-scale
Open

fix(physics-2d): account for scale in Box2D joint anchors#138
orchid-world wants to merge 1 commit into
cocos:v4.0.0from
orchid-world:v4.0.0-fix/box2d-joint-anchor-scale

Conversation

@orchid-world

@orchid-world orchid-world commented May 20, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Apply each rigid body's node.worldScale when converting Joint2D.anchor and connectedAnchor to Box2D local anchor units.
  • Rebuild joints attached to or connected with a rigid body when that body's scale changes at runtime.
  • Add tests for scaled 2D joint anchors and runtime scale updates.

Issue source: https://forum.cocos.org/t/topic/121887

Re: #

Changelog

  • Fixed Box2D 2D joint anchor offset when rigid body nodes are scaled.

Re: #

Changelog

  • Fixed Box2D 2D joint anchor offset when rigid body nodes are scaled.

Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

- Apply node worldScale to Joint2D anchor and connectedAnchor
- Rebuild affected joints when rigid body scale changes
- Add 2D joint scale and runtime scale tests
@github-actions

Copy link
Copy Markdown

Code Size Check Report

Wechat (WASM) Before After Diff
2D Empty (legacy pipeline) 1014386 bytes 1014386 bytes ✅ 0 bytes
2D All (legacy pipeline) 2681399 bytes 2682043 bytes ⚠️ +644 bytes
2D All (new pipeline) 2773169 bytes 2773810 bytes ⚠️ +641 bytes
(2D + 3D) All 10030380 bytes 10031156 bytes ⚠️ +776 bytes
Web (WASM + ASMJS) Before After Diff
(2D + 3D) All 16866793 bytes 16867569 bytes ⚠️ +776 bytes

Interface Check Report

! WARNING this pull request has changed these public interfaces:

@@ -48192,8 +48192,28 @@
          */
         connectedBody: RigidBody2D | null;
         /**
          * @en
+         * Gets the attached body's anchor after node scale is applied, in Box2D local units.
+         * @zh
+         * 获取自身刚体锚点。返回值已经按节点世界缩放换算,并转换为 Box2D 使用的物理单位。
+         * @param out @en Optional output vector. A new Vec2 is created when it is not provided. @zh 可选,输出向量。未传入时会创建新的 Vec2。
+         * @returns @en The scaled local anchor in Box2D units. @zh 已缩放并转换为物理单位的本地锚点。
+         * @internal
+         */
+        _getScaledLocalAnchorA(out?: math.Vec2): math.Vec2;
+        /**
+         * @en
+         * Gets the connected body's anchor after node scale is applied, in Box2D local units.
+         * @zh
+         * 获取连接刚体锚点。连接刚体为空时保持世界原点静态刚体语义,不应用节点缩放;返回值会转换为 Box2D 物理单位。
+         * @param out @en Optional output vector. A new Vec2 is created when it is not provided. @zh 可选,输出向量。未传入时会创建新的 Vec2。
+         * @returns @en The scaled connected local anchor in Box2D units. @zh 已缩放并转换为物理单位的连接锚点。
+         * @internal
+         */
+        _getScaledLocalAnchorB(out?: math.Vec2): math.Vec2;
+        /**
+         * @en
          * the Joint2D attached rigid-body.
          * @zh
          * 关节所绑定的刚体组件。
          */
@@ -48207,8 +48227,17 @@
          * @zh
          * 此关节的类型。
          */
         TYPE: EJoint2DType;
+        /**
+         * @en
+         * Rebuilds all joints whose anchors may be affected by the rigid body's scale.
+         * @zh
+         * 重建所有会受指定刚体缩放影响的关节。
+         * @param body @en The scaled rigid body. @zh 发生缩放变化的刚体。
+         * @internal
+         */
+        static _applyScaleToConnectedJoints(body: RigidBody2D): void;
         protected onLoad(): void;
         protected onEnable(): void;
         protected onDisable(): void;
         protected start(): void;

@github-actions

Copy link
Copy Markdown

@orchid-world, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
web-mobile PASS PASS FAIL
ios PASS PASS PASS
mac PASS PASS PASS

@github-actions

Copy link
Copy Markdown

@orchid-world, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
windows PASS PASS PASS
android PASS PASS PASS
wechatgame PASS FAIL FAIL

@star-e star-e requested a review from HuazyYang June 2, 2026 10:22
@star-e star-e added the investigation Investigate the impact on the code base label Jun 3, 2026
@star-e

star-e commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

We may use shorter variable names to reduce code size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

investigation Investigate the impact on the code base

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants