Skip to content
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
6 changes: 4 additions & 2 deletions cocos/physics-2d/box2d-jsb/joints/distance-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ export class b2DistanceJoint extends b2Joint implements IDistanceJoint {
_createJointDef (): any {
const comp = this._jointComp as DistanceJoint2D;
const def = new b2jsb.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 };
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA = { x: anchorA.x, y: anchorA.y };
def.localAnchorB = { x: anchorB.x, y: anchorB.y };
def.maxLength = comp.maxLength / PHYSICS_2D_PTM_RATIO;
return def;
}
Expand Down
7 changes: 4 additions & 3 deletions cocos/physics-2d/box2d-jsb/joints/fixed-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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 {
Expand All @@ -41,8 +40,10 @@ export class b2FixedJoint extends b2Joint implements IFixedJoint {
_createJointDef (): any {
const comp = this._jointComp as FixedJoint2D;
const def = new b2jsb.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 };
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA = { x: anchorA.x, y: anchorA.y };
def.localAnchorB = { x: anchorB.x, y: anchorB.y };
def.referenceAngle = 0;
def.frequencyHz = comp.frequency;
def.dampingRatio = comp.dampingRatio;
Expand Down
7 changes: 4 additions & 3 deletions cocos/physics-2d/box2d-jsb/joints/hinge-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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 {
Expand Down Expand Up @@ -67,8 +66,10 @@ export class b2HingeJoint extends b2Joint implements IHingeJoint {
_createJointDef (): any {
const comp = this._jointComp as HingeJoint2D;
const def = new b2jsb.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 };
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA = { x: anchorA.x, y: anchorA.y };
def.localAnchorB = { x: anchorB.x, y: anchorB.y };

def.enableMotor = comp.enableMotor;
def.maxMotorTorque = comp.maxMotorTorque;
Expand Down
12 changes: 12 additions & 0 deletions cocos/physics-2d/box2d-jsb/joints/joint-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import { IJoint2D } from '../../spec/i-physics-joint';
import { Joint2D, PhysicsSystem2D, RigidBody2D } from '../../framework';
import { b2PhysicsWorld } from '../physics-world';
import { Vec2 } from '../../../core';

const tempAnchorA = new Vec2();
const tempAnchorB = new Vec2();

export class b2Joint implements IJoint2D {
get impl (): b2jsb.Joint | null {
Expand Down Expand Up @@ -117,6 +121,14 @@ export class b2Joint implements IJoint2D {
return null;
}

protected _getAnchorA (): Vec2 {
return this._jointComp!._getAnchorA(tempAnchorA);
}

protected _getAnchorB (): Vec2 {
return this._jointComp!._getAnchorB(tempAnchorB);
}

isValid (): Joint2D | null {
return this._b2joint && this._body && this._body.impl && this._jointComp;
}
Expand Down
6 changes: 4 additions & 2 deletions cocos/physics-2d/box2d-jsb/joints/slider-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ export class b2SliderJoint extends b2Joint implements ISliderJoint {
_createJointDef (): any {
const comp = this._jointComp as SliderJoint2D;
const def = new b2jsb.PrismaticJointDef();
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 };
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA = { x: anchorA.x, y: anchorA.y };
def.localAnchorB = { x: anchorB.x, y: anchorB.y };
const angle = toRadian(comp.angle);
def.localAxisA = { x: Math.cos(angle), y: Math.sin(angle) };
def.referenceAngle = 0;
Expand Down
6 changes: 4 additions & 2 deletions cocos/physics-2d/box2d-jsb/joints/spring-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ export class b2SpringJoint extends b2Joint implements ISpringJoint {
_createJointDef (): any {
const comp = this._jointComp as SpringJoint2D;
const def = new b2jsb.DistanceJointDef();
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 };
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA = { x: anchorA.x, y: anchorA.y };
def.localAnchorB = { x: anchorB.x, y: anchorB.y };
def.length = comp.distance / PHYSICS_2D_PTM_RATIO;
def.dampingRatio = comp.dampingRatio;
def.frequencyHz = comp.frequency;
Expand Down
7 changes: 4 additions & 3 deletions cocos/physics-2d/box2d-jsb/joints/wheel-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import { IWheelJoint } from '../../spec/i-physics-joint';
import { WheelJoint2D } from '../../framework';
import { b2Joint } from './joint-2d';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';
import { toRadian } from '../../../core';

export class b2WheelJoint extends b2Joint implements IWheelJoint {
Expand Down Expand Up @@ -59,8 +58,10 @@ export class b2WheelJoint extends b2Joint implements IWheelJoint {
_createJointDef (): any {
const comp = this._jointComp as WheelJoint2D;
const def = new b2jsb.WheelJointDef();
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 };
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA = { x: anchorA.x, y: anchorA.y };
def.localAnchorB = { x: anchorB.x, y: anchorB.y };
const angle = toRadian(comp.angle);
def.localAxisA = { x: Math.cos(angle), y: Math.sin(angle) };
def.maxMotorTorque = comp.maxMotorTorque;
Expand Down
3 changes: 2 additions & 1 deletion cocos/physics-2d/box2d-jsb/rigid-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { Vec2, toRadian, Vec3, Quat, IVec2Like, TWO_PI, HALF_PI } from '../../co
import { PHYSICS_2D_PTM_RATIO, ERigidBody2DType } from '../framework/physics-types';

import { Node } from '../../scene-graph/node';
import { Collider2D } from '../framework';
import { Collider2D, Joint2D } from '../framework';

const tempVec3 = new Vec3();

Expand Down Expand Up @@ -90,6 +90,7 @@ export class b2RigidBody2D implements IRigidBody2D {
for (let i = 0; i < colliders.length; i++) {
colliders[i].apply();
}
Joint2D._applyScale(this.rigidBody);
}

const bodyType = this._rigidBody.type;
Expand Down
6 changes: 4 additions & 2 deletions cocos/physics-2d/box2d-wasm/joints/distance-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ export class B2DistanceJoint extends B2Joint implements IDistanceJoint {
_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 };
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA = { x: anchorA.x, y: anchorA.y };
def.localAnchorB = { x: anchorB.x, y: anchorB.y };
def.maxLength = comp.maxLength / PHYSICS_2D_PTM_RATIO;
return def;
}
Expand Down
7 changes: 4 additions & 3 deletions cocos/physics-2d/box2d-wasm/joints/fixed-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { B2 } from '../instantiated';
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';

/** @mangle */
export class B2FixedJoint extends B2Joint implements IFixedJoint {
Expand All @@ -44,8 +43,10 @@ export class B2FixedJoint extends B2Joint implements IFixedJoint {
_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 };
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA = { x: anchorA.x, y: anchorA.y };
def.localAnchorB = { x: anchorB.x, y: anchorB.y };
def.referenceAngle = 0;
def.dampingRatio = comp.dampingRatio;
def.frequencyHz = comp.frequency;
Expand Down
7 changes: 4 additions & 3 deletions cocos/physics-2d/box2d-wasm/joints/hinge-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { B2 } from '../instantiated';
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';

/** @mangle */
Expand Down Expand Up @@ -69,8 +68,10 @@ export class B2HingeJoint extends B2Joint implements IHingeJoint {
_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 };
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA = { x: anchorA.x, y: anchorA.y };
def.localAnchorB = { x: anchorB.x, y: anchorB.y };

def.enableMotor = comp.enableMotor;
def.maxMotorTorque = comp.maxMotorTorque;
Expand Down
13 changes: 12 additions & 1 deletion cocos/physics-2d/box2d-wasm/joints/joint-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
THE SOFTWARE.
*/

import { B2, B2ObjectType, addImplPtrReference, addImplPtrReferenceWASM, getImplPtr, removeImplPtrReference, removeImplPtrReferenceWASM } from '../instantiated';

Check warning on line 25 in cocos/physics-2d/box2d-wasm/joints/joint-2d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 161. Maximum allowed is 150
import { IJoint2D } from '../../spec/i-physics-joint';
import { Joint2D, PhysicsSystem2D, RigidBody2D } from '../../framework';
import { B2PhysicsWorld } from '../physics-world';
import { warn } from '../../../core';
import { Vec2, warn } from '../../../core';

const tempAnchorA = new Vec2();
const tempAnchorB = new Vec2();

/** @mangle */
export class B2Joint implements IJoint2D {
Expand Down Expand Up @@ -127,6 +130,14 @@
return null;
}

protected _getAnchorA (): Vec2 {
return this._jointComp!._getAnchorA(tempAnchorA);
}

protected _getAnchorB (): Vec2 {
return this._jointComp!._getAnchorB(tempAnchorB);
}

isValid (): Joint2D | null {
return this._b2joint && this._body && this._body.impl && this._jointComp;
}
Expand Down
6 changes: 4 additions & 2 deletions cocos/physics-2d/box2d-wasm/joints/slider-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ export class B2SliderJoint extends B2Joint implements ISliderJoint {
_createJointDef (): any {
const comp = this._jointComp as SliderJoint2D;
const def = new B2.PrismaticJointDef();
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 };
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA = { x: anchorA.x, y: anchorA.y };
def.localAnchorB = { x: anchorB.x, y: anchorB.y };
const angle = toRadian(comp.angle);
def.localAxisA = { x: Math.cos(angle), y: Math.sin(angle) };
def.referenceAngle = 0;
Expand Down
6 changes: 4 additions & 2 deletions cocos/physics-2d/box2d-wasm/joints/spring-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ export class B2SpringJoint extends B2Joint implements ISpringJoint {
_createJointDef (): any {
const comp = this._jointComp as SpringJoint2D;
const def = new B2.DistanceJointDef();
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 };
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA = { x: anchorA.x, y: anchorA.y };
def.localAnchorB = { x: anchorB.x, y: anchorB.y };
def.length = comp.distance / PHYSICS_2D_PTM_RATIO;
def.dampingRatio = comp.dampingRatio;
def.frequencyHz = comp.frequency;
Expand Down
7 changes: 4 additions & 3 deletions cocos/physics-2d/box2d-wasm/joints/wheel-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { B2 } from '../instantiated';
import { IWheelJoint } from '../../spec/i-physics-joint';
import { WheelJoint2D } from '../../framework';
import { B2Joint } from './joint-2d';
import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types';
import { toRadian } from '../../../core';

/** @mangle */
Expand Down Expand Up @@ -62,8 +61,10 @@ export class B2WheelJoint extends B2Joint implements IWheelJoint {
_createJointDef (): any {
const comp = this._jointComp as WheelJoint2D;
const def = new B2.WheelJointDef();
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 };
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA = { x: anchorA.x, y: anchorA.y };
def.localAnchorB = { x: anchorB.x, y: anchorB.y };
const angle = toRadian(comp.angle);
def.localAxisA = { x: Math.cos(angle), y: Math.sin(angle) };
def.maxMotorTorque = comp.maxMotorTorque;
Expand Down
3 changes: 2 additions & 1 deletion cocos/physics-2d/box2d-wasm/rigid-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { Vec2, toRadian, Vec3, Quat, IVec2Like, TWO_PI, HALF_PI, warn } from '..
import { PHYSICS_2D_PTM_RATIO, ERigidBody2DType } from '../framework/physics-types';

import { Node } from '../../scene-graph/node';
import { Collider2D } from '../framework';
import { Collider2D, Joint2D } from '../framework';
import { B2Shape2D } from './shapes/shape-2d';
import { B2Joint } from './joints/joint-2d';

Expand Down Expand Up @@ -95,6 +95,7 @@ export class B2RigidBody2D implements IRigidBody2D {
for (let i = 0; i < colliders.length; i++) {
colliders[i].apply();
}
Joint2D._applyScale(this.rigidBody);
}
if (type & Node.TransformBit.POSITION) {
this.syncPositionToPhysics(true);
Expand Down
6 changes: 4 additions & 2 deletions cocos/physics-2d/box2d/joints/distance-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ export class b2DistanceJoint extends b2Joint implements IDistanceJoint {
_createJointDef (): any {
const comp = this._jointComp as DistanceJoint2D;
const def = new b2.RopeJointDef();
def.localAnchorA.Set(comp.anchor.x / PHYSICS_2D_PTM_RATIO, comp.anchor.y / PHYSICS_2D_PTM_RATIO);
def.localAnchorB.Set(comp.connectedAnchor.x / PHYSICS_2D_PTM_RATIO, comp.connectedAnchor.y / PHYSICS_2D_PTM_RATIO);
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA.Set(anchorA.x, anchorA.y);
def.localAnchorB.Set(anchorB.x, anchorB.y);
def.maxLength = comp.maxLength / PHYSICS_2D_PTM_RATIO;
return def;
}
Expand Down
7 changes: 4 additions & 3 deletions cocos/physics-2d/box2d/joints/fixed-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import b2 from '@cocos/box2d';
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';

/** @mangle */
export class b2FixedJoint extends b2Joint implements IFixedJoint {
Expand All @@ -44,8 +43,10 @@ export class b2FixedJoint extends b2Joint implements IFixedJoint {
_createJointDef (): any {
const comp = this._jointComp as FixedJoint2D;
const def = new b2.WeldJointDef();
def.localAnchorA.Set(comp.anchor.x / PHYSICS_2D_PTM_RATIO, comp.anchor.y / PHYSICS_2D_PTM_RATIO);
def.localAnchorB.Set(comp.connectedAnchor.x / PHYSICS_2D_PTM_RATIO, comp.connectedAnchor.y / PHYSICS_2D_PTM_RATIO);
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA.Set(anchorA.x, anchorA.y);
def.localAnchorB.Set(anchorB.x, anchorB.y);
def.referenceAngle = 0;
def.frequencyHz = comp.frequency;
def.dampingRatio = comp.dampingRatio;
Expand Down
7 changes: 4 additions & 3 deletions cocos/physics-2d/box2d/joints/hinge-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ 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';

/** @mangle */
Expand Down Expand Up @@ -69,8 +68,10 @@ export class b2HingeJoint extends b2Joint implements IHingeJoint {
_createJointDef (): any {
const comp = this._jointComp as HingeJoint2D;
const def = new b2.RevoluteJointDef();
def.localAnchorA.Set(comp.anchor.x / PHYSICS_2D_PTM_RATIO, comp.anchor.y / PHYSICS_2D_PTM_RATIO);
def.localAnchorB.Set(comp.connectedAnchor.x / PHYSICS_2D_PTM_RATIO, comp.connectedAnchor.y / PHYSICS_2D_PTM_RATIO);
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA.Set(anchorA.x, anchorA.y);
def.localAnchorB.Set(anchorB.x, anchorB.y);

def.enableMotor = comp.enableMotor;
def.maxMotorTorque = comp.maxMotorTorque;
Expand Down
12 changes: 12 additions & 0 deletions cocos/physics-2d/box2d/joints/joint-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ 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';

const tempAnchorA = new Vec2();
const tempAnchorB = new Vec2();

/** @mangle */
export class b2Joint implements IJoint2D {
Expand Down Expand Up @@ -118,6 +122,14 @@ export class b2Joint implements IJoint2D {
return null;
}

protected _getAnchorA (): Vec2 {
return this._jointComp!._getAnchorA(tempAnchorA);
}

protected _getAnchorB (): Vec2 {
return this._jointComp!._getAnchorB(tempAnchorB);
}

isValid (): Joint2D | null {
return this._b2joint && this._body && this._body.impl && this._jointComp;
}
Expand Down
6 changes: 4 additions & 2 deletions cocos/physics-2d/box2d/joints/slider-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ export class b2SliderJoint extends b2Joint implements ISliderJoint {
_createJointDef (): any {
const comp = this._jointComp as SliderJoint2D;
const def = new b2.PrismaticJointDef();
def.localAnchorA.Set(comp.anchor.x / PHYSICS_2D_PTM_RATIO, comp.anchor.y / PHYSICS_2D_PTM_RATIO);
def.localAnchorB.Set(comp.connectedAnchor.x / PHYSICS_2D_PTM_RATIO, comp.connectedAnchor.y / PHYSICS_2D_PTM_RATIO);
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA.Set(anchorA.x, anchorA.y);
def.localAnchorB.Set(anchorB.x, anchorB.y);
const angle = toRadian(comp.angle);
def.localAxisA.Set(Math.cos(angle), Math.sin(angle));
def.referenceAngle = 0;
Expand Down
6 changes: 4 additions & 2 deletions cocos/physics-2d/box2d/joints/spring-joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ export class b2SpringJoint extends b2Joint implements ISpringJoint {
_createJointDef (): any {
const comp = this._jointComp as SpringJoint2D;
const def = new b2.DistanceJointDef();
def.localAnchorA.Set(comp.anchor.x / PHYSICS_2D_PTM_RATIO, comp.anchor.y / PHYSICS_2D_PTM_RATIO);
def.localAnchorB.Set(comp.connectedAnchor.x / PHYSICS_2D_PTM_RATIO, comp.connectedAnchor.y / PHYSICS_2D_PTM_RATIO);
const anchorA = this._getAnchorA();
const anchorB = this._getAnchorB();
def.localAnchorA.Set(anchorA.x, anchorA.y);
def.localAnchorB.Set(anchorB.x, anchorB.y);
def.length = comp.distance / PHYSICS_2D_PTM_RATIO;
def.dampingRatio = comp.dampingRatio;
def.frequencyHz = comp.frequency;
Expand Down
Loading
Loading