diff --git a/cocos/3d/framework/physics/detail/physics-based-component.ts b/cocos/3d/framework/physics/detail/physics-based-component.ts index 8d214ff654..b8a1006438 100644 --- a/cocos/3d/framework/physics/detail/physics-based-component.ts +++ b/cocos/3d/framework/physics/detail/physics-based-component.ts @@ -36,7 +36,8 @@ export class PhysicsBasedComponent extends Component { /** * @zh - * 获取所在的组, 返回的是移动的位数,即 “ 1 << v ” 中的 v。 + * 获取分组位, 返回的是移动的位数,即 “ 1 << v ” 中的 v。 + * @note 注:使用此接口表示只在一个组。 * @returns 整数,范围为 0 到 31 */ public getGroup (): number { @@ -48,7 +49,8 @@ export class PhysicsBasedComponent extends Component { /** * @zh - * 设置所在的组,将会进行移位操作,即 1 << v。 + * 设置分组位,将会进行位操作,即 1 << v。 + * @note 注:使用此接口表示只在一个组。 * @param v - 整数,范围为 0 到 31 */ public setGroup (v: number) { @@ -59,19 +61,30 @@ export class PhysicsBasedComponent extends Component { /** * @zh - * 返回检测的掩码,范围为 2 的 0 次方 到 2 的 31 次方。 + * 获取分组值。 + * @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方 + */ + public setCollisionFilterGroup (v: number): void { + if (this._assertPreload) { + return this._body!.setCollisionFilterGroup(v); + } + } + + /** + * @zh + * 设置分组值。 * @returns 整数,范围为 2 的 0 次方 到 2 的 31 次方 */ - public getMask (): number { + public getCollisionFilterGroup (): number { if (this._assertPreload) { - return this._body!.getMask(); + return this._body!.getCollisionFilterGroup(); } return 0; } /** * @zh - * 设置掩码, 将会把 v 位写为 1 ,其它位改为 0。 + * 设置掩码位,将会把掩码值的第 v 位写为 1,其它位写位 0。 * @param v - 整数,范围为 0 到 31 */ public setMask (v: number) { @@ -93,7 +106,41 @@ export class PhysicsBasedComponent extends Component { /** * @zh - * 直接设置碰撞分组和掩码值。 + * 移除掩码位,将会把掩码值的第 v 位写为 0,可填入不需要检查的 group。 + * @param v - 整数,范围为 0 到 31 + */ + public removeMask (v: number) { + if (this._assertPreload) { + return this._body!.removeMask(v); + } + } + + /** + * @zh + * 获取掩码值。 + * @returns 整数,范围为 2 的 0 次方 到 2 的 31 次方 + */ + public getCollisionFilterMask (): number { + if (this._assertPreload) { + return this._body!.getCollisionFilterMask(); + } + return 0; + } + + /** + * @zh + * 设置掩码值。 + * @param v - 整数,范围为 2 的 0 次方 到 2 的 31 次方 + */ + public setCollisionFilterMask (v: number) { + if (this._assertPreload) { + return this._body!.setCollisionFilterMask(v); + } + } + + /** + * @zh + * 直接设置分组值和掩码值。 * @param group - 分组值 * @param mask - 掩码值 */ diff --git a/cocos/3d/physics/api.ts b/cocos/3d/physics/api.ts index 3618bbd7e2..2a6e8dac76 100644 --- a/cocos/3d/physics/api.ts +++ b/cocos/3d/physics/api.ts @@ -62,39 +62,32 @@ export interface PhysicsWorldBase { } export interface BuiltInRigidBodyBase{ - /** - * @return group ∈ [0, 31] (int) - */ + getGroup (): number; - /** - * @param v ∈ [0, 31] (int) - */ setGroup (v: number): void; - /** - * @return (int) - */ - getMask (): number; + setCollisionFilterGroup (group: number): void; + + getCollisionFilterGroup (): number; - /** - * this will reset the mask - * @param v ∈ [0, 31] (int) - */ setMask (v: number): void; - /** - * this will add a mask - * @param v ∈ [0, 31] (int) - */ addMask (v: number): void; - /** - * this will remove a mask - * @param v ∈ [0, 31] (int) - */ removeMask (v: number): void; + getCollisionFilterMask (): number; + + setCollisionFilterMask (v: number): void; + + /** + * Set the collision filter of this body, remember that they are tested bitwise. + * @param group The group which this body will be put into. + * @param mask The groups which this body can collide with. + */ + setCollisionFilter (group: number, mask: number): void; + addShape (shape: ShapeBase, offset?: Vec3): void; removeShape (shape: ShapeBase): void; @@ -119,13 +112,6 @@ export interface BuiltInRigidBodyBase{ setUserData (data: any): void; - /** - * Set the collision filter of this body, remember that they are tested bitwise. - * @param group The group which this body will be put into. - * @param mask The groups which this body can collide with. - */ - setCollisionFilter (group: number, mask: number): void; - setWorld (world: PhysicsWorldBase | null): void; } diff --git a/cocos/3d/physics/cannon-impl.ts b/cocos/3d/physics/cannon-impl.ts index 2443404ca1..4100308295 100644 --- a/cocos/3d/physics/cannon-impl.ts +++ b/cocos/3d/physics/cannon-impl.ts @@ -219,11 +219,15 @@ export class CannonRigidBody implements RigidBodyBase { public setGroup (v: number): void { this._group = clamp(v, 0, 31); - this._body.collisionFilterGroup = 1 << v; + this._body.collisionFilterGroup = 1 << this._group; } - public getMask (): number { - return this._body.collisionFilterMask; + public setCollisionFilterGroup (group: number): void { + this._body.collisionFilterGroup = group; + } + + public getCollisionFilterGroup (): number { + return this._body.collisionFilterGroup; } public setMask (v: number): void { @@ -241,6 +245,14 @@ export class CannonRigidBody implements RigidBodyBase { this._body.collisionFilterMask -= 1 << v; } + public getCollisionFilterMask (): number { + return this._body.collisionFilterMask; + } + + public setCollisionFilterMask (v: number): void { + this._body.collisionFilterMask = v; + } + public wakeUp (): void { return this._body.wakeUp(); } diff --git a/cocos/3d/physics/cocos/builtin-body.ts b/cocos/3d/physics/cocos/builtin-body.ts index 6cdee19509..dc831cbeef 100644 --- a/cocos/3d/physics/cocos/builtin-body.ts +++ b/cocos/3d/physics/cocos/builtin-body.ts @@ -42,20 +42,29 @@ export class BuiltInBody implements BuiltInRigidBodyBase { constructor (options) { this._id = BuiltInBody.idCounter++; } + public getGroup (): number { return this._group; } + public setGroup (v: number): void { this._group = clamp(Math.floor(v), 0, 31); - this._collisionFilterGroup = 1 << v; + this._collisionFilterGroup = 1 << this._group; } - public getMask (): number { - return this._collisionFilterMask; + + public getCollisionFilterGroup (): number { + return this._collisionFilterGroup; } + + public setCollisionFilterGroup (group: number): void { + this._collisionFilterGroup = group; + } + public setMask (v: number): void { v = clamp(Math.floor(v), 0, 31); this._collisionFilterMask = 1 << v; } + public addMask (v: number): void { v = clamp(Math.floor(v), 0, 31); this._collisionFilterMask += 1 << v; @@ -66,6 +75,14 @@ export class BuiltInBody implements BuiltInRigidBodyBase { this._collisionFilterMask -= 1 << v; } + public getCollisionFilterMask (): number { + return this._collisionFilterMask; + } + + public setCollisionFilterMask (v: number): void { + this._collisionFilterMask = v; + } + public intersects (body: BuiltInBody): boolean { // tslint:disable-next-line: prefer-for-of for (let i = 0; i < this._shapes.length; i++) { @@ -80,11 +97,13 @@ export class BuiltInBody implements BuiltInRigidBodyBase { } return false; } + public onCollision (type: ICollisionEventType, event: ICollisionEvent) { for (const callback of this._collisionCB) { callback(type, event); } } + public onTrigger (type: ICollisionEventType, event: ICollisionEvent) { for (const callback of this._collisionCB) { callback(type, event); @@ -94,22 +113,27 @@ export class BuiltInBody implements BuiltInRigidBodyBase { public wakeUp (): void { } + public sleep (): void { } + public addShape (shape: BuiltinShape): void { this._shapes.push(shape); } + public removeShape (shape: BuiltinShape): void { const i = this._shapes.indexOf(shape); if (i >= 0) { this._shapes.splice(i, 1); } } + public setCollisionFilter (group: number, mask: number): void { this._collisionFilterGroup = group; this._collisionFilterMask = mask; } + public setWorld (world: PhysicsWorldBase | null): void { const cworld = world as unknown as (BuiltInWorld | null); @@ -123,41 +147,50 @@ export class BuiltInBody implements BuiltInRigidBodyBase { this._world = cworld; } + public getPosition (out: Vec3): void { } + public setPosition (pos: Vec3): void { /** 更新所有形状的位置 */ for (const shape of this._shapes) { shape.setPosition(pos); } } + public getRotation (out: Quat): void { } + public setRotation (rot: Quat): void { /** 更新所有形状的方向 */ for (const shape of this._shapes) { shape.setRotation(rot); } } + public scaleAllShapes (scale: Vec3): void { for (const shape of this._shapes) { shape.setScale(scale); } } + public addCollisionCallback (callback: ICollisionCallback): void { this._collisionCB.push(callback); } + public removeCollisionCllback (callback: ICollisionCallback): void { const i = this._collisionCB.indexOf(callback); if (i >= 0) { this._collisionCB.splice(i, 1); } } + public getUserData () { return this.userData; } + public setUserData (data: any): void { this.userData = data; }