From da63436c2b6dee8ec6971599aefbf23a6de522f7 Mon Sep 17 00:00:00 2001 From: lealzhan Date: Mon, 18 Sep 2023 17:24:23 +0800 Subject: [PATCH 1/6] [fix] box2d wasm joint stiffness and damping ratio now behaviors the same as box2d.ts; fix rigidbody set type; fix box2d-wasm reallocate mem --- cocos/physics-2d/box2d-wasm/instantiated.ts | 7 ++++--- .../box2d-wasm/joints/fixed-joint.ts | 17 ++++------------ .../physics-2d/box2d-wasm/joints/joint-2d.ts | 6 ------ .../box2d-wasm/joints/spring-joint.ts | 20 +++++++------------ .../box2d-wasm/joints/wheel-joint.ts | 17 ++++------------ cocos/physics-2d/box2d-wasm/rigid-body.ts | 9 ++++++++- 6 files changed, 27 insertions(+), 49 deletions(-) diff --git a/cocos/physics-2d/box2d-wasm/instantiated.ts b/cocos/physics-2d/box2d-wasm/instantiated.ts index 023cb80afe1..fcb536b7263 100644 --- a/cocos/physics-2d/box2d-wasm/instantiated.ts +++ b/cocos/physics-2d/box2d-wasm/instantiated.ts @@ -31,7 +31,8 @@ import { game } from '../../game'; import { getError, error, sys, debug, IVec2Like } from '../../core'; import { WebAssemblySupportMode } from '../../misc/webassembly-support'; -export const B2 = {} as any; +// eslint-disable-next-line import/no-mutable-exports +export let B2 = {} as any; export function getImplPtr (wasmObject: any): number { // eslint-disable-next-line @typescript-eslint/no-unsafe-return @@ -113,7 +114,7 @@ function initWasm (wasmUrl: string): Promise { }, }).then((Instance: any) => { if (!EDITOR && !TEST) debug('[box2d]:box2d wasm lib loaded.'); - Object.assign(B2, Instance); + B2 = Instance; }).then(resolve).catch((err: any) => reject(errorMessage(err))); }); } @@ -122,7 +123,7 @@ function initAsm (): Promise { if (asmFactory != null) { return asmFactory().then((instance: any) => { if (!EDITOR && !TEST) debug('[box2d]:box2d asm lib loaded.'); - Object.assign(B2, instance); + B2 = instance; }); } else { return new Promise((resolve, reject) => { diff --git a/cocos/physics-2d/box2d-wasm/joints/fixed-joint.ts b/cocos/physics-2d/box2d-wasm/joints/fixed-joint.ts index d5de28d963a..6084d7e55c5 100644 --- a/cocos/physics-2d/box2d-wasm/joints/fixed-joint.ts +++ b/cocos/physics-2d/box2d-wasm/joints/fixed-joint.ts @@ -30,19 +30,10 @@ import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types'; export class B2FixedJoint extends B2Joint implements IFixedJoint { setFrequency (v: number): void { - this.updateStiffnessAndDamping(); + (this._b2joint as B2.WeldJoint).SetFrequency(v); } setDampingRatio (v: number): void { - this.updateStiffnessAndDamping(); - } - updateStiffnessAndDamping (): void { - if (this._b2joint) { - B2.SetLinearFrequencyAndDampingRatio( - this._b2joint, - (this._jointComp as FixedJoint2D).frequency, - (this._jointComp as FixedJoint2D).dampingRatio, - ); - } + (this._b2joint as B2.WeldJoint).SetDampingRatio(v); } _createJointDef (): any { @@ -51,8 +42,8 @@ export class B2FixedJoint extends B2Joint implements IFixedJoint { 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.damping = 0;//comp.dampingRatio; - def.stiffness = 1;//comp.frequency; + def.dampingRatio = comp.dampingRatio; + def.frequencyHz = comp.frequency; return def; } } diff --git a/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts b/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts index 0f4c964861b..fbfd87b237c 100644 --- a/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts +++ b/cocos/physics-2d/box2d-wasm/joints/joint-2d.ts @@ -101,8 +101,6 @@ export class B2Joint implements IJoint2D { addImplPtrReference(this, getImplPtr(this._b2joint)); addImplPtrReferenceWASM(this._b2joint, getImplPtr(this._b2joint)); - this.updateStiffnessAndDamping(); - this._inited = true; } @@ -124,8 +122,4 @@ export class B2Joint implements IJoint2D { isValid (): Joint2D | null { return this._b2joint && this._body && this._body.impl && this._jointComp; } - - updateStiffnessAndDamping (): void { - // do nothing - } } diff --git a/cocos/physics-2d/box2d-wasm/joints/spring-joint.ts b/cocos/physics-2d/box2d-wasm/joints/spring-joint.ts index d8faa118417..e0ca9102b88 100644 --- a/cocos/physics-2d/box2d-wasm/joints/spring-joint.ts +++ b/cocos/physics-2d/box2d-wasm/joints/spring-joint.ts @@ -30,20 +30,14 @@ import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types'; export class B2SpringJoint extends B2Joint implements ISpringJoint { setFrequency (v: number): void { - this.updateStiffnessAndDamping(); + (this._b2joint as B2.DistanceJoint).SetFrequency(v); } + setDampingRatio (v: number): void { - this.updateStiffnessAndDamping(); - } - updateStiffnessAndDamping (): void { - if (this._b2joint) { - B2.SetLinearFrequencyAndDampingRatio( - this._b2joint, - (this._jointComp as SpringJoint2D).frequency, - (this._jointComp as SpringJoint2D).dampingRatio, - ); - } + //this.updateStiffnessAndDamping(); + (this._b2joint as B2.DistanceJoint).SetDampingRatio(v); } + setDistance (v: number): void { if (this._b2joint) { (this._b2joint as B2.DistanceJoint).SetLength(v); @@ -56,8 +50,8 @@ export class B2SpringJoint extends B2Joint implements ISpringJoint { 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.length = comp.distance / PHYSICS_2D_PTM_RATIO; - def.damping = 0;//comp.dampingRatio; - def.stiffness = 1;//comp.frequency; + def.dampingRatio = comp.dampingRatio; + def.frequencyHz = comp.frequency; return def; } } diff --git a/cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts b/cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts index 72be5356593..c5973208153 100644 --- a/cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts +++ b/cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts @@ -31,19 +31,10 @@ import { toRadian } from '../../../core'; export class B2WheelJoint extends B2Joint implements IWheelJoint { setFrequency (v: number): void { - this.updateStiffnessAndDamping(); + (this._b2joint as B2.WheelJoint).SetSpringFrequencyHz(v); } setDampingRatio (v: number): void { - this.updateStiffnessAndDamping(); - } - updateStiffnessAndDamping (): void { - if (this._b2joint) { - B2.SetLinearFrequencyAndDampingRatio( - this._b2joint, - (this._jointComp as WheelJoint2D).frequency, - (this._jointComp as WheelJoint2D).dampingRatio, - ); - } + (this._b2joint as B2.WheelJoint).SetSpringDampingRatio(v); } // motor @@ -73,8 +64,8 @@ export class B2WheelJoint extends B2Joint implements IWheelJoint { def.maxMotorTorque = comp.maxMotorTorque; def.motorSpeed = toRadian(comp.motorSpeed); def.enableMotor = comp.enableMotor; - def.damping = 0;//comp.dampingRatio; - def.stiffness = 1;//comp.frequency; + def.dampingRatio = comp.dampingRatio; + def.frequencyHz = comp.frequency; return def; } } diff --git a/cocos/physics-2d/box2d-wasm/rigid-body.ts b/cocos/physics-2d/box2d-wasm/rigid-body.ts index a92e0533863..29c7cfa8771 100644 --- a/cocos/physics-2d/box2d-wasm/rigid-body.ts +++ b/cocos/physics-2d/box2d-wasm/rigid-body.ts @@ -231,8 +231,15 @@ export class B2RigidBody2D implements IRigidBody2D { } setType (v: ERigidBody2DType): void { - this._body!.SetType(v as number); + if (v === ERigidBody2DType.Dynamic) { + this._body!.SetType(B2.BodyType.b2_dynamicBody); + } else if (v === ERigidBody2DType.Kinematic) { + this._body!.SetType(B2.BodyType.b2_kinematicBody); + } else if (v === ERigidBody2DType.Static) { + this._body!.SetType(B2.BodyType.b2_staticBody); + } } + setLinearDamping (v: number): void { this._body!.SetLinearDamping(v); } From b72562d83b3df6f2bda2a74a4d1250d9c26cb15c Mon Sep 17 00:00:00 2001 From: lealzhan Date: Wed, 20 Sep 2023 10:20:43 +0800 Subject: [PATCH 2/6] eslint --- cocos/physics-2d/box2d-wasm/rigid-body.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cocos/physics-2d/box2d-wasm/rigid-body.ts b/cocos/physics-2d/box2d-wasm/rigid-body.ts index 29c7cfa8771..569142e0944 100644 --- a/cocos/physics-2d/box2d-wasm/rigid-body.ts +++ b/cocos/physics-2d/box2d-wasm/rigid-body.ts @@ -232,11 +232,11 @@ export class B2RigidBody2D implements IRigidBody2D { setType (v: ERigidBody2DType): void { if (v === ERigidBody2DType.Dynamic) { - this._body!.SetType(B2.BodyType.b2_dynamicBody); + this._body!.SetType(B2.BodyType.b2_dynamicBody as B2.BodyType); } else if (v === ERigidBody2DType.Kinematic) { - this._body!.SetType(B2.BodyType.b2_kinematicBody); + this._body!.SetType(B2.BodyType.b2_kinematicBody as B2.BodyType); } else if (v === ERigidBody2DType.Static) { - this._body!.SetType(B2.BodyType.b2_staticBody); + this._body!.SetType(B2.BodyType.b2_staticBody as B2.BodyType); } } From 9e575cc5807f6d8f08655266fa1779313547fe6a Mon Sep 17 00:00:00 2001 From: lealzhan Date: Wed, 20 Sep 2023 11:00:58 +0800 Subject: [PATCH 3/6] [fix] [physics2d] 1. debugdraw circle shape not consider rotation transform; 2. box2dwasm circle shape offset error --- cocos/physics-2d/box2d-wasm/platform/physics-debug-draw.ts | 7 ++++--- cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts | 3 +-- cocos/physics-2d/box2d/platform/physics-debug-draw.ts | 5 +++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cocos/physics-2d/box2d-wasm/platform/physics-debug-draw.ts b/cocos/physics-2d/box2d-wasm/platform/physics-debug-draw.ts index 9127274bdb5..e8d3f81417c 100644 --- a/cocos/physics-2d/box2d-wasm/platform/physics-debug-draw.ts +++ b/cocos/physics-2d/box2d-wasm/platform/physics-debug-draw.ts @@ -97,10 +97,11 @@ export class PhysicsDebugDraw {// extends B2.Draw { } static _DrawCircle (center: B2.Vec2, radius: number): void { - const p = PhysicsDebugDraw._xf.p; + b2Mul(PhysicsDebugDraw._xf, center, _tmp_vec3); + //scale? PhysicsDebugDraw._drawer!.circle( - (center.x + p.x) * PHYSICS_2D_PTM_RATIO, - (center.y + p.y) * PHYSICS_2D_PTM_RATIO, + _tmp_vec3.x * PHYSICS_2D_PTM_RATIO, + _tmp_vec3.y * PHYSICS_2D_PTM_RATIO, radius * PHYSICS_2D_PTM_RATIO, ); } diff --git a/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts b/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts index 254e2a1ece4..828fa57cadd 100644 --- a/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts +++ b/cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts @@ -51,8 +51,7 @@ export class B2CircleShape extends B2Shape2D implements ICircleShape { const shape = new B2.CircleShape(); shape.m_radius = comp.radius / PHYSICS_2D_PTM_RATIO * scaleX; - shape.m_p.x = offsetX; - shape.m_p.y = offsetY; + shape.m_p = { x: offsetX, y: offsetY }; return [shape as unknown as B2.CircleShape]; } diff --git a/cocos/physics-2d/box2d/platform/physics-debug-draw.ts b/cocos/physics-2d/box2d/platform/physics-debug-draw.ts index 937a39457e9..4aa8c8acfdb 100644 --- a/cocos/physics-2d/box2d/platform/physics-debug-draw.ts +++ b/cocos/physics-2d/box2d/platform/physics-debug-draw.ts @@ -74,8 +74,9 @@ export class PhysicsDebugDraw extends b2.Draw { } _DrawCircle (center: b2.Vec2, radius: number): void { - const p = this._xf.p; - this._drawer!.circle((center.x + p.x) * PHYSICS_2D_PTM_RATIO, (center.y + p.y) * PHYSICS_2D_PTM_RATIO, radius * PHYSICS_2D_PTM_RATIO); + b2.Transform.MulXV(this._xf, center, _tmp_vec2); + //scale? + this._drawer!.circle((_tmp_vec2.x) * PHYSICS_2D_PTM_RATIO, (_tmp_vec2.y) * PHYSICS_2D_PTM_RATIO, radius * PHYSICS_2D_PTM_RATIO); } DrawCircle (center: b2.Vec2, radius: number, color): void { From 64c47d04598e5528ab2168358ef8237bf3795bbe Mon Sep 17 00:00:00 2001 From: lealzhan Date: Wed, 20 Sep 2023 11:14:44 +0800 Subject: [PATCH 4/6] eslint --- cocos/physics-2d/box2d/platform/physics-debug-draw.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cocos/physics-2d/box2d/platform/physics-debug-draw.ts b/cocos/physics-2d/box2d/platform/physics-debug-draw.ts index 4aa8c8acfdb..547b31922e1 100644 --- a/cocos/physics-2d/box2d/platform/physics-debug-draw.ts +++ b/cocos/physics-2d/box2d/platform/physics-debug-draw.ts @@ -22,7 +22,7 @@ THE SOFTWARE. */ -import b2 from '@cocos/box2d'; +import b2, { Vec2 } from '@cocos/box2d'; import { Color } from '../../../core'; import { PHYSICS_2D_PTM_RATIO } from '../../framework'; import { Graphics } from '../../../2d'; @@ -48,7 +48,7 @@ export class PhysicsDebugDraw extends b2.Draw { const drawer = this._drawer!; for (let i = 0; i < vertexCount; i++) { - b2.Transform.MulXV(this._xf, vertices[i], _tmp_vec2); + b2.Transform.MulXV(this._xf, vertices[i] as Vec2, _tmp_vec2); const x = _tmp_vec2.x * PHYSICS_2D_PTM_RATIO; const y = _tmp_vec2.y * PHYSICS_2D_PTM_RATIO; if (i === 0) drawer.moveTo(x, y); @@ -138,9 +138,11 @@ export class PhysicsDebugDraw extends b2.Draw { } DrawPoint (center, radius, color): void { + //empty } DrawParticles (): void { + //empty } _applyStrokeColor (color): void { From 7d69521fb1cba7040a5c734cfa3fc744c2ccc164 Mon Sep 17 00:00:00 2001 From: lealzhan Date: Tue, 26 Sep 2023 15:10:49 +0800 Subject: [PATCH 5/6] update native/external-config.json --> v3.8.2-9 --- native/external-config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/external-config.json b/native/external-config.json index a768805a4d8..88416cdce72 100644 --- a/native/external-config.json +++ b/native/external-config.json @@ -3,6 +3,6 @@ "type": "github", "owner": "cocos-creator", "name": "engine-native-external", - "checkout": "v3.8.2-8" + "checkout": "v3.8.2-9" } } \ No newline at end of file From f3a107e31a28db1daace6a4dee6c12b7c46391e3 Mon Sep 17 00:00:00 2001 From: lealzhan Date: Tue, 26 Sep 2023 15:52:03 +0800 Subject: [PATCH 6/6] eslint --- cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts b/cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts index c5973208153..c2faa6e1654 100644 --- a/cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts +++ b/cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts @@ -31,10 +31,10 @@ import { toRadian } from '../../../core'; export class B2WheelJoint extends B2Joint implements IWheelJoint { setFrequency (v: number): void { - (this._b2joint as B2.WheelJoint).SetSpringFrequencyHz(v); + (this._b2joint as B2.WheelJoint as any).SetSpringFrequencyHz(v); } setDampingRatio (v: number): void { - (this._b2joint as B2.WheelJoint).SetSpringDampingRatio(v); + (this._b2joint as B2.WheelJoint as any).SetSpringDampingRatio(v); } // motor