diff --git a/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts b/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts index 0f4c964861b..9d28ebc03e3 100644 --- a/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts +++ b/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts @@ -62,6 +62,13 @@ export class B2Joint implements IJoint2D { PhysicsSystem2D.instance._callAfterStep(this, this._init); } + apply (): void { + PhysicsSystem2D.instance._callAfterStep(this, this.destroy); + if (this.comp!.enabledInHierarchy) { + PhysicsSystem2D.instance._callAfterStep(this, this._init); + } + } + _init (): void { if (this._inited) return; diff --git a/cocos/physics-2d/box2d/joints/joint-2d.ts b/cocos/physics-2d/box2d/joints/joint-2d.ts index 8aa60b35467..9cf1d9b7087 100644 --- a/cocos/physics-2d/box2d/joints/joint-2d.ts +++ b/cocos/physics-2d/box2d/joints/joint-2d.ts @@ -62,6 +62,13 @@ export class b2Joint implements IJoint2D { PhysicsSystem2D.instance._callAfterStep(this, this._init); } + apply (): void { + PhysicsSystem2D.instance._callAfterStep(this, this._destroy); + if (this.comp!.enabledInHierarchy) { + PhysicsSystem2D.instance._callAfterStep(this, this._init); + } + } + _init (): void { if (this._inited) return; diff --git a/cocos/physics-2d/framework/components/joints/joint-2d.ts b/cocos/physics-2d/framework/components/joints/joint-2d.ts index 57e2d0285a0..dd4653afc0e 100644 --- a/cocos/physics-2d/framework/components/joints/joint-2d.ts +++ b/cocos/physics-2d/framework/components/joints/joint-2d.ts @@ -81,7 +81,7 @@ export class Joint2D extends Component { * @zh * 关节所绑定的刚体组件。 */ - _body: RigidBody2D | null = null + _body: RigidBody2D | null = null; get body (): RigidBody2D | null { return this._body; } @@ -132,4 +132,16 @@ export class Joint2D extends Component { this._joint.onDestroy(); } } + + /** + * @en + * If the physics engine is box2d, need to call this function to apply current changes to joint, this will regenerate inner box2d joint. + * @zh + * 如果物理引擎是 box2d, 需要调用此函数来应用当前 joint 中的修改。 + */ + apply (): void { + if (this._joint && this._joint.apply) { + this._joint.apply(); + } + } } diff --git a/cocos/physics-2d/framework/physics-selector.ts b/cocos/physics-2d/framework/physics-selector.ts index 958ae1da47b..443f778831d 100644 --- a/cocos/physics-2d/framework/physics-selector.ts +++ b/cocos/physics-2d/framework/physics-selector.ts @@ -297,6 +297,7 @@ const ENTIRE_JOINT: IEntireJoint = { impl: null, initialize: FUNC, + apply: FUNC, setDampingRatio: FUNC, setFrequency: FUNC, diff --git a/cocos/physics-2d/spec/i-physics-joint.ts b/cocos/physics-2d/spec/i-physics-joint.ts index 67b5d7cd999..ae23b558420 100644 --- a/cocos/physics-2d/spec/i-physics-joint.ts +++ b/cocos/physics-2d/spec/i-physics-joint.ts @@ -22,15 +22,13 @@ THE SOFTWARE. */ - - import { IVec2Like } from '../../core'; import { ILifecycle } from '../../physics/spec/i-lifecycle'; import { Joint2D, RigidBody2D } from '../framework'; export interface IJoint2D extends ILifecycle { readonly impl: any; - + apply (): void; initialize (v: Joint2D): void; }