diff --git a/cocos/3d/framework/physics/collider-component.ts b/cocos/3d/framework/physics/collider-component.ts index c85d86104f..c007a5f033 100644 --- a/cocos/3d/framework/physics/collider-component.ts +++ b/cocos/3d/framework/physics/collider-component.ts @@ -97,6 +97,23 @@ export class ColliderComponentBase extends PhysicsBasedComponent { } super.onDestroy(); } + + public getGroup () { + return this._body!.getGroup(); + } + + public setGroup (v: number) { + return this._body!.setGroup(v); + } + + public getMask () { + return this._body!.getMask(); + } + + public setMask (v: number) { + return this._body!.setMask(v); + } + } @ccclass('cc.BoxColliderComponent') diff --git a/cocos/3d/framework/physics/detail/physics-based-component.ts b/cocos/3d/framework/physics/detail/physics-based-component.ts index 382358b173..48bb3d1c9d 100644 --- a/cocos/3d/framework/physics/detail/physics-based-component.ts +++ b/cocos/3d/framework/physics/detail/physics-based-component.ts @@ -3,6 +3,7 @@ import { ccclass } from '../../../../core/data/class-decorator'; import { Quat, Vec3 } from '../../../../core/value-types'; import { quat, vec3 } from '../../../../core/vmath'; import { Node } from '../../../../scene-graph/node'; +// tslint:disable-next-line:max-line-length import { AfterStepCallback, BeforeStepCallback, ICollisionCallback, ICollisionEvent, ICollisionType, PhysicsWorldBase, RigidBodyBase } from '../../../physics/api'; import { createRigidBody } from '../../../physics/instance'; import { ERigidBodyType, ETransformSource } from '../../../physics/physic-enum'; diff --git a/cocos/3d/physics/api.ts b/cocos/3d/physics/api.ts index 322ee92ca6..e453788be8 100644 --- a/cocos/3d/physics/api.ts +++ b/cocos/3d/physics/api.ts @@ -64,11 +64,20 @@ export interface PhysicsWorldBase { export interface RigidBodyBase { // constructor (options?: ICreateBodyOptions); - /** 获取/设置刚体类型 : ERigidBodyType */ + /** group ∈ [0, 31] */ + getGroup (): number; + /** group ∈ [0, 31] */ + setGroup (v: number): void; + + /** Mask ∈ [0, 31] */ + getMask (): number; + /** Mask ∈ [0, 31] */ + setMask (v: number): void; + + /** the body type */ getType (): ERigidBodyType; setType (v: ERigidBodyType): void; - /** 唤醒/睡眠刚体 */ wakeUp (): void; sleep (): void; diff --git a/cocos/3d/physics/cannon-impl.ts b/cocos/3d/physics/cannon-impl.ts index be6d429a3d..bc4518d50c 100644 --- a/cocos/3d/physics/cannon-impl.ts +++ b/cocos/3d/physics/cannon-impl.ts @@ -1,6 +1,6 @@ import CANNON from 'cannon'; import { Quat, Vec3 } from '../../core/value-types'; -import { quat, vec3 } from '../../core/vmath'; +import { clamp, quat, vec3 } from '../../core/vmath'; import { AfterStepCallback, BeforeStepCallback, BoxShapeBase, @@ -168,6 +168,9 @@ function fillRaycastResult (result: RaycastResult, cannonResult: CANNON.RaycastR export class CannonRigidBody implements RigidBodyBase { + private _group: number = 0; + private _mask: number = 0; + get impl (): CANNON.Body { return this._cannonBody; } @@ -210,6 +213,25 @@ export class CannonRigidBody implements RigidBodyBase { this._onSelfPostStep(); }; } + + public getGroup (): number { + return this._group; + } + + public setGroup (v: number): void { + this._group = clamp(v, 0, 31); + this._cannonBody.collisionFilterGroup = 1 << v; + } + + public getMask (): number { + return this._mask; + } + + public setMask (v: number): void { + this._mask = clamp(v, 0, 31); + this._cannonBody.collisionFilterMask = 1 << v; + } + public wakeUp (): void { return this._cannonBody.wakeUp(); } diff --git a/cocos/3d/physics/cocos/built-in-body.ts b/cocos/3d/physics/cocos/built-in-body.ts index e7fc3dd6c0..ad1661f708 100644 --- a/cocos/3d/physics/cocos/built-in-body.ts +++ b/cocos/3d/physics/cocos/built-in-body.ts @@ -1,4 +1,5 @@ import { Quat, Vec3 } from '../../../core/value-types'; +import { clamp } from '../../../core/vmath'; import { intersect } from '../../geom-utils'; import { ICollisionCallback, ICollisionEvent, ICollisionType as ICollisionEventType, PhysicsWorldBase, RigidBodyBase } from '../api'; import { ERigidBodyType } from '../physic-enum'; @@ -9,28 +10,29 @@ import { BuiltInWorld } from './built-in-world'; * Built-in static collider, no physical forces involved */ export class BuiltInBody implements RigidBodyBase { - get collisionFilterGroup () { return this._collisionFilterGroup; } - set collisionFilterGroup (v: number) { this._collisionFilterGroup = v; } - get collisionFilterMask () { return this._collisionFilterMask; } - set collisionFilterMask (v: number) { this._collisionFilterMask = v; } - /** id生成器 */ + /** id generator */ private static idCounter: number = 0; - /** 刚体id */ - private readonly _id: number; + /** id unique */ public get id () { return this._id; } - /** 刚体类型 */ - private type: ERigidBodyType = ERigidBodyType.DYNAMIC; - /** 属于的组 */ - private _collisionFilterGroup: number = 1; - /** 检测的组 */ - private _collisionFilterMask: number = 1; - /** 碰撞回调 */ - /** Enter */ - private _collisionEnterCB: ICollisionCallback[] = []; - /** Stay */ - private _collisionStayCB: ICollisionCallback[] = []; - /** Exit */ - private _collisionExitCB: ICollisionCallback[] = []; + private readonly _id: number; + + private _type: ERigidBodyType = ERigidBodyType.DYNAMIC; + + private _group: number = 0; + private _mask: number = 0; + + public get collisionFilterGroup () { return this._collisionFilterGroup; } + private _collisionFilterGroup: number = 1 << 0; + + public get collisionFilterMask () { return this._collisionFilterMask; } + private _collisionFilterMask: number = 1 << 0; + + private _collisionCB: ICollisionCallback[] = []; + + // private _collisionEnterCB: ICollisionCallback[] = []; + // private _collisionStayCB: ICollisionCallback[] = []; + // private _collisionExitCB: ICollisionCallback[] = []; + /** 物理世界 */ private _world!: BuiltInWorld | null; /** Body拥有的现状 */ @@ -41,7 +43,20 @@ export class BuiltInBody implements RigidBodyBase { constructor (options) { this._id = BuiltInBody.idCounter++; } - + public getGroup (): number { + return this._group; + } + public setGroup (v: number): void { + this._group = clamp(v, 0, 31); + this._collisionFilterGroup = 1 << v; + } + public getMask (): number { + return this._mask; + } + public setMask (v: number): void { + this._mask = clamp(v, 0, 31); + this._collisionFilterMask = 1 << v; + } public intersects (body: BuiltInBody): boolean { // tslint:disable-next-line: prefer-for-of for (let i = 0; i < this._shapes.length; i++) { @@ -57,20 +72,20 @@ export class BuiltInBody implements RigidBodyBase { return false; } public onCollision (type: ICollisionEventType, event: ICollisionEvent) { - for (const callback of this._collisionStayCB) { + for (const callback of this._collisionCB) { callback(type, event); } } public onTrigger (type: ICollisionEventType, event: ICollisionEvent) { - for (const callback of this._collisionStayCB) { + for (const callback of this._collisionCB) { callback(type, event); } } public getType (): ERigidBodyType { - return this.type; + return this._type; } public setType (v: ERigidBodyType): void { - this.type = v; + this._type = v; } public wakeUp (): void { @@ -185,12 +200,12 @@ export class BuiltInBody implements RigidBodyBase { } } public addCollisionCallback (callback: ICollisionCallback): void { - this._collisionStayCB.push(callback); + this._collisionCB.push(callback); } public removeCollisionCllback (callback: ICollisionCallback): void { - const i = this._collisionStayCB.indexOf(callback); + const i = this._collisionCB.indexOf(callback); if (i >= 0) { - this._collisionStayCB.splice(i, 1); + this._collisionCB.splice(i, 1); } } public getUserData () { diff --git a/cocos/3d/physics/cocos/built-in-world.ts b/cocos/3d/physics/cocos/built-in-world.ts index b0ea67932e..650170b637 100644 --- a/cocos/3d/physics/cocos/built-in-world.ts +++ b/cocos/3d/physics/cocos/built-in-world.ts @@ -22,10 +22,9 @@ export class BuiltInWorld implements PhysicsWorldBase { constructor () { } public step (deltaTime: number): void { - /** 执行 before step */ this._customBeforeStepListener.forEach((fx) => fx()); - /** 进行物理检测 */ + /** collision detection */ // tslint:disable-next-line: prefer-for-of for (let i = 0; i < this._bodies.length; i++) { const bodyA = this._bodies[i] as BuiltInBody; @@ -33,7 +32,7 @@ export class BuiltInWorld implements PhysicsWorldBase { for (let j = i + 1; j < this._bodies.length; j++) { const bodyB = this._bodies[j] as BuiltInBody; - // TODO: 判断是否碰撞 + // first, Check collision filter masks if ((bodyA.collisionFilterGroup & bodyB.collisionFilterMask) === 0 || (bodyB.collisionFilterGroup & bodyA.collisionFilterMask) === 0) { continue;