Add method to collider (#4561)

* Fix bug of Cannon physics

* Add on、off、once to collider-component

* Modify collision event emit param

* Rename ColliderComponentBase to ColliderComponent and export ColliderComponent;

* Add export-api file for export some physics api to user

* on、off、once Change to private

* Collider manage collision event
This commit is contained in:
jiaxin.lai
2019-06-11 17:32:33 +08:00
committed by Weiliang Wang
parent 74fc5b23c3
commit 38c2478b19
10 changed files with 229 additions and 147 deletions
+11 -5
View File
@@ -37,7 +37,7 @@ import { LineComponent } from './particle/line-component';
import { ParticleSystemComponent } from './particle/particle-system-component';
import { ParticleUtils } from './particle/particle-utils';
import { BoxColliderComponent } from './physics/collider/box-collider-component';
import { ColliderComponentBase } from './physics/collider/collider-component';
import { ColliderComponent } 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';
@@ -57,14 +57,20 @@ export {
BatchedSkinningModelComponent,
SkinningModelUnit,
BoxColliderComponent,
ParticleSystemComponent,
BillboardComponent,
LineComponent,
RigidBodyComponent,
RenderableComponent,
};
/// PHYSICS EXPORT ///
export {
ColliderComponent,
BoxColliderComponent,
SphereColliderComponent,
RenderableComponent,
RigidBodyComponent,
};
// cc.AnimationComponent = AnimationComponent;
@@ -89,7 +95,7 @@ cc.ParticleSystemComponent = ParticleSystemComponent;
cc.BillboardComponent = BillboardComponent;
cc.LineComponent = LineComponent;
cc.ColliderComponentBase = ColliderComponentBase;
cc.ColliderComponent = ColliderComponent;
cc.BoxColliderComponent = BoxColliderComponent;
cc.SphereColliderComponent = SphereColliderComponent;
cc.RigidBodyComponent = RigidBodyComponent;
@@ -9,13 +9,13 @@ 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';
import { ColliderComponent } from './collider-component';
@ccclass('cc.BoxColliderComponent')
@executionOrder(98)
@menu('Components/BoxColliderComponent')
@executeInEditMode
export class BoxColliderComponent extends ColliderComponentBase {
export class BoxColliderComponent extends ColliderComponent {
private _shape!: BoxShapeBase;
@@ -35,7 +35,7 @@ export class BoxColliderComponent extends ColliderComponentBase {
/// COMPONENT LIFECYCLE ///
public onLoad () {
protected onLoad () {
super.onLoad();
if (!CC_EDITOR) {
@@ -51,11 +51,11 @@ export class BoxColliderComponent extends ColliderComponentBase {
* @note Shall not specify size with component 0.
*/
@property({ type: Vec3 })
get size () {
public get size () {
return this._size;
}
set size (value) {
public set size (value) {
vec3.copy(this._size, value);
if (!CC_EDITOR) {
@@ -2,28 +2,20 @@ import {
ccclass,
property,
} from '../../../../core/data/class-decorator';
import { CallbacksInvoker, ICallbackTable } from '../../../../core/event/callbacks-invoker';
import { applyMixins, IEventTarget } from '../../../../core/event/event-target-factory';
import { createMap } from '../../../../core/utils/js';
import Vec3 from '../../../../core/value-types/vec3';
import { vec3 } from '../../../../core/vmath';
import { ShapeBase } from '../../../physics/api';
import { ICollisionCallback, ICollisionEvent, RigidBodyBase, ShapeBase } from '../../../physics/api';
import { CollisionCallback, CollisionEventType } from '../../../physics/export-api';
import { ERigidBodyType, ETransformSource } from '../../../physics/physic-enum';
import { PhysicsBasedComponent } from '../detail/physics-based-component';
@ccclass('cc.ColliderComponentBase')
export class ColliderComponentBase extends PhysicsBasedComponent {
@ccclass('cc.ColliderComponent')
export class ColliderComponent extends PhysicsBasedComponent implements IEventTarget {
protected _shapeBase!: ShapeBase;
/// PRIVATE PROPERTY ///
@property
private _isTrigger: boolean = false;
@property
private _center: Vec3 = new cc.Vec3(0, 0, 0);
constructor () {
super();
}
public _callbackTable: ICallbackTable = createMap(true);
/// PUBLIC PROPERTY GETTER\SETTER ///
@@ -66,9 +58,82 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
}
}
protected _shapeBase!: ShapeBase;
private _collisionCallBack!: ICollisionCallback;
/// PRIVATE PROPERTY ///
@property
private _isTrigger: boolean = false;
@property
private _center: Vec3 = new cc.Vec3(0, 0, 0);
constructor () {
super();
if (!CC_EDITOR) {
this._collisionCallBack = this._onCollision.bind(this);
}
}
/// PRIVATE METHOD ///
/**
* @zh
* 注册碰撞事件相关的回调
* @param type - 碰撞事件的类型,可为 'onCollisionEnter' 、 'onCollisionStay' 、 'onCollisionExit';
* @param callback - 注册的回调函数
* @param target - 可选参数,执行回调函数的目标
* @param useCapture - 可选参数,当设置为 true,监听器将在捕获阶段触发,否则将在冒泡阶段触发。默认为 false。
*/
public on (type: CollisionEventType, callback: CollisionCallback, target?: Object, useCapture?: any): any{
}
/**
* @zh
* 取消已经注册的碰撞事件相关的回调
* @param type - 碰撞事件的类型,可为 'onCollisionEnter' 、 'onCollisionStay' 、 'onCollisionExit';
* @param callback - 注册的回调函数
* @param target - 可选参数,执行回调函数的目标
* @param useCapture - 可选参数,当设置为 true,监听器将在捕获阶段触发,否则将在冒泡阶段触发。默认为 false。
*/
public off (type: CollisionEventType, callback: CollisionCallback, target?: Object, useCapture?: any) {
}
/**
* @zh
* 注册碰撞事件相关的回调,但只会执行一次
* @param type - 碰撞事件的类型,可为 'onCollisionEnter' 、 'onCollisionStay' 、 'onCollisionExit';
* @param callback - 注册的回调函数
* @param target - 可选参数,执行回调函数的目标
* @param useCapture - 可选参数,当设置为 true,监听器将在捕获阶段触发,否则将在冒泡阶段触发。默认为 false。
*/
public once (type: CollisionEventType, callback: CollisionCallback, target?: Object, useCapture?: any): any {
}
/**
* IEventTarget implementations, they will be overwrote with the same implementation in EventTarget by applyMixins
*/
public targetOff (keyOrTarget?: CollisionEventType | Object): void {
}
public dispatchEvent (event: Event): void {
}
public hasEventListener (key: CollisionEventType, callback?: CollisionCallback, target?: Object): boolean {
return false;
}
public removeAll (keyOrTarget?: CollisionEventType | Object): void {
}
public emit (key: CollisionEventType, ...args: any[]): void {
}
/// COMPONENT LIFECYCLE ///
public onLoad () {
protected onLoad () {
if (!CC_EDITOR && !CC_PHYSICS_BUILT_IN) {
// init collider
this.sharedBody.transfromSource = ETransformSource.SCENE;
@@ -79,16 +144,18 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
this.center = this._center;
}
public onEnable () {
protected onEnable () {
super.onEnable();
if (!CC_EDITOR) {
this.sharedBody.body.addShape(this._shapeBase!, this._center);
this.sharedBody.body.addCollisionCallback(this._collisionCallBack);
}
}
public onDisable () {
protected onDisable () {
if (!CC_EDITOR) {
this.sharedBody.body.removeCollisionCllback(this._collisionCallBack);
this.sharedBody.body.removeShape(this._shapeBase!);
// TODO : Change to determine the reference count
@@ -98,10 +165,19 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
}
}
public onDestroy () {
protected onDestroy () {
if (!CC_EDITOR) {
this.sharedBody.body.removeShape(this._shapeBase!);
}
super.onDestroy();
}
private _onCollision (type: CollisionEventType, event: ICollisionEvent) {
this.emit(type, type, {
source: (event.source as RigidBodyBase).getUserData() as Node,
target: (event.target as RigidBodyBase).getUserData() as Node,
});
}
}
applyMixins(ColliderComponent, [CallbacksInvoker, EventTarget]);
@@ -7,13 +7,13 @@ import {
} from '../../../../core/data/class-decorator';
import { SphereShapeBase } from '../../../physics/api';
import { createSphereShape } from '../../../physics/instance';
import { ColliderComponentBase } from './collider-component';
import { ColliderComponent } from './collider-component';
@ccclass('cc.SphereColliderComponent')
@executionOrder(98)
@menu('Components/SphereColliderComponent')
@executeInEditMode
export class SphereColliderComponent extends ColliderComponentBase {
export class SphereColliderComponent extends ColliderComponent {
private _shape!: SphereShapeBase;
@@ -31,7 +31,7 @@ export class SphereColliderComponent extends ColliderComponentBase {
/// COMPONENT LIFECYCLE ///
public onLoad () {
protected onLoad () {
super.onLoad();
if (!CC_EDITOR) {
@@ -46,11 +46,11 @@ export class SphereColliderComponent extends ColliderComponentBase {
* The radius of the sphere.
*/
@property
get radius () {
public get radius () {
return this._radius;
}
set radius (value) {
public set radius (value) {
this._radius = value;
if (!CC_EDITOR) {
@@ -34,47 +34,6 @@ export class PhysicsBasedComponent extends Component {
super();
}
public __preload () {
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 (!CC_EDITOR) {
this.sharedBody.enable();
}
}
public onDisable () {
if (!CC_EDITOR) {
this.sharedBody.disable();
}
}
public onDestroy () {
if (!CC_EDITOR) {
this._sharedBody.deref();
(this._sharedBody as any) = null;
}
}
/**
* @return group ∈ [0, 31] (int)
*/
@@ -123,6 +82,49 @@ export class PhysicsBasedComponent extends Component {
return this._body!.addMask(v);
}
}
/// COMPONENT LIFECYCLE ///
protected __preload () {
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;
}
}
protected onEnable () {
if (!CC_EDITOR) {
this.sharedBody.enable();
}
}
protected onDisable () {
if (!CC_EDITOR) {
this.sharedBody.disable();
}
}
protected onDestroy () {
if (!CC_EDITOR) {
this._sharedBody.deref();
(this._sharedBody as any) = null;
}
}
}
class SharedRigidBody {
@@ -163,8 +165,6 @@ class SharedRigidBody {
private _afterStepCallback!: AfterStepCallback;
private _onCollidedCallback!: ICollisionCallback;
private _transformInitialized: boolean = false;
/** 是否只有Collider组件 */
@@ -182,7 +182,6 @@ class SharedRigidBody {
this._body.setUserData(this._node);
this._beforeStepCallback = this._beforeStep.bind(this);
this._afterStepCallback = this._afterStep.bind(this);
this._onCollidedCallback = this._onCollided.bind(this);
}
public ref () {
@@ -209,7 +208,6 @@ class SharedRigidBody {
(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;
}
@@ -251,7 +249,6 @@ class SharedRigidBody {
this._actived = true;
this._body.setWorld(this._world);
this._body.addCollisionCallback(this._onCollidedCallback);
this._world.addBeforeStep(this._beforeStepCallback);
this._world.addAfterStep(this._afterStepCallback);
this._body.wakeUp();
@@ -264,21 +261,10 @@ 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);
}
private _onCollided (type: ICollisionEventType, event: ICollisionEvent) {
if (!this._node) {
return;
}
this._node.emit(type, {
source: event.source.getUserData() as Node,
target: event.target.getUserData() as Node,
});
}
private _beforeStep () {
// 开始物理计算之前,用户脚本或引擎功能有可能改变节点的Transform,所以需要判断并进行更新
@@ -25,60 +25,6 @@ const NonRigidBodyProperties = {
@executeInEditMode
export class RigidBodyComponent extends PhysicsBasedComponent {
/// PRIVATE PROPERTY ///
@property
private _material: PhysicsMaterial | null = null;
@property
private _mass: number = NonRigidBodyProperties.mass;
@property
private _linearDamping: number = NonRigidBodyProperties.linearDamping;
@property
private _angularDamping: number = NonRigidBodyProperties.angularDamping;
@property
private _fixedRotation: boolean = false;
@property
private _isTrigger: boolean = false;
@property
private _isKinematic: boolean = false;
@property
private _useGravity: boolean = true;
private _velocity: Vec3 = new Vec3();
constructor () {
super();
}
/// COMPONENT LIFECYCLE ///
public onLoad () {
if (!CC_PHYSICS_BUILT_IN) {
/**
* 此处设置刚体属性是因为__preload不受executionOrder的顺序影响,
* 从而导致ColliderComponent后添加会导致刚体的某些属性被重写
*/
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.fixedRotation = this._fixedRotation;
}
}
}
/// PUBLIC PROPERTY GETTER\SETTER ///
// Shielding physics material for alpha version
@@ -204,6 +150,38 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
}
}
/// PRIVATE PROPERTY ///
@property
private _material: PhysicsMaterial | null = null;
@property
private _mass: number = NonRigidBodyProperties.mass;
@property
private _linearDamping: number = NonRigidBodyProperties.linearDamping;
@property
private _angularDamping: number = NonRigidBodyProperties.angularDamping;
@property
private _fixedRotation: boolean = false;
@property
private _isTrigger: boolean = false;
@property
private _isKinematic: boolean = false;
@property
private _useGravity: boolean = true;
private _velocity: Vec3 = new Vec3();
constructor () {
super();
}
/// PUBLIC METHOD ///
public applyForce (force: Vec3, position?: Vec3) {
@@ -235,4 +213,26 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
this._body!.setCollisionFilter(group, mask);
}
}
/// COMPONENT LIFECYCLE ///
protected onLoad () {
if (!CC_PHYSICS_BUILT_IN) {
/**
* 此处设置刚体属性是因为__preload不受executionOrder的顺序影响,
* 从而导致ColliderComponent后添加会导致刚体的某些属性被重写
*/
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.fixedRotation = this._fixedRotation;
}
}
}
}
+3
View File
@@ -42,3 +42,6 @@ export {
cc.geometry = geometry;
cc.primitives = primitives;
cc.utils = utils;
/// PHYSICS EXPORT ///
export * from './physics/export-api';
+1
View File
@@ -1,5 +1,6 @@
import { Quat, Vec3 } from '../../core/value-types';
import { mat4, quat } from '../../core/vmath';
import { Node } from '../../scene-graph/node';
import { ERigidBodyType } from './physic-enum';
import { RaycastResult } from './raycast-result';
+10
View File
@@ -0,0 +1,10 @@
import { Node } from '../../scene-graph/node';
export interface ICollisionEvent {
source: Node;
target: Node;
}
export type CollisionEventType = 'onCollisionEnter' | 'onCollisionStay' | 'onCollisionExit';
export type CollisionCallback = (type: CollisionEventType, event: ICollisionEvent) => void;
+1 -1
View File
@@ -8,7 +8,7 @@ export function createPhysicsWorld (): PhysicsWorldBase {
}
export function createRigidBody (options?: ICreateBodyOptions): RigidBodyBase {
return new RigidBody(options);
return new RigidBody(options) as RigidBodyBase;
}
export function createBoxShape (size: Vec3): BoxShapeBase {