mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
Add group&mask to rigidbody (#4399)
This commit is contained in:
committed by
Weiliang Wang
parent
b89ddd3eb6
commit
45c1c12ab5
@@ -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')
|
||||
|
||||
@@ -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';
|
||||
|
||||
+11
-2
@@ -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;
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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 () {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user