Skip to content

Commit

Permalink
physics2d supports joint2d apply (#16299)
Browse files Browse the repository at this point in the history
* support joint2d apply

* tweak
  • Loading branch information
lealzhan authored Sep 21, 2023
1 parent 2a44a31 commit df58eb6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
7 changes: 7 additions & 0 deletions cocos/physics-2d/box2d-wasm/joints/joint-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions cocos/physics-2d/box2d/joints/joint-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
14 changes: 13 additions & 1 deletion cocos/physics-2d/framework/components/joints/joint-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
}
}
1 change: 1 addition & 0 deletions cocos/physics-2d/framework/physics-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ const ENTIRE_JOINT: IEntireJoint = {
impl: null,

initialize: FUNC,
apply: FUNC,

setDampingRatio: FUNC,
setFrequency: FUNC,
Expand Down
4 changes: 1 addition & 3 deletions cocos/physics-2d/spec/i-physics-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit df58eb6

Please sign in to comment.