mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
Adjust the collision event trigger mechanism of the built-in physical engine; (#4346)
Add interface " onTrigger*** & onCollision*** " to the built-in physical engine; Add util class " ArrayCollisionMatrix "
This commit is contained in:
committed by
Weiliang Wang
parent
be0f644f95
commit
17f37f7c91
@@ -13,7 +13,11 @@ export class BuiltInBody implements RigidBodyBase {
|
||||
set collisionFilterGroup (v: number) { this._collisionFilterGroup = v; }
|
||||
get collisionFilterMask () { return this._collisionFilterMask; }
|
||||
set collisionFilterMask (v: number) { this._collisionFilterMask = v; }
|
||||
|
||||
/** id生成器 */
|
||||
private static idCounter: number = 0;
|
||||
/** 刚体id */
|
||||
private readonly _id: number;
|
||||
public get id () { return this._id; }
|
||||
/** 刚体类型 */
|
||||
private type: ERigidBodyType = ERigidBodyType.DYNAMIC;
|
||||
/** 属于的组 */
|
||||
@@ -29,9 +33,11 @@ export class BuiltInBody implements RigidBodyBase {
|
||||
/** Body对应的场景对象 */
|
||||
private userData: any;
|
||||
|
||||
constructor (options) { }
|
||||
constructor (options) {
|
||||
this._id = BuiltInBody.idCounter++;
|
||||
}
|
||||
|
||||
public intersects (body: BuiltInBody) {
|
||||
public intersects (body: BuiltInBody): boolean {
|
||||
// tslint:disable-next-line: prefer-for-of
|
||||
for (let i = 0; i < this._shapes.length; i++) {
|
||||
const shapeA = this._shapes[i];
|
||||
@@ -39,19 +45,31 @@ export class BuiltInBody implements RigidBodyBase {
|
||||
for (let j = 0; j < body._shapes.length; j++) {
|
||||
const shapeB = body._shapes[j];
|
||||
if (intersect.resolve(shapeA.shape, shapeB.shape)) {
|
||||
const event: ICollisionEvent = {
|
||||
source: this,
|
||||
target: body,
|
||||
};
|
||||
for (const callback of this._collisionCallbacks) {
|
||||
callback(event);
|
||||
}
|
||||
for (const callback of body._collisionCallbacks) {
|
||||
callback(event);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public onCollisionEnter (event: ICollisionEvent) {
|
||||
// TODO : Enter事件
|
||||
}
|
||||
public onCollisionStay (event: ICollisionEvent) {
|
||||
// TODO : Stay事件
|
||||
}
|
||||
public onCollisionExit (event: ICollisionEvent) {
|
||||
// TODO : Exit事件
|
||||
}
|
||||
public onTriggerEnter (event: ICollisionEvent) {
|
||||
for (const callback of this._collisionCallbacks) {
|
||||
callback(event);
|
||||
}
|
||||
}
|
||||
public onTriggerStay (event: ICollisionEvent) {
|
||||
// TODO : Stay事件
|
||||
}
|
||||
public onTriggerExit (event: ICollisionEvent) {
|
||||
// TODO : Exit事件
|
||||
}
|
||||
public getType (): ERigidBodyType {
|
||||
return this.type;
|
||||
|
||||
@@ -3,8 +3,8 @@ import { ShapeBase } from '../api';
|
||||
import { EShapeType } from '../physic-enum';
|
||||
import { BuiltInBody } from './built-in-body';
|
||||
import { IShapeTransform } from './built-in-interface';
|
||||
import { BoxShape } from './built-in-shape/BoxShape';
|
||||
import { SphereShape } from './built-in-shape/SphereShape';
|
||||
import { BoxShape } from './shapes/BoxShape';
|
||||
import { SphereShape } from './shapes/SphereShape';
|
||||
|
||||
export class BuiltInShape implements ShapeBase {
|
||||
get shape () { return this._shape; }
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Vec3 } from '../../../core';
|
||||
import { AfterStepCallback, BeforeStepCallback, IRaycastOptions, PhysicsWorldBase, RigidBodyBase } from '../api';
|
||||
import { AfterStepCallback, BeforeStepCallback, ICollisionEvent, IRaycastOptions, PhysicsWorldBase, RigidBodyBase } from '../api';
|
||||
import { RaycastResult } from '../raycast-result';
|
||||
import { BuiltInBody } from './built-in-body';
|
||||
import { ArrayCollisionMatrix } from './utils/array-collision-matrix';
|
||||
|
||||
/**
|
||||
* Built-in collision system, intended for use as a
|
||||
@@ -12,6 +13,12 @@ export class BuiltInWorld implements PhysicsWorldBase {
|
||||
private _bodies: RigidBodyBase[] = [];
|
||||
private _customBeforeStepListener: BeforeStepCallback[] = [];
|
||||
private _customAfterStepListener: AfterStepCallback[] = [];
|
||||
|
||||
/** 碰撞矩阵 */
|
||||
private _collisionMatrix: ArrayCollisionMatrix = new ArrayCollisionMatrix();
|
||||
/** 上一次的碰撞矩阵 */
|
||||
// private _preCollisionMatrix: ArrayCollisionMatrix = new ArrayCollisionMatrix();
|
||||
|
||||
constructor () {
|
||||
}
|
||||
public step (deltaTime: number): void {
|
||||
@@ -32,7 +39,35 @@ export class BuiltInWorld implements PhysicsWorldBase {
|
||||
continue;
|
||||
}
|
||||
|
||||
bodyA.intersects(bodyB);
|
||||
if (bodyA.intersects(bodyB)) {
|
||||
const event: ICollisionEvent = {
|
||||
source: bodyA,
|
||||
target: bodyB,
|
||||
};
|
||||
if (this._collisionMatrix.get(bodyA.id, bodyB.id)) {
|
||||
// TODO: 触发Stay
|
||||
bodyA.onTriggerStay(event);
|
||||
bodyB.onTriggerStay(event);
|
||||
} else {
|
||||
this._collisionMatrix.set(bodyA.id, bodyB.id, true);
|
||||
/** 是第一次 */ // TODO: 触发Enter
|
||||
bodyA.onTriggerEnter(event);
|
||||
bodyB.onTriggerEnter(event);
|
||||
}
|
||||
} else {
|
||||
if (this._collisionMatrix.get(bodyA.id, bodyB.id)) {
|
||||
// TODO: 触发Exiter
|
||||
const event: ICollisionEvent = {
|
||||
source: bodyA,
|
||||
target: bodyB,
|
||||
};
|
||||
bodyA.onTriggerExit(event);
|
||||
bodyB.onTriggerExit(event);
|
||||
}
|
||||
|
||||
this._collisionMatrix.set(bodyA.id, bodyB.id, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this._customAfterStepListener.forEach((fx) => fx());
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* Collision "matrix". It's actually a triangular-shaped array of whether two bodies are touching this step, for reference next step
|
||||
* @class ArrayCollisionMatrix
|
||||
* @constructor
|
||||
*/
|
||||
export class ArrayCollisionMatrix {
|
||||
|
||||
/**
|
||||
* The matrix storage
|
||||
* @property matrix
|
||||
* @type {Array}
|
||||
*/
|
||||
public matrix: number[] = [];
|
||||
|
||||
/**
|
||||
* Get an element
|
||||
* @method get
|
||||
* @param {Number} i
|
||||
* @param {Number} j
|
||||
* @return {Number}
|
||||
*/
|
||||
public get (i: number, j: number): number {
|
||||
if (j > i) {
|
||||
const temp = j;
|
||||
j = i;
|
||||
i = temp;
|
||||
}
|
||||
return this.matrix[(i * (i + 1) >> 1) + j - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an element
|
||||
* @method set
|
||||
* @param {Number} i
|
||||
* @param {Number} j
|
||||
* @param {boolean} value
|
||||
*/
|
||||
public set (i: number, j: number, value: boolean) {
|
||||
if (j > i) {
|
||||
const temp = j;
|
||||
j = i;
|
||||
i = temp;
|
||||
}
|
||||
this.matrix[(i * (i + 1) >> 1) + j - 1] = value ? 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets all elements to zero
|
||||
* @method reset
|
||||
*/
|
||||
public reset () {
|
||||
for (let i = 0, l = this.matrix.length; i !== l; i++) {
|
||||
this.matrix[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the max number of objects
|
||||
* @method setNumObjects
|
||||
* @param {Number} n
|
||||
*/
|
||||
public setNumObjects (n: number) {
|
||||
this.matrix.length = n * (n - 1) >> 1;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user