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

physics2d supports joint2d apply #16299

Merged
merged 2 commits into from Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions cocos/physics-2d/box2d-wasm/joints/joint-2d.ts
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
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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why you need to check if .apply exists

Copy link
Contributor Author

Choose a reason for hiding this comment

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

builtin does not have this funciton

this._joint.apply();
}
}
}
1 change: 1 addition & 0 deletions cocos/physics-2d/framework/physics-selector.ts
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
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