Files
esengine/source/src/Physics/Physics.ts

67 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-07-23 11:00:46 +08:00
module es {
export class Physics {
private static _spatialHash: SpatialHash;
/** 调用reset并创建一个新的SpatialHash时使用的单元格大小 */
public static spatialHashCellSize = 100;
/** 接受layerMask的所有方法的默认值 */
public static readonly allLayers: number = -1;
2020-07-23 11:00:46 +08:00
public static reset(){
this._spatialHash = new SpatialHash(this.spatialHashCellSize);
}
2020-06-16 11:59:40 +08:00
2020-07-23 11:00:46 +08:00
/**
* SpatialHash中移除所有碰撞器
*/
public static clear(){
this._spatialHash.clear();
}
2020-07-23 11:00:46 +08:00
public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1){
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
}
2020-06-15 08:46:38 +08:00
2020-07-23 11:00:46 +08:00
public static boxcastBroadphase(rect: Rectangle, layerMask: number = this.allLayers){
let boxcastResult = this._spatialHash.aabbBroadphase(rect, null, layerMask);
return {colliders: boxcastResult.tempHashSet, rect: boxcastResult.bounds};
}
2020-06-15 10:42:06 +08:00
2020-07-23 11:00:46 +08:00
public static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask = this.allLayers){
return this._spatialHash.aabbBroadphase(rect, collider, layerMask);
}
2020-07-23 11:00:46 +08:00
/**
*
* @param collider
*/
public static addCollider(collider: Collider){
Physics._spatialHash.register(collider);
}
2020-07-23 11:00:46 +08:00
/**
*
* @param collider
*/
public static removeCollider(collider: Collider){
Physics._spatialHash.remove(collider);
}
2020-07-23 11:00:46 +08:00
/**
*
* @param collider
*/
public static updateCollider(collider: Collider){
this._spatialHash.remove(collider);
this._spatialHash.register(collider);
}
2020-07-09 16:16:04 +08:00
2020-07-23 11:00:46 +08:00
/**
* debug绘制空间散列的内容
* @param secondsToDisplay
*/
public static debugDraw(secondsToDisplay){
this._spatialHash.debugDraw(secondsToDisplay, 2);
}
2020-07-09 16:16:04 +08:00
}
2020-07-23 11:00:46 +08:00
}