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. #18152

Open
wants to merge 18 commits into
base: v3.8.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -96,6 +96,9 @@
"physics-2d-framework": {
"modules": ["physics-2d-framework"]
},
"physics-2d-box2d-jsb": {
"modules": ["physics-2d-box2d-jsb", "physics-2d-framework"]
},
"physics-2d-box2d": {
"modules": ["physics-2d-box2d", "physics-2d-framework"]
},
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) 2024 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-jsb', {
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,
});
});
44 changes: 44 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,44 @@
/*
Copyright (c) 2024 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 { 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);
}
}

_createJointDef (): any {
const comp = this._jointComp as DistanceJoint2D;
const def = new b2.RopeJointDef();
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;
}
}
51 changes: 51 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,51 @@
/*
Copyright (c) 2024 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 { 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);
}
}
setDampingRatio (v: number): void {
if (this._b2joint) {
(this._b2joint as b2.WeldJoint).SetDampingRatio(v);
}
}

_createJointDef (): any {
const comp = this._jointComp as FixedJoint2D;
const def = new b2.WeldJointDef();
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;
}
}
82 changes: 82 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,82 @@
/*
Copyright (c) 2024 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 { 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);
}
}
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));
}
}

// motor
enableMotor (v: boolean): void {
if (this._b2joint) {
(this._b2joint as b2.RevoluteJoint).EnableMotor(v);
}
}
setMaxMotorTorque (v: number): void {
if (this._b2joint) {
(this._b2joint as b2.RevoluteJoint).SetMaxMotorTorque(v);
}
}
setMotorSpeed (v: number): void {
if (this._b2joint) {
(this._b2joint as b2.RevoluteJoint).SetMotorSpeed(v);
}
}

_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;
}
}
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) 2024 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 { 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 {
return this._b2joint;
}
get comp (): Joint2D | null {
return this._jointComp;
}
get body (): RigidBody2D | null {
return this._body;
}

protected _b2joint: b2.Joint | null = null;
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;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
(PhysicsSystem2D.instance.physicsWorld as b2PhysicsWorld).impl.DestroyJoint(this._b2joint);

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

_createJointDef (): b2.JointDef | null {
return null;
}

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