mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-03 01:12:42 +08:00
Fix physic component (#4490)
* Fix physic component * rename ' _triggered ' to ' _isTrigger ' in rigid-body-component * Code stylized
This commit is contained in:
@@ -17,20 +17,20 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
|
||||
protected _shapeBase!: ShapeBase;
|
||||
|
||||
@property
|
||||
private _triggered: boolean = false;
|
||||
private _isTrigger: boolean = false;
|
||||
|
||||
@property
|
||||
get isTrigger () { return this._triggered; }
|
||||
get isTrigger () {
|
||||
return this._isTrigger;
|
||||
}
|
||||
|
||||
set isTrigger (value) {
|
||||
this._triggered = value;
|
||||
this._isTrigger = value;
|
||||
|
||||
const type = this._triggered ? ERigidBodyType.DYNAMIC : ERigidBodyType.STATIC;
|
||||
if (this.sharedBody) {
|
||||
if (!CC_EDITOR) {
|
||||
const type = this._isTrigger ? ERigidBodyType.DYNAMIC : ERigidBodyType.STATIC;
|
||||
this.sharedBody.body.setType(type);
|
||||
}
|
||||
if (this._shapeBase) {
|
||||
this._shapeBase.setCollisionResponse(!this._triggered);
|
||||
this._shapeBase.setCollisionResponse(!this._isTrigger);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,8 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
|
||||
|
||||
set center (value: Vec3) {
|
||||
vec3.copy(this._center, value);
|
||||
if (this._shapeBase) {
|
||||
|
||||
if (!CC_EDITOR) {
|
||||
this._shapeBase.setCenter(this._center);
|
||||
}
|
||||
}
|
||||
@@ -56,17 +57,13 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
|
||||
super();
|
||||
}
|
||||
|
||||
public __preload () {
|
||||
super.__preload();
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
if (this.sharedBody) {
|
||||
if (!CC_EDITOR) {
|
||||
// init collider
|
||||
this.sharedBody.transfromSource = ETransformSource.SCENE;
|
||||
// useGravity属性默认是为true的
|
||||
this.sharedBody.body.setUseGravity(false);
|
||||
this.center = this._center;
|
||||
this.isTrigger = this._triggered;
|
||||
this.isTrigger = this._isTrigger;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,13 +76,14 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
|
||||
|
||||
public onEnable () {
|
||||
super.onEnable();
|
||||
if (this.sharedBody) {
|
||||
|
||||
if (!CC_EDITOR) {
|
||||
this.sharedBody.body.addShape(this._shapeBase!);
|
||||
}
|
||||
}
|
||||
|
||||
public onDisable () {
|
||||
if (this.sharedBody) {
|
||||
if (!CC_EDITOR) {
|
||||
this.sharedBody.body.removeShape(this._shapeBase!);
|
||||
|
||||
if (this.sharedBody.isShapeOnly) {
|
||||
@@ -95,7 +93,7 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
|
||||
}
|
||||
|
||||
public onDestroy () {
|
||||
if (this.sharedBody) {
|
||||
if (!CC_EDITOR) {
|
||||
this.sharedBody.body.removeShape(this._shapeBase!);
|
||||
}
|
||||
super.onDestroy();
|
||||
@@ -110,21 +108,25 @@ export class BoxColliderComponent extends ColliderComponentBase {
|
||||
@property
|
||||
private _size: Vec3 = new Vec3(0, 0, 0);
|
||||
|
||||
private _shape: BoxShapeBase | undefined;
|
||||
private _shape!: BoxShapeBase;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
if (!CC_EDITOR) {
|
||||
// 父类在__preload要对_shapeBase进行操作,需要保证其已经构造完成
|
||||
this._shape = createBoxShape(this._size);
|
||||
this._shape.setUserData(this);
|
||||
this._shapeBase = this._shape;
|
||||
}
|
||||
}
|
||||
|
||||
public __preload () {
|
||||
// 父类在__preload要对_shapeBase进行操作,需要保证其已经构造完成
|
||||
this._shape = createBoxShape(this._size);
|
||||
this._shape.setUserData(this);
|
||||
this._shapeBase = this._shape;
|
||||
public onLoad () {
|
||||
super.onLoad();
|
||||
|
||||
super.__preload();
|
||||
|
||||
this._shape.setScale(this.node._scale);
|
||||
if (!CC_EDITOR) {
|
||||
this.size = this._size;
|
||||
this._shape.setScale(this.node._scale);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,10 +140,9 @@ export class BoxColliderComponent extends ColliderComponentBase {
|
||||
|
||||
set size (value) {
|
||||
vec3.copy(this._size, value);
|
||||
if (this._shape) {
|
||||
|
||||
if (!CC_EDITOR) {
|
||||
this._shape.setSize(this._size);
|
||||
}
|
||||
if (this.sharedBody) {
|
||||
this.sharedBody.body.commitShapeUpdates();
|
||||
}
|
||||
}
|
||||
@@ -159,18 +160,18 @@ export class SphereColliderComponent extends ColliderComponentBase {
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
}
|
||||
|
||||
public __preload () {
|
||||
// const scale = this.node._scale;
|
||||
// const max = Math.abs(Math.max(scale.x, Math.max(scale.y, scale.z)));
|
||||
this._shape = createSphereShape(this._radius);
|
||||
this._shape.setUserData(this);
|
||||
this._shapeBase = this._shape;
|
||||
}
|
||||
|
||||
super.__preload();
|
||||
public onLoad () {
|
||||
super.onLoad();
|
||||
|
||||
this._shape.setScale(this.node._scale);
|
||||
if (!CC_EDITOR) {
|
||||
this.radius = this._radius;
|
||||
this._shape.setScale(this.node._scale);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,10 +184,9 @@ export class SphereColliderComponent extends ColliderComponentBase {
|
||||
|
||||
set radius (value) {
|
||||
this._radius = value;
|
||||
if (this._shape) {
|
||||
|
||||
if (!CC_EDITOR) {
|
||||
this._shape.setRadius(value);
|
||||
}
|
||||
if (this.sharedBody) {
|
||||
this.sharedBody.body.commitShapeUpdates();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,38 +12,66 @@ import { stringfyQuat, stringfyVec3 } from '../../../physics/util';
|
||||
export class PhysicsBasedComponent extends Component {
|
||||
|
||||
protected get _body () {
|
||||
return this._sharedBody ? this._sharedBody.body : null;
|
||||
return this._sharedBody.body;
|
||||
}
|
||||
|
||||
protected get sharedBody () {
|
||||
return this._sharedBody;
|
||||
}
|
||||
private _sharedBody!: SharedRigidBody | null;
|
||||
|
||||
protected get _assertPreload (): boolean {
|
||||
if (!this._isPreLoaded) {
|
||||
console.error('Physic Error :', 'Please make sure that the node has been added to the scene');
|
||||
}
|
||||
return this._isPreLoaded;
|
||||
}
|
||||
|
||||
private _sharedBody!: SharedRigidBody;
|
||||
|
||||
private _isPreLoaded: boolean = false;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
}
|
||||
|
||||
public __preload () {
|
||||
this._refSharedBody();
|
||||
if (!CC_EDITOR) {
|
||||
if (this._sharedBody == null) {
|
||||
const physicsBasedComponents = this.node.getComponents(PhysicsBasedComponent);
|
||||
let sharedBody: SharedRigidBody | null = null;
|
||||
for (const physicsBasedComponent of physicsBasedComponents) {
|
||||
if (physicsBasedComponent._sharedBody) {
|
||||
sharedBody = physicsBasedComponent._sharedBody;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!sharedBody) {
|
||||
sharedBody = new SharedRigidBody(this.node, cc.director._physicsSystem.world);
|
||||
}
|
||||
sharedBody.ref();
|
||||
this._sharedBody = sharedBody;
|
||||
}
|
||||
|
||||
this._isPreLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
public onEnable () {
|
||||
if (this.sharedBody) {
|
||||
this.sharedBody!.enable();
|
||||
if (!CC_EDITOR) {
|
||||
this.sharedBody.enable();
|
||||
}
|
||||
}
|
||||
|
||||
public onDisable () {
|
||||
if (this.sharedBody) {
|
||||
this.sharedBody!.disable();
|
||||
if (!CC_EDITOR) {
|
||||
this.sharedBody.disable();
|
||||
}
|
||||
}
|
||||
|
||||
public onDestroy () {
|
||||
if (this._sharedBody) {
|
||||
if (!CC_EDITOR) {
|
||||
this._sharedBody.deref();
|
||||
this._sharedBody = null;
|
||||
(this._sharedBody as any) = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,21 +79,29 @@ export class PhysicsBasedComponent extends Component {
|
||||
* @return group ∈ [0, 31] (int)
|
||||
*/
|
||||
public getGroup (): number {
|
||||
return this._body!.getGroup();
|
||||
if (this._assertPreload) {
|
||||
return this._body!.getGroup();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param v ∈ [0, 31] (int)
|
||||
*/
|
||||
public setGroup (v: number) {
|
||||
return this._body!.setGroup(v);
|
||||
if (this._assertPreload) {
|
||||
return this._body!.setGroup(v);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return (int)
|
||||
*/
|
||||
public getMask (): number {
|
||||
return this._body!.getMask();
|
||||
if (this._assertPreload) {
|
||||
return this._body!.getMask();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +109,9 @@ export class PhysicsBasedComponent extends Component {
|
||||
* @param v ∈ [0, 31] (int)
|
||||
*/
|
||||
public setMask (v: number) {
|
||||
return this._body!.setMask(v);
|
||||
if (this._assertPreload) {
|
||||
return this._body!.setMask(v);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,26 +119,9 @@ export class PhysicsBasedComponent extends Component {
|
||||
* @param v ∈ [0, 31] (int)
|
||||
*/
|
||||
public addMask (v: number) {
|
||||
return this._body!.addMask(v);
|
||||
}
|
||||
|
||||
private _refSharedBody () {
|
||||
if (this._sharedBody) {
|
||||
return;
|
||||
if (this._assertPreload) {
|
||||
return this._body!.addMask(v);
|
||||
}
|
||||
const physicsBasedComponents = this.node.getComponents(PhysicsBasedComponent);
|
||||
let sharedBody: SharedRigidBody | null = null;
|
||||
for (const physicsBasedComponent of physicsBasedComponents) {
|
||||
if (physicsBasedComponent._sharedBody) {
|
||||
sharedBody = physicsBasedComponent._sharedBody;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!sharedBody) {
|
||||
sharedBody = new SharedRigidBody(this.node, cc.director._physicsSystem.world);
|
||||
}
|
||||
sharedBody.ref();
|
||||
this._sharedBody = sharedBody;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,17 +132,17 @@ class SharedRigidBody {
|
||||
|
||||
private _actived = false;
|
||||
|
||||
private _world: PhysicsWorldBase;
|
||||
private _world!: PhysicsWorldBase;
|
||||
|
||||
private _node: Node;
|
||||
private _node!: Node;
|
||||
|
||||
private _worldScale: Vec3 = new Vec3(1, 1, 1);
|
||||
|
||||
private _beforeStepCallback: BeforeStepCallback;
|
||||
private _beforeStepCallback!: BeforeStepCallback;
|
||||
|
||||
private _afterStepCallback: AfterStepCallback;
|
||||
private _afterStepCallback!: AfterStepCallback;
|
||||
|
||||
private _onCollidedCallback: ICollisionCallback;
|
||||
private _onCollidedCallback!: ICollisionCallback;
|
||||
|
||||
private _transformInitialized: boolean = false;
|
||||
|
||||
@@ -142,8 +163,6 @@ class SharedRigidBody {
|
||||
this._beforeStepCallback = this._beforeStep.bind(this);
|
||||
this._afterStepCallback = this._afterStep.bind(this);
|
||||
this._onCollidedCallback = this._onCollided.bind(this);
|
||||
this._body.addCollisionCallback(this._onCollidedCallback);
|
||||
this._world.addBeforeStep(this._beforeStepCallback);
|
||||
}
|
||||
|
||||
public get body () {
|
||||
@@ -160,18 +179,13 @@ class SharedRigidBody {
|
||||
}
|
||||
|
||||
public ref () {
|
||||
// if (!this._refCount) {
|
||||
// this._activeBody();
|
||||
// }
|
||||
++this._refCount;
|
||||
}
|
||||
|
||||
public deref () {
|
||||
--this._refCount;
|
||||
if (!this._refCount) {
|
||||
// this._deactiveBody();
|
||||
// TODO : destroy this body
|
||||
// this.destroy();
|
||||
this.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,6 +197,16 @@ class SharedRigidBody {
|
||||
this._deactiveBody();
|
||||
}
|
||||
|
||||
public destroy () {
|
||||
this._body.setUserData(null);
|
||||
(this._body as any) = null;
|
||||
(this._beforeStepCallback as any) = null;
|
||||
(this._afterStepCallback as any) = null;
|
||||
(this._onCollidedCallback as any) = null;
|
||||
(this._world as any) = null;
|
||||
(this._node as any) = null;
|
||||
}
|
||||
|
||||
public syncPhysWithScene (node: Node) {
|
||||
// sync position rotation
|
||||
const p = node.getWorldPosition();
|
||||
@@ -229,7 +253,8 @@ class SharedRigidBody {
|
||||
|
||||
this._actived = true;
|
||||
this._body.setWorld(this._world);
|
||||
// this._world.addBeforeStep(this._beforeStepCallback);
|
||||
this._body.addCollisionCallback(this._onCollidedCallback);
|
||||
this._world.addBeforeStep(this._beforeStepCallback);
|
||||
this._world.addAfterStep(this._afterStepCallback);
|
||||
this._body.wakeUp();
|
||||
}
|
||||
@@ -241,6 +266,7 @@ class SharedRigidBody {
|
||||
this._actived = false;
|
||||
this._world.removeBeforeStep(this._beforeStepCallback);
|
||||
this._world.removeAfterStep(this._afterStepCallback);
|
||||
this._body.removeCollisionCllback(this._onCollidedCallback);
|
||||
this._body.sleep();
|
||||
this._body.setWorld(null);
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
private _fixedRotation: boolean = false;
|
||||
|
||||
@property
|
||||
private _triggered: boolean = false;
|
||||
private _isTrigger: boolean = false;
|
||||
|
||||
@property
|
||||
private _isKinematic: boolean = false;
|
||||
@@ -54,10 +54,6 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
super();
|
||||
}
|
||||
|
||||
public __preload () {
|
||||
super.__preload();
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
/**
|
||||
* 此处设置刚体属性是因为__preload不受executionOrder的顺序影响,
|
||||
@@ -85,7 +81,7 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
|
||||
set material (value) {
|
||||
this._material = value;
|
||||
// if (this._body) {
|
||||
// if (!CC_EDITOR) {
|
||||
// this._body.material = (this._material || DefaultPhysicsMaterial)._getImpl();
|
||||
// }
|
||||
}
|
||||
@@ -97,7 +93,7 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
|
||||
set mass (value) {
|
||||
this._mass = value;
|
||||
if (this._body) {
|
||||
if (!CC_EDITOR) {
|
||||
this._body.setMass(value);
|
||||
}
|
||||
}
|
||||
@@ -109,7 +105,7 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
|
||||
set isKinematic (value) {
|
||||
this._isKinematic = value;
|
||||
if (this._body) {
|
||||
if (!CC_EDITOR) {
|
||||
this._body.setIsKinematic(value);
|
||||
}
|
||||
}
|
||||
@@ -121,7 +117,7 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
|
||||
set useGravity (value) {
|
||||
this._useGravity = value;
|
||||
if (this._body) {
|
||||
if (!CC_EDITOR) {
|
||||
this._body.setUseGravity(value);
|
||||
}
|
||||
}
|
||||
@@ -133,7 +129,7 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
|
||||
set linearDamping (value) {
|
||||
this._linearDamping = value;
|
||||
if (this._body) {
|
||||
if (!CC_EDITOR) {
|
||||
this._body.setLinearDamping(value);
|
||||
}
|
||||
}
|
||||
@@ -145,7 +141,7 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
|
||||
set angularDamping (value) {
|
||||
this._angularDamping = value;
|
||||
if (this._body) {
|
||||
if (!CC_EDITOR) {
|
||||
this._body.setAngularDamping(value);
|
||||
}
|
||||
}
|
||||
@@ -157,53 +153,60 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
|
||||
set fixedRotation (value) {
|
||||
this._fixedRotation = value;
|
||||
if (this._body) {
|
||||
if (!CC_EDITOR) {
|
||||
this._body.setFreezeRotation(value);
|
||||
}
|
||||
}
|
||||
|
||||
get isTrigger () {
|
||||
return this._triggered;
|
||||
return this._isTrigger;
|
||||
}
|
||||
|
||||
set isTrigger (value) {
|
||||
this._triggered = value;
|
||||
if (this._body) {
|
||||
this._isTrigger = value;
|
||||
if (!CC_EDITOR) {
|
||||
this._body.setIsTrigger(value);
|
||||
}
|
||||
}
|
||||
|
||||
get velocity () {
|
||||
if (this._body) {
|
||||
vec3.copy(this._velocity, this._body!.getVelocity());
|
||||
}
|
||||
return this._velocity;
|
||||
}
|
||||
|
||||
set velocity (value: Vec3) {
|
||||
vec3.copy(this._velocity, value);
|
||||
if (this._body) {
|
||||
if (!CC_EDITOR) {
|
||||
this._body.setVelocity(this._velocity);
|
||||
}
|
||||
}
|
||||
|
||||
public applyForce (force: Vec3, position?: Vec3) {
|
||||
this._body!.applyForce(force, position);
|
||||
if (this._assertPreload) {
|
||||
this._body!.applyForce(force, position);
|
||||
}
|
||||
}
|
||||
|
||||
public applyImpulse (impulse: Vec3, position?: Vec3) {
|
||||
this._body!.applyImpulse(impulse, position);
|
||||
if (this._assertPreload) {
|
||||
this._body!.applyImpulse(impulse, position);
|
||||
}
|
||||
}
|
||||
|
||||
public wakeUp () {
|
||||
this._body!.wakeUp();
|
||||
if (this._assertPreload) {
|
||||
this._body!.wakeUp();
|
||||
}
|
||||
}
|
||||
|
||||
public sleep () {
|
||||
this._body!.sleep();
|
||||
if (this._assertPreload) {
|
||||
this._body!.sleep();
|
||||
}
|
||||
}
|
||||
|
||||
public setCollisionFilter (group: number, mask: number) {
|
||||
this._body!.setCollisionFilter(group, mask);
|
||||
if (this._assertPreload) {
|
||||
this._body!.setCollisionFilter(group, mask);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user