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

Add box2d JSB codes to engine. #18090

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cc.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
"physics-2d-box2d-wasm": {
"modules": ["physics-2d-box2d-wasm", "physics-2d-framework"]
},
"physics-2d-box2d-jsb": {
"modules": ["physics-2d-box2d-jsb", "physics-2d-framework"]
},
"intersection-2d": {
"modules": ["intersection-2d"]
},
Expand Down
60 changes: 60 additions & 0 deletions cocos/physics-2d/box2d-jsb/instantiate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.

https://www.cocos.com/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

import { selector } from '../framework/physics-selector';
import { b2PhysicsWorld } from './physics-world';
import { b2RigidBody2D } from './rigid-body';
import { b2BoxShape } from './shapes/box-shape-2d';
import { b2CircleShape } from './shapes/circle-shape-2d';
import { b2PolygonShape } from './shapes/polygon-shape-2d';
import { b2MouseJoint } from './joints/mouse-joint';
import { b2DistanceJoint } from './joints/distance-joint';
import { b2SpringJoint } from './joints/spring-joint';
import { b2RelativeJoint } from './joints/relative-joint';
import { b2SliderJoint } from './joints/slider-joint';
import { b2FixedJoint } from './joints/fixed-joint';
import { b2WheelJoint } from './joints/wheel-joint';
import { b2HingeJoint } from './joints/hinge-joint';

import { Game, game } from '../../game';

game.once(Game.EVENT_PRE_SUBSYSTEM_INIT, () => {
selector.register('box2d', {
PhysicsWorld: b2PhysicsWorld,
RigidBody: b2RigidBody2D,

BoxShape: b2BoxShape,
CircleShape: b2CircleShape,
PolygonShape: b2PolygonShape,

MouseJoint: b2MouseJoint,
DistanceJoint: b2DistanceJoint,
SpringJoint: b2SpringJoint,
RelativeJoint: b2RelativeJoint,
SliderJoint: b2SliderJoint,
FixedJoint: b2FixedJoint,
WheelJoint: b2WheelJoint,
HingeJoint: b2HingeJoint,
});
});
46 changes: 46 additions & 0 deletions cocos/physics-2d/box2d-jsb/joints/distance-joint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
minggo marked this conversation as resolved.
Show resolved Hide resolved

https://www.cocos.com/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

//cjh import b2 from '@cocos/box2d';
minggo marked this conversation as resolved.
Show resolved Hide resolved
import { IDistanceJoint } from '../../spec/i-physics-joint';
import { b2Joint } from './joint-2d';
import { DistanceJoint2D } from '../../framework';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';

export class b2DistanceJoint extends b2Joint implements IDistanceJoint {
setMaxLength (v: number): void {
if (this._b2joint) {
(this._b2joint as b2.RopeJoint).SetMaxLength(v);

Check failure on line 34 in cocos/physics-2d/box2d-jsb/joints/distance-joint.ts

View workflow job for this annotation

GitHub Actions / npm_test

Cannot find namespace 'b2'.
}
}

_createJointDef (): any {
const comp = this._jointComp as DistanceJoint2D;
const def = new b2.RopeJointDef();

Check failure on line 40 in cocos/physics-2d/box2d-jsb/joints/distance-joint.ts

View workflow job for this annotation

GitHub Actions / npm_test

Cannot find name 'b2'.
def.localAnchorA = { x: comp.anchor.x / PHYSICS_2D_PTM_RATIO, y: comp.anchor.y / PHYSICS_2D_PTM_RATIO };
def.localAnchorB = { x: comp.connectedAnchor.x / PHYSICS_2D_PTM_RATIO, y: comp.connectedAnchor.y / PHYSICS_2D_PTM_RATIO };
def.maxLength = comp.maxLength / PHYSICS_2D_PTM_RATIO;
return def;

Check failure on line 44 in cocos/physics-2d/box2d-jsb/joints/distance-joint.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe return of an `any` typed value
}
}
53 changes: 53 additions & 0 deletions cocos/physics-2d/box2d-jsb/joints/fixed-joint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.

https://www.cocos.com/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

//cjh import b2 from '@cocos/box2d';
minggo marked this conversation as resolved.
Show resolved Hide resolved
import { IFixedJoint } from '../../spec/i-physics-joint';
import { b2Joint } from './joint-2d';
import { FixedJoint2D } from '../../framework';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';

export class b2FixedJoint extends b2Joint implements IFixedJoint {
setFrequency (v: number): void {
if (this._b2joint) {
(this._b2joint as b2.WeldJoint).SetFrequency(v);

Check failure on line 34 in cocos/physics-2d/box2d-jsb/joints/fixed-joint.ts

View workflow job for this annotation

GitHub Actions / npm_test

Cannot find namespace 'b2'.
}
}
setDampingRatio (v: number): void {
if (this._b2joint) {
(this._b2joint as b2.WeldJoint).SetDampingRatio(v);

Check failure on line 39 in cocos/physics-2d/box2d-jsb/joints/fixed-joint.ts

View workflow job for this annotation

GitHub Actions / npm_test

Cannot find namespace 'b2'.
}
}

_createJointDef (): any {
const comp = this._jointComp as FixedJoint2D;
const def = new b2.WeldJointDef();

Check failure on line 45 in cocos/physics-2d/box2d-jsb/joints/fixed-joint.ts

View workflow job for this annotation

GitHub Actions / npm_test

Cannot find name 'b2'.
def.localAnchorA = { x: comp.anchor.x / PHYSICS_2D_PTM_RATIO, y: comp.anchor.y / PHYSICS_2D_PTM_RATIO };
def.localAnchorB = { x: comp.connectedAnchor.x / PHYSICS_2D_PTM_RATIO, y: comp.connectedAnchor.y / PHYSICS_2D_PTM_RATIO };
def.referenceAngle = 0;
def.frequencyHz = comp.frequency;
def.dampingRatio = comp.dampingRatio;
return def;

Check failure on line 51 in cocos/physics-2d/box2d-jsb/joints/fixed-joint.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe return of an `any` typed value
}
}
83 changes: 83 additions & 0 deletions cocos/physics-2d/box2d-jsb/joints/hinge-joint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.

https://www.cocos.com/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

//cjh import b2 from '@cocos/box2d';
import { IHingeJoint } from '../../spec/i-physics-joint';
import { HingeJoint2D } from '../../framework';
import { b2Joint } from './joint-2d';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';
import { toRadian } from '../../../core';

export class b2HingeJoint extends b2Joint implements IHingeJoint {
enableLimit (v: boolean): void {
if (this._b2joint) {
(this._b2joint as b2.RevoluteJoint).EnableLimit(v);

Check failure on line 35 in cocos/physics-2d/box2d-jsb/joints/hinge-joint.ts

View workflow job for this annotation

GitHub Actions / npm_test

Cannot find namespace 'b2'.
}
}
setLowerAngle (v: number): void {
this.updateLimits();
}
setUpperAngle (v: number): void {
this.updateLimits();
}
updateLimits (): void {
if (this._b2joint) {
const comp = this._jointComp as HingeJoint2D;
(this._b2joint as b2.RevoluteJoint).SetLimits(toRadian(comp.lowerAngle), toRadian(comp.upperAngle));

Check failure on line 47 in cocos/physics-2d/box2d-jsb/joints/hinge-joint.ts

View workflow job for this annotation

GitHub Actions / npm_test

Cannot find namespace 'b2'.
}
}

// motor
enableMotor (v: boolean): void {
if (this._b2joint) {
(this._b2joint as b2.RevoluteJoint).EnableMotor(v);

Check failure on line 54 in cocos/physics-2d/box2d-jsb/joints/hinge-joint.ts

View workflow job for this annotation

GitHub Actions / npm_test

Cannot find namespace 'b2'.
}
}
setMaxMotorTorque (v: number): void {
if (this._b2joint) {
(this._b2joint as b2.RevoluteJoint).SetMaxMotorTorque(v);

Check failure on line 59 in cocos/physics-2d/box2d-jsb/joints/hinge-joint.ts

View workflow job for this annotation

GitHub Actions / npm_test

Cannot find namespace 'b2'.
}
}
setMotorSpeed (v: number): void {
if (this._b2joint) {
(this._b2joint as b2.RevoluteJoint).SetMotorSpeed(v);

Check failure on line 64 in cocos/physics-2d/box2d-jsb/joints/hinge-joint.ts

View workflow job for this annotation

GitHub Actions / npm_test

Cannot find namespace 'b2'.
}
}

_createJointDef (): any {
const comp = this._jointComp as HingeJoint2D;
const def = new b2.RevoluteJointDef();
def.localAnchorA = { x: comp.anchor.x / PHYSICS_2D_PTM_RATIO, y: comp.anchor.y / PHYSICS_2D_PTM_RATIO };
def.localAnchorB = { x: comp.connectedAnchor.x / PHYSICS_2D_PTM_RATIO, y: comp.connectedAnchor.y / PHYSICS_2D_PTM_RATIO };

def.enableMotor = comp.enableMotor;
def.maxMotorTorque = comp.maxMotorTorque;
def.motorSpeed = toRadian(comp.motorSpeed);

def.enableLimit = comp.enableLimit;
def.lowerAngle = toRadian(comp.lowerAngle);
def.upperAngle = toRadian(comp.upperAngle);
return def;

Check failure on line 81 in cocos/physics-2d/box2d-jsb/joints/hinge-joint.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe return of an `any` typed value
}
}
124 changes: 124 additions & 0 deletions cocos/physics-2d/box2d-jsb/joints/joint-2d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.

https://www.cocos.com/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

//cjh import b2 from '@cocos/box2d';
import { IJoint2D } from '../../spec/i-physics-joint';
import { Joint2D, PhysicsSystem2D, RigidBody2D } from '../../framework';
import { b2PhysicsWorld } from '../physics-world';
import { Vec2 } from '../../../core';

export class b2Joint implements IJoint2D {
get impl (): b2.Joint | null {

Check failure on line 32 in cocos/physics-2d/box2d-jsb/joints/joint-2d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

'any' overrides all other types in this union type
return this._b2joint;

Check failure on line 33 in cocos/physics-2d/box2d-jsb/joints/joint-2d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe return of an `any` typed value
}
get comp (): Joint2D | null {
return this._jointComp;
}
get body (): RigidBody2D | null {
return this._body;
}

protected _b2joint: b2.Joint | null = null;

Check failure on line 42 in cocos/physics-2d/box2d-jsb/joints/joint-2d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

'any' overrides all other types in this union type
protected _jointComp: Joint2D | null = null;
protected _body: RigidBody2D | null = null;

private _inited = false;

initialize (comp: Joint2D): void {
this._jointComp = comp;
}

onEnable (): void {
PhysicsSystem2D.instance._callAfterStep(this, this._init);
}

onDisable (): void {
PhysicsSystem2D.instance._callAfterStep(this, this._destroy);
}

// need init after body and connected body init
start (): void {
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;

const comp = this._jointComp!;
if (!comp.isValid) {
return;
}

this._body = comp.getComponent(RigidBody2D);

const def = this._createJointDef();
if (!def) {
return;
}

def.bodyA = this._body!.impl!.impl;
const connectedBody = comp.connectedBody;
//if connected body is set but not active, return
if (connectedBody && !connectedBody.enabledInHierarchy) {
return;
}

//if connected body is not set, use scene origin as connected body
if (!connectedBody) {
def.bodyB = (PhysicsSystem2D.instance.physicsWorld as b2PhysicsWorld).groundBodyImpl;
} else {
def.bodyB = connectedBody.impl!.impl;
}

def.collideConnected = comp.collideConnected;

this._b2joint = (PhysicsSystem2D.instance.physicsWorld as b2PhysicsWorld).impl.CreateJoint(def);

this._inited = true;
}

_destroy (): void {
if (!this._inited) return;

(PhysicsSystem2D.instance.physicsWorld as b2PhysicsWorld).impl.DestroyJoint(this._b2joint!);

Check failure on line 111 in cocos/physics-2d/box2d-jsb/joints/joint-2d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This assertion is unnecessary since it does not change the type of the expression

this._b2joint = null;
this._inited = false;
}

_createJointDef (): b2.JointDef | null {

Check failure on line 117 in cocos/physics-2d/box2d-jsb/joints/joint-2d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

'any' overrides all other types in this union type
return null;
}

isValid (): Joint2D | null {
return this._b2joint && this._body && this._body.impl && this._jointComp;

Check failure on line 122 in cocos/physics-2d/box2d-jsb/joints/joint-2d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe return of an `any` typed value
}
}
Loading
Loading