split box\sphere collider component from base collider component (#4541)

This commit is contained in:
jiaxin.lai
2019-06-03 19:26:43 +08:00
committed by Weiliang Wang
parent 63c3bdc31e
commit 4cbd3e63f2
6 changed files with 320 additions and 251 deletions
+7 -3
View File
@@ -35,7 +35,9 @@ import { BillboardComponent } from './particle/billboard-component';
import { LineComponent } from './particle/line-component';
import { ParticleSystemComponent } from './particle/particle-system-component';
import { ParticleUtils } from './particle/particle-utils';
import { BoxColliderComponent, SphereColliderComponent } from './physics/collider-component';
import { BoxColliderComponent } from './physics/collider/box-collider-component';
import { ColliderComponentBase } from './physics/collider/collider-component';
import { SphereColliderComponent } from './physics/collider/sphere-collider-component';
import { RigidBodyComponent } from './physics/rigid-body-component';
import { RenderableComponent } from './renderable-component';
import { SkinningModelComponent } from './skinning-model-component';
@@ -77,11 +79,13 @@ cc.DirectionalLightComponent = DirectionalLightComponent;
cc.SphereLightComponent = SphereLightComponent;
cc.SpotLightComponent = SpotLightComponent;
cc.BoxColliderComponent = BoxColliderComponent;
cc.ParticleSystemComponent = ParticleSystemComponent;
cc.BillboardComponent = BillboardComponent;
cc.LineComponent = LineComponent;
cc.RigidBodyComponent = RigidBodyComponent;
cc.ColliderComponentBase = ColliderComponentBase;
cc.BoxColliderComponent = BoxColliderComponent;
cc.SphereColliderComponent = SphereColliderComponent;
cc.RigidBodyComponent = RigidBodyComponent;
cc.ParticleUtils = ParticleUtils;
@@ -1,193 +0,0 @@
import {
ccclass,
executeInEditMode,
executionOrder,
menu,
property,
} from '../../../core/data/class-decorator';
import Vec3 from '../../../core/value-types/vec3';
import { vec3 } from '../../../core/vmath';
import { BoxShapeBase, ShapeBase, SphereShapeBase } from '../../physics/api';
import { createBoxShape, createSphereShape } from '../../physics/instance';
import { ERigidBodyType, ETransformSource } from '../../physics/physic-enum';
import { PhysicsBasedComponent } from './detail/physics-based-component';
@ccclass('cc.ColliderComponentBase')
export class ColliderComponentBase extends PhysicsBasedComponent {
protected _shapeBase!: ShapeBase;
@property
private _isTrigger: boolean = false;
@property
get isTrigger () {
return this._isTrigger;
}
set isTrigger (value) {
this._isTrigger = value;
if (!CC_EDITOR) {
const type = this._isTrigger ? ERigidBodyType.DYNAMIC : ERigidBodyType.STATIC;
this.sharedBody.body.setType(type);
this._shapeBase.setCollisionResponse(!this._isTrigger);
}
}
@property
private _center: Vec3 = new cc.Vec3(0, 0, 0);
/**
* The center of the collider, in local space.
*/
@property({ type: Vec3 })
get center () {
return this._center;
}
set center (value: Vec3) {
vec3.copy(this._center, value);
if (!CC_EDITOR) {
this._shapeBase.setCenter(this._center);
}
}
constructor () {
super();
}
public onLoad () {
if (!CC_EDITOR) {
// init collider
this.sharedBody.transfromSource = ETransformSource.SCENE;
this.sharedBody.body.setUseGravity(false);
this.center = this._center;
this.isTrigger = this._isTrigger;
}
}
// public lateUpdate () {
// TODO : 根据node的scale dirty标记去缩放shape
// if (this.node.hasChanged) {
// this._shapeBase.setScale(this.node._scale);
// }
// }
public onEnable () {
super.onEnable();
if (!CC_EDITOR) {
this.sharedBody.body.addShape(this._shapeBase!);
}
}
public onDisable () {
if (!CC_EDITOR) {
this.sharedBody.body.removeShape(this._shapeBase!);
if (this.sharedBody.isShapeOnly) {
super.onDisable();
}
}
}
public onDestroy () {
if (!CC_EDITOR) {
this.sharedBody.body.removeShape(this._shapeBase!);
}
super.onDestroy();
}
}
@ccclass('cc.BoxColliderComponent')
@executionOrder(98)
@menu('Components/BoxColliderComponent')
@executeInEditMode
export class BoxColliderComponent extends ColliderComponentBase {
@property
private _size: Vec3 = new Vec3(0, 0, 0);
private _shape!: BoxShapeBase;
constructor () {
super();
if (!CC_EDITOR) {
// 父类在__preload要对_shapeBase进行操作,需要保证其已经构造完成
this._shape = createBoxShape(this._size);
this._shape.setUserData(this);
this._shapeBase = this._shape;
}
}
public onLoad () {
super.onLoad();
if (!CC_EDITOR) {
this.size = this._size;
this._shape.setScale(this.node._scale);
}
}
/**
* The size of the box, in local space.
* @note Shall not specify size with component 0.
*/
@property({ type: Vec3 })
get size () {
return this._size;
}
set size (value) {
vec3.copy(this._size, value);
if (!CC_EDITOR) {
this._shape.setSize(this._size);
this.sharedBody.body.commitShapeUpdates();
}
}
}
@ccclass('cc.SphereColliderComponent')
@executionOrder(98)
@menu('Components/SphereColliderComponent')
@executeInEditMode
export class SphereColliderComponent extends ColliderComponentBase {
@property
private _radius: number = 0;
private _shape!: SphereShapeBase;
constructor () {
super();
this._shape = createSphereShape(this._radius);
this._shape.setUserData(this);
this._shapeBase = this._shape;
}
public onLoad () {
super.onLoad();
if (!CC_EDITOR) {
this.radius = this._radius;
this._shape.setScale(this.node._scale);
}
}
/**
* The radius of the sphere.
*/
@property
get radius () {
return this._radius;
}
set radius (value) {
this._radius = value;
if (!CC_EDITOR) {
this._shape.setRadius(value);
this.sharedBody.body.commitShapeUpdates();
}
}
}
@@ -0,0 +1,66 @@
import {
ccclass,
executeInEditMode,
executionOrder,
menu,
property,
} from '../../../../core/data/class-decorator';
import Vec3 from '../../../../core/value-types/vec3';
import { vec3 } from '../../../../core/vmath';
import { BoxShapeBase } from '../../../physics/api';
import { createBoxShape } from '../../../physics/instance';
import { ColliderComponentBase } from './collider-component';
@ccclass('cc.BoxColliderComponent')
@executionOrder(98)
@menu('Components/BoxColliderComponent')
@executeInEditMode
export class BoxColliderComponent extends ColliderComponentBase {
private _shape!: BoxShapeBase;
/// PRIVATE PROPERTY ///
@property
private _size: Vec3 = new Vec3(0, 0, 0);
constructor () {
super();
if (!CC_EDITOR) {
this._shape = createBoxShape(this._size);
this._shape.setUserData(this);
this._shapeBase = this._shape;
}
}
/// COMPONENT LIFECYCLE ///
public onLoad () {
super.onLoad();
if (!CC_EDITOR) {
this.size = this._size;
this._shape.setScale(this.node._scale);
}
}
/// PUBLIC PROPERTY GETTER\SETTER ///
/**
* The size of the box, in local space.
* @note Shall not specify size with component 0.
*/
@property({ type: Vec3 })
get size () {
return this._size;
}
set size (value) {
vec3.copy(this._size, value);
if (!CC_EDITOR) {
this._shape.setSize(this._size);
this.sharedBody.body.commitShapeUpdates();
}
}
}
@@ -0,0 +1,105 @@
import {
ccclass,
property,
} from '../../../../core/data/class-decorator';
import Vec3 from '../../../../core/value-types/vec3';
import { vec3 } from '../../../../core/vmath';
import { ShapeBase } from '../../../physics/api';
import { ERigidBodyType, ETransformSource } from '../../../physics/physic-enum';
import { PhysicsBasedComponent } from '../detail/physics-based-component';
@ccclass('cc.ColliderComponentBase')
export class ColliderComponentBase extends PhysicsBasedComponent {
protected _shapeBase!: ShapeBase;
/// PRIVATE PROPERTY ///
@property
private _isTrigger: boolean = false;
@property
private _center: Vec3 = new cc.Vec3(0, 0, 0);
constructor () {
super();
}
/// PUBLIC PROPERTY GETTER\SETTER ///
@property({
displayOrder: 0,
})
public get isTrigger () {
return this._isTrigger;
}
public set isTrigger (value) {
this._isTrigger = value;
if (!CC_EDITOR) {
if (!CC_PHYISCS_BUILT_IN) {
const type = this._isTrigger ? ERigidBodyType.DYNAMIC : ERigidBodyType.STATIC;
this.sharedBody.body.setType(type);
this._shapeBase.setCollisionResponse!(!this._isTrigger);
}
}
}
/**
* The center of the collider, in local space.
*/
@property({
type: Vec3,
displayOrder: 1,
tooltip: 'The center of the collider, in local space',
})
public get center () {
return this._center;
}
public set center (value: Vec3) {
vec3.copy(this._center, value);
if (!CC_EDITOR) {
this._shapeBase.setCenter(this._center);
}
}
/// COMPONENT LIFECYCLE ///
public onLoad () {
if (!CC_EDITOR) {
// init collider
this.sharedBody.transfromSource = ETransformSource.SCENE;
this.sharedBody.body.setUseGravity(false);
this.center = this._center;
this.isTrigger = this._isTrigger;
}
}
public onEnable () {
super.onEnable();
if (!CC_EDITOR) {
this.sharedBody.body.addShape(this._shapeBase!);
}
}
public onDisable () {
if (!CC_EDITOR) {
this.sharedBody.body.removeShape(this._shapeBase!);
if (this.sharedBody.isShapeOnly) {
super.onDisable();
}
}
}
public onDestroy () {
if (!CC_EDITOR) {
this.sharedBody.body.removeShape(this._shapeBase!);
}
super.onDestroy();
}
}
@@ -0,0 +1,61 @@
import {
ccclass,
executeInEditMode,
executionOrder,
menu,
property,
} from '../../../../core/data/class-decorator';
import { SphereShapeBase } from '../../../physics/api';
import { createSphereShape } from '../../../physics/instance';
import { ColliderComponentBase } from './collider-component';
@ccclass('cc.SphereColliderComponent')
@executionOrder(98)
@menu('Components/SphereColliderComponent')
@executeInEditMode
export class SphereColliderComponent extends ColliderComponentBase {
private _shape!: SphereShapeBase;
/// PRIVATE PROPERTY ///
@property
private _radius: number = 0;
constructor () {
super();
this._shape = createSphereShape(this._radius);
this._shape.setUserData(this);
this._shapeBase = this._shape;
}
/// COMPONENT LIFECYCLE ///
public onLoad () {
super.onLoad();
if (!CC_EDITOR) {
this.radius = this._radius;
this._shape.setScale(this.node._scale);
}
}
/// PUBLIC PROPERTY GETTER\SETTER ///
/**
* The radius of the sphere.
*/
@property
get radius () {
return this._radius;
}
set radius (value) {
this._radius = value;
if (!CC_EDITOR) {
this._shape.setRadius(value);
this.sharedBody.body.commitShapeUpdates();
}
}
}
@@ -24,6 +24,9 @@ const NonRigidBodyProperties = {
@menu('Components/RigidBodyComponent')
@executeInEditMode
export class RigidBodyComponent extends PhysicsBasedComponent {
/// PRIVATE PROPERTY ///
@property
private _material: PhysicsMaterial | null = null;
@@ -54,6 +57,8 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
super();
}
/// COMPONENT LIFECYCLE ///
public onLoad () {
/**
* 此处设置刚体属性是因为__preload不受executionOrder的顺序影响,
@@ -61,125 +66,146 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
*/
if (this.sharedBody) {
this.sharedBody.transfromSource = ETransformSource.PHYSIC;
this.mass = this._mass;
this.linearDamping = this._linearDamping;
this.angularDamping = this._angularDamping;
this.material = this._material;
this.useGravity = this._useGravity;
this.velocity = this._velocity;
this.isKinematic = this._isKinematic;
this.useGravity = this._useGravity;
this.fixedRotation = this._fixedRotation;
// this.material = this._material;
this.velocity = this._velocity;
}
}
/// PUBLIC PROPERTY GETTER\SETTER ///
// Shielding physics material for alpha version
// @property({
// type: PhysicsMaterial,
// })
// get material () {
// return this._material;
// }
// set material (value) {
// this._material = value;
// // if (!CC_EDITOR) {
// // this._body.material = (this._material || DefaultPhysicsMaterial)._getImpl();
// // }
// }
@property({
type: PhysicsMaterial,
displayOrder: 0,
})
get material () {
return this._material;
}
set material (value) {
this._material = value;
// if (!CC_EDITOR) {
// this._body.material = (this._material || DefaultPhysicsMaterial)._getImpl();
// }
}
@property
get mass () {
public get mass () {
return this._mass;
}
set mass (value) {
public set mass (value) {
this._mass = value;
if (!CC_EDITOR) {
this._body.setMass(value);
}
}
@property
get isKinematic () {
return this._isKinematic;
}
set isKinematic (value) {
this._isKinematic = value;
if (!CC_EDITOR) {
this._body.setIsKinematic(value);
}
}
@property
get useGravity () {
return this._useGravity;
}
set useGravity (value) {
this._useGravity = value;
if (!CC_EDITOR) {
this._body.setUseGravity(value);
}
}
@property
get linearDamping () {
@property({
displayOrder: 1,
})
public get linearDamping () {
return this._linearDamping;
}
set linearDamping (value) {
public set linearDamping (value) {
this._linearDamping = value;
if (!CC_EDITOR) {
this._body.setLinearDamping(value);
}
}
@property
get angularDamping () {
@property({
displayOrder: 2,
})
public get angularDamping () {
return this._angularDamping;
}
set angularDamping (value) {
public set angularDamping (value) {
this._angularDamping = value;
if (!CC_EDITOR) {
this._body.setAngularDamping(value);
}
}
@property
get fixedRotation () {
@property({
displayOrder: 3,
})
public get isKinematic () {
return this._isKinematic;
}
public set isKinematic (value) {
this._isKinematic = value;
if (!CC_EDITOR) {
this._body.setIsKinematic(value);
}
}
@property({
displayOrder: 4,
})
public get useGravity () {
return this._useGravity;
}
public set useGravity (value) {
this._useGravity = value;
if (!CC_EDITOR) {
this._body.setUseGravity(value);
}
}
@property({
displayOrder: 5,
})
public get fixedRotation () {
return this._fixedRotation;
}
set fixedRotation (value) {
public set fixedRotation (value) {
this._fixedRotation = value;
if (!CC_EDITOR) {
this._body.setFreezeRotation(value);
}
}
get isTrigger () {
/// PUBLIC GETTER\SETTER ///
public get isTrigger () {
return this._isTrigger;
}
set isTrigger (value) {
public set isTrigger (value) {
this._isTrigger = value;
if (!CC_EDITOR) {
this._body.setIsTrigger(value);
}
}
get velocity () {
public get velocity () {
return this._velocity;
}
set velocity (value: Vec3) {
public set velocity (value: Vec3) {
vec3.copy(this._velocity, value);
if (!CC_EDITOR) {
this._body.setVelocity(this._velocity);
}
}
/// PUBLIC METHOD ///
public applyForce (force: Vec3, position?: Vec3) {
if (this._assertPreload) {
this._body!.applyForce(force, position);