Skip to content

Commit 55545f2

Browse files
authored
Merge pull request #1831 from INEEDSSD/Master3.0
fix: Fixed 2D physics Shape naming duplication issue.
2 parents dcb4e86 + 4a90d00 commit 55545f2

23 files changed

+155
-140
lines changed

src/layaAir/laya/physics/Collider2D/ColliderBase.ts

+26-16
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class ColliderBase extends Component {
118118
public set inertia(value: number) {
119119
this._inertia = value;
120120
if (!this._useAutoMass) {
121-
Physics2D.I._factory.set_rigidBody_Mass(this._box2DBody, this._mass, this._centerOfMass, this._inertia, this._massData);
121+
this._box2DBody && Physics2D.I._factory.set_rigidBody_Mass(this._box2DBody, this._mass, this._centerOfMass, this._inertia, this._massData);
122122
}
123123
}
124124

@@ -128,7 +128,7 @@ export class ColliderBase extends Component {
128128
*/
129129
public get centerOfMass(): IV2 | Vector2 {
130130
let center;
131-
if (this._useAutoMass) {
131+
if (this._useAutoMass && this._box2DBody) {
132132
center = Physics2D.I._factory.get_rigidBody_Center(this._box2DBody);
133133
this._centerOfMass.x = center.x;
134134
this._centerOfMass.y = center.y;
@@ -145,7 +145,7 @@ export class ColliderBase extends Component {
145145
this._centerOfMass.y = value.y;
146146
}
147147
if (!this._useAutoMass) {
148-
Physics2D.I._factory.set_rigidBody_Mass(this._box2DBody, this._mass, this._centerOfMass, this._inertia, this._massData);
148+
this._box2DBody && Physics2D.I._factory.set_rigidBody_Mass(this._box2DBody, this._mass, this._centerOfMass, this._inertia, this._massData);
149149
}
150150
}
151151

@@ -155,7 +155,7 @@ export class ColliderBase extends Component {
155155
*/
156156
public get mass(): number {
157157
let mass;
158-
if (this._useAutoMass) {
158+
if (this._useAutoMass && this._box2DBody) {
159159
mass = Physics2D.I._factory.get_rigidBody_Mass(this._box2DBody);
160160
} else {
161161
mass = this._mass;
@@ -165,8 +165,7 @@ export class ColliderBase extends Component {
165165
public set mass(value: number) {
166166
this._mass = value;
167167
if (!this._useAutoMass) {
168-
Physics2D.I._factory.set_rigidBody_Mass(this._box2DBody, this._mass, this._centerOfMass, this._inertia, this._massData);
169-
168+
this._box2DBody && Physics2D.I._factory.set_rigidBody_Mass(this._box2DBody, this._mass, this._centerOfMass, this._inertia, this._massData);
170169
}
171170
}
172171

@@ -179,19 +178,21 @@ export class ColliderBase extends Component {
179178
}
180179
public set useAutoMass(value: boolean) {
181180
this._useAutoMass = value;
182-
Physics2D.I._factory.set_rigidBody_Mass(this._box2DBody, this._mass, this._centerOfMass, this._inertia, this._massData);
181+
this._box2DBody && Physics2D.I._factory.set_rigidBody_Mass(this._box2DBody, this._mass, this._centerOfMass, this._inertia, this._massData);
183182
}
184183

185184
/**
186185
* @zh 当前碰撞体在物理世界中是否在激活状态
187186
*/
188187
public get isAwake(): boolean {
189-
this._isAwake = Physics2D.I._factory.get_rigidBody_IsAwake(this._box2DBody);
188+
if (this._box2DBody) {
189+
this._isAwake = Physics2D.I._factory.get_rigidBody_IsAwake(this._box2DBody);
190+
}
190191
return this.isAwake;
191192
}
192193
public set isAwake(value: boolean) {
193194
this._isAwake = value;
194-
Physics2D.I._factory.set_rigidBody_Awake(this._box2DBody, value);
195+
this._box2DBody && Physics2D.I._factory.set_rigidBody_Awake(this._box2DBody, value);
195196
}
196197

197198
/**
@@ -231,7 +232,7 @@ export class ColliderBase extends Component {
231232
set x(value: number) {
232233
if (this._x == value) return;
233234
this._x = value;
234-
this._updateTransformFromRender();
235+
this._needupdataShapeAttribute();
235236
}
236237

237238
/**
@@ -245,7 +246,7 @@ export class ColliderBase extends Component {
245246
set y(value: number) {
246247
if (this._y == value) return;
247248
this._y = value;
248-
this._updateTransformFromRender();
249+
this._needupdataShapeAttribute();
249250
}
250251

251252
/**
@@ -300,12 +301,13 @@ export class ColliderBase extends Component {
300301
return this.owner.globalTrans.localToGlobal(x, y);
301302
}
302303

304+
303305
/**
304-
* @zh 从渲染更新碰撞体的位置
306+
* @internal
307+
* @en Refresh the physics world collision information after the collision body parameters change.
308+
* @zh 碰撞体参数发生变化后,刷新物理世界碰撞信息
305309
*/
306-
private _updateTransformFromRender() {
307-
var sp: Sprite = this.owner;
308-
Physics2D.I._factory.set_RigibBody_Transform(this._box2DBody, sp.globalTrans.x, sp.globalTrans.y, Utils.toRadian(this.owner.globalTrans.rotation));
310+
_refresh(): void {
309311
}
310312

311313
/**@internal*/
@@ -431,7 +433,15 @@ export class ColliderBase extends Component {
431433

432434
/**@internal @deprecated 通知rigidBody 更新shape 属性值 */
433435
protected _needupdataShapeAttribute(): void {
434-
this._rigidbody && this.createShape(this._rigidbody);
436+
//兼容模式下使用,设置类似BoxCollider的偏移方式
437+
if (this._rigidbody && this._rigidbody.applyOwnerColliderComponent) {
438+
this.createShape(this._rigidbody);
439+
}
440+
//非dynamic类型下可以直接设置位置
441+
if (this._type != "dynamic") {
442+
var sp: Sprite = this.owner;
443+
this._box2DBody && Physics2D.I._factory.set_RigibBody_Transform(this._box2DBody, sp.globalTrans.x, sp.globalTrans.y, Utils.toRadian(this.owner.globalTrans.rotation));
444+
}
435445
}
436446

437447
// ----------------------- 已废弃 deprecated ------------------------

src/layaAir/laya/physics/ModuleDef.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@ import { PolygonCollider } from "./Collider2D/PolygonCollider";
2020
import { RigidBody } from "./RigidBody";
2121
import { StaticCollider } from "./StaticCollider";
2222
import { Physics2DShapeBase } from "./Shape/Physics2DShapeBase";
23-
import { BoxShape } from "./Shape/BoxShape";
24-
import { ChainShape } from "./Shape/ChainShape";
25-
import { EdgeShape } from "./Shape/EdgeShape";
26-
import { PolygonShape } from "./Shape/PolygonShape";
23+
import { BoxShape2D } from "./Shape/BoxShape2D";
24+
import { ChainShape2D } from "./Shape/ChainShape2D";
25+
import { EdgeShape2D } from "./Shape/EdgeShape2D";
26+
import { PolygonShape2D } from "./Shape/PolygonShape2D";
2727
import { Physics2DWorldManager } from "./Physics2DWorldManager";
28+
import { CircleShape2D } from "./Shape/CircleShape2D";
29+
import { FilterData } from "./factory/IPhysics2DFactory";
2830

2931
let c = ClassUtils.regClass;
3032
c("Physics2D", Physics2D);
3133
c("Physics2DDebugDraw", Physics2DDebugDraw);
3234
c("Physics2DWorldManager", Physics2DWorldManager);
35+
c("FilterData", FilterData);
3336

3437
c("ColliderBase", ColliderBase);
3538
c("RigidBody", RigidBody);
@@ -52,9 +55,9 @@ c("CircleCollider", CircleCollider);
5255
c("EdgeCollider", EdgeCollider);
5356
c("PolygonCollider", PolygonCollider);
5457

55-
5658
c("Physics2DShapeBase", Physics2DShapeBase);
57-
c("BoxShape", BoxShape);
58-
c("ChainShape", ChainShape);
59-
c("EdgeShape", EdgeShape);
60-
c("PolygonShape", PolygonShape);
59+
c("BoxShape2D", BoxShape2D);
60+
c("CircleShape2D", CircleShape2D);
61+
c("ChainShape2D", ChainShape2D);
62+
c("EdgeShape2D", EdgeShape2D);
63+
c("PolygonShape2D", PolygonShape2D);

src/layaAir/laya/physics/Physics2DWorldManager.ts

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class Physics2DWorldManager implements IElementComponentManager {
2828
private _contactListener: any;
2929
private _JSQuerycallback: any;
3030
private _JSRayCastcallback: any;
31+
private _allowWorldSleep: boolean = false;
3132

3233
get box2DWorld(): any {
3334
return this._box2DWorld;
@@ -49,6 +50,7 @@ export class Physics2DWorldManager implements IElementComponentManager {
4950
this._worldDef.velocityIterations = this._velocityIterations = Physics2DOption.velocityIterations;
5051
this._worldDef.positionIterations = this._positionIterations = Physics2DOption.positionIterations;
5152
this._worldDef.gravity = this._gravity.setValue(Physics2DOption.gravity.x, Physics2DOption.gravity.y);
53+
this._allowWorldSleep = Physics2DOption.allowSleeping;
5254
this._scene = scene;
5355
this.setRootSprite(this._scene);
5456
}
@@ -69,6 +71,7 @@ export class Physics2DWorldManager implements IElementComponentManager {
6971
setRootSprite(scene: Scene | Sprite): void {
7072
this._scene = scene;
7173
this._box2DWorld = Physics2D.I._factory.createWorld(this._worldDef);
74+
Physics2D.I._factory.allowWorldSleep(this._box2DWorld, this._allowWorldSleep);
7275
this._box2DWorld._pixelRatio = this._pixelRatio;
7376
this._box2DWorld._indexInMap = Physics2D.I._factory.worldCount;
7477
Physics2D.I._factory.worldMap.set(Physics2D.I._factory.worldCount, this);

src/layaAir/laya/physics/RigidBody.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class RigidBody extends ColliderBase {
100100
/**
101101
* @zh 碰撞形状,可以是多个
102102
*/
103-
private _shapes: Physics2DShapeBase[] = [];
103+
private _shapes: Physics2DShapeBase[];
104104

105105
/**
106106
* @zh 是否兼容Collider组件的方式
@@ -264,11 +264,17 @@ export class RigidBody extends ColliderBase {
264264
}
265265

266266
public set shapes(shapes: Physics2DShapeBase[]) {
267+
if (!shapes || shapes.length == 0) return;
268+
this._shapes = shapes;
267269
shapes.forEach((shape) => {
268270
shape.setCollider(this);
269271
});
270-
this._shapes = shapes;
271-
272+
if (this._useAutoMass) {
273+
// 根据shape自动计算质量
274+
this._box2DBody && Physics2D.I._factory.retSet_rigidBody_MassData(this._box2DBody);
275+
} else {
276+
this._box2DBody && Physics2D.I._factory.set_rigidBody_Mass(this._box2DBody, this._mass, this._centerOfMass, this._inertia, this._massData);
277+
}
272278
}
273279

274280
/**
@@ -364,16 +370,16 @@ export class RigidBody extends ColliderBase {
364370
let owner: Sprite = this.owner;
365371
this._bodyDef.position.setValue(owner.globalTrans.x, owner.globalTrans.y);
366372
this._bodyDef.angle = Utils.toRadian(owner.globalTrans.rotation);
373+
this._bodyDef.fixedRotation = !this._allowRotation;
367374
this._bodyDef.allowSleep = this._allowSleep;
368375
this._bodyDef.angularVelocity = this._angularVelocity;
369376
this._bodyDef.angularDamping = this._angularDamping;
370377
this._bodyDef.linearDamping = this._linearDamping;
371-
this._bodyDef.linearVelocity.setValue(this._linearVelocity.x, this._linearVelocity.y);
378+
if (this._linearVelocity.x != 0 || this._linearVelocity.y != 0) {
379+
this._bodyDef.linearVelocity.setValue(this._linearVelocity.x, this._linearVelocity.y);
380+
}
372381
this._bodyDef.type = this._type;
373-
374382
this._bodyDef.bullet = this._bullet;
375-
this._bodyDef.fixedRotation = !this._allowRotation;
376-
377383
this._bodyDef.gravityScale = this._gravityScale;
378384
this._bodyDef.group = this.group;
379385
}
@@ -393,12 +399,6 @@ export class RigidBody extends ColliderBase {
393399
//更新shapes
394400
this.shapes = this._shapes;
395401
}
396-
if (this._useAutoMass) {
397-
// 根据shape自动计算质量
398-
Physics2D.I._factory.retSet_rigidBody_MassData(this._box2DBody);
399-
} else {
400-
Physics2D.I._factory.set_rigidBody_Mass(this._box2DBody, this._mass, this._centerOfMass, this._inertia, this._massData);
401-
}
402402
this.owner.on(SpriteGlobalTransform.CHANGED, this, this._globalChangeHandler);
403403
}
404404

@@ -446,6 +446,7 @@ export class RigidBody extends ColliderBase {
446446
}
447447

448448
getUserData(): any {
449+
if (!this._box2DBody) return;
449450
return Physics2D.I._factory.get_rigidBody_userData(this._box2DBody);
450451
}
451452

src/layaAir/laya/physics/Shape/BoxShape.ts renamed to src/layaAir/laya/physics/Shape/BoxShape2D.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { EPhysics2DShape } from "../factory/IPhysics2DFactory";
33
import { Physics2D } from "../Physics2D";
44
import { Physics2DShapeBase } from "./Physics2DShapeBase";
55

6-
export class BoxShape extends Physics2DShapeBase {
6+
export class BoxShape2D extends Physics2DShapeBase {
77

88
private _width: number = 100;
99

@@ -51,13 +51,13 @@ export class BoxShape extends Physics2DShapeBase {
5151

5252
}
5353

54-
clone(): BoxShape {
55-
let dest: BoxShape = new BoxShape();
54+
clone(): BoxShape2D {
55+
let dest: BoxShape2D = new BoxShape2D();
5656
this.cloneTo(dest);
5757
return dest;
5858
}
5959

60-
cloneTo(destObject: BoxShape): void {
60+
cloneTo(destObject: BoxShape2D): void {
6161
super.cloneTo(destObject);
6262
destObject.width = this.width;
6363
destObject.height = this.height;

src/layaAir/laya/physics/Shape/ChainShape.ts renamed to src/layaAir/laya/physics/Shape/ChainShape2D.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { EPhysics2DShape } from "../factory/IPhysics2DFactory";
33
import { Physics2D } from "../Physics2D";
44
import { Physics2DShapeBase } from "./Physics2DShapeBase";
55

6-
export class ChainShape extends Physics2DShapeBase {
6+
export class ChainShape2D extends Physics2DShapeBase {
77

88
/**@internal 顶点数据*/
9-
private _datas: number[] = [];
9+
private _datas: number[] = [0, 0, 100, 0];
1010

1111
/**@internal 是否是闭环,注意不要有自相交的链接形状,它可能不能正常工作*/
1212
private _loop: boolean = false;
@@ -58,13 +58,13 @@ export class ChainShape extends Physics2DShapeBase {
5858
Physics2D.I._factory.set_ChainShape_data(shape, this.pivotoffx, this.pivotoffy, this._datas, this._loop, this.scaleX, this.scaleY);
5959
}
6060

61-
clone(): ChainShape {
62-
let dest: ChainShape = new ChainShape();
61+
clone(): ChainShape2D {
62+
let dest: ChainShape2D = new ChainShape2D();
6363
this.cloneTo(dest);
6464
return dest;
6565
}
6666

67-
cloneTo(destObject: ChainShape): void {
67+
cloneTo(destObject: ChainShape2D): void {
6868
super.cloneTo(destObject);
6969
destObject.datas = this.datas;
7070
destObject.loop = this.loop;

src/layaAir/laya/physics/Shape/CircleShape.ts renamed to src/layaAir/laya/physics/Shape/CircleShape2D.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { EPhysics2DShape } from "../factory/IPhysics2DFactory";
33
import { Physics2D } from "../Physics2D";
44
import { Physics2DShapeBase } from "./Physics2DShapeBase";
55

6-
export class CircleShape extends Physics2DShapeBase {
6+
export class CircleShape2D extends Physics2DShapeBase {
77
private _radius: number = 50;
88

99

@@ -33,13 +33,13 @@ export class CircleShape extends Physics2DShapeBase {
3333
Physics2D.I._factory.set_CircleShape_pos(this._box2DShapeDef._shape, this.x, this.y, scale);
3434
}
3535

36-
clone(): CircleShape {
37-
let dest: CircleShape = new CircleShape();
36+
clone(): CircleShape2D {
37+
let dest: CircleShape2D = new CircleShape2D();
3838
this.cloneTo(dest);
3939
return dest;
4040
}
4141

42-
cloneTo(destObject: CircleShape): void {
42+
cloneTo(destObject: CircleShape2D): void {
4343
super.cloneTo(destObject);
4444
destObject.radius = this.radius;
4545
}

src/layaAir/laya/physics/Shape/EdgeShape.ts renamed to src/layaAir/laya/physics/Shape/EdgeShape2D.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { EPhysics2DShape } from "../factory/IPhysics2DFactory";
33
import { Physics2D } from "../Physics2D";
44
import { Physics2DShapeBase } from "./Physics2DShapeBase";
55

6-
export class EdgeShape extends Physics2DShapeBase {
6+
export class EdgeShape2D extends Physics2DShapeBase {
77

88
/**@internal 顶点数据*/
99
private _datas: number[] = [0, 0, 100, 0];
@@ -41,13 +41,13 @@ export class EdgeShape extends Physics2DShapeBase {
4141
Physics2D.I._factory.set_EdgeShape_data(shape, this.pivotoffx, this.pivotoffy, this._datas, this.scaleX, this.scaleY);
4242
}
4343

44-
clone(): EdgeShape {
45-
let dest: EdgeShape = new EdgeShape();
44+
clone(): EdgeShape2D {
45+
let dest: EdgeShape2D = new EdgeShape2D();
4646
this.cloneTo(dest);
4747
return dest;
4848
}
4949

50-
cloneTo(destObject: EdgeShape): void {
50+
cloneTo(destObject: EdgeShape2D): void {
5151
super.cloneTo(destObject);
5252
destObject.datas = this.datas;
5353
}

src/layaAir/laya/physics/Shape/Physics2DShapeBase.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export class Physics2DShapeBase implements IClone {
148148
_updateFilterData(): void {
149149
if (!this._box2DShape || !this._box2DFilter) return;
150150
this._box2DFilter.groupIndex = this._filterData.group;
151-
this._box2DFilter.categoryBits = this._filterData.catagory;
151+
this._box2DFilter.categoryBits = this._filterData.category;
152152
this._box2DFilter.maskBits = this._filterData.mask;
153153
this._shapeDef.filter = this._filterData;
154154
Physics2D.I._factory.setfilterData(this._box2DShape, this._box2DFilter);

0 commit comments

Comments
 (0)