Skip to content

Commit

Permalink
[fix] box2d wasm joint stiffness and damping ratio now behaves the sa…
Browse files Browse the repository at this point in the history
…me as box2d.ts; fix rigidbody set type; fix box2d-wasm reallocate mem (#16278)

* [fix] box2d wasm joint stiffness and damping ratio now behaviors the same as box2d.ts; 
fix rigidbody set type; 
fix box2d-wasm reallocate mem
[fix] [physics2d] 1. debugdraw circle shape not consider rotation transform; 2. box2dwasm circle shape offset error
update native/external-config.json --> v3.8.2-9
  • Loading branch information
lealzhan authored Sep 26, 2023
1 parent 2928e74 commit d8f1be6
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 59 deletions.
7 changes: 4 additions & 3 deletions cocos/physics-2d/box2d-wasm/instantiated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,7 +114,7 @@ function initWasm (wasmUrl: string): Promise<void> {
},
}).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)));
});
}
Expand All @@ -122,7 +123,7 @@ function initAsm (): Promise<void> {
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<void>((resolve, reject) => {
Expand Down
17 changes: 4 additions & 13 deletions cocos/physics-2d/box2d-wasm/joints/fixed-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
}
6 changes: 0 additions & 6 deletions cocos/physics-2d/box2d-wasm/joints/joint-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ export class B2Joint implements IJoint2D {
addImplPtrReference(this, getImplPtr(this._b2joint));
addImplPtrReferenceWASM(this._b2joint, getImplPtr(this._b2joint));

this.updateStiffnessAndDamping();

this._inited = true;
}

Expand All @@ -131,8 +129,4 @@ export class B2Joint implements IJoint2D {
isValid (): Joint2D | null {
return this._b2joint && this._body && this._body.impl && this._jointComp;
}

updateStiffnessAndDamping (): void {
// do nothing
}
}
20 changes: 7 additions & 13 deletions cocos/physics-2d/box2d-wasm/joints/spring-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}
17 changes: 4 additions & 13 deletions cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 as any).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 as any).SetSpringDampingRatio(v);
}

// motor
Expand Down Expand Up @@ -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;
}
}
7 changes: 4 additions & 3 deletions cocos/physics-2d/box2d-wasm/platform/physics-debug-draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Expand Down
9 changes: 8 additions & 1 deletion cocos/physics-2d/box2d-wasm/rigid-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 as B2.BodyType);
} else if (v === ERigidBody2DType.Kinematic) {
this._body!.SetType(B2.BodyType.b2_kinematicBody as B2.BodyType);
} else if (v === ERigidBody2DType.Static) {
this._body!.SetType(B2.BodyType.b2_staticBody as B2.BodyType);
}
}

setLinearDamping (v: number): void {
this._body!.SetLinearDamping(v);
}
Expand Down
3 changes: 1 addition & 2 deletions cocos/physics-2d/box2d-wasm/shapes/circle-shape-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
11 changes: 7 additions & 4 deletions cocos/physics-2d/box2d/platform/physics-debug-draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -137,9 +138,11 @@ export class PhysicsDebugDraw extends b2.Draw {
}

DrawPoint (center, radius, color): void {
//empty
}

DrawParticles (): void {
//empty
}

_applyStrokeColor (color): void {
Expand Down
2 changes: 1 addition & 1 deletion native/external-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"type": "github",
"owner": "cocos-creator",
"name": "engine-native-external",
"checkout": "v3.8.2-8"
"checkout": "v3.8.2-9"
}
}

0 comments on commit d8f1be6

Please sign in to comment.