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

56 lines
1.8 KiB
TypeScript
Raw Normal View History

class Physics {
private static _spatialHash: SpatialHash;
2020-06-16 11:59:40 +08:00
/** 调用reset并创建一个新的SpatialHash时使用的单元格大小 */
public static spatialHashCellSize = 100;
2020-07-08 18:12:17 +08:00
/** 接受layerMask的所有方法的默认值 */
public static readonly allLayers: number = -1;
2020-06-16 11:59:40 +08:00
public static reset(){
this._spatialHash = new SpatialHash(this.spatialHashCellSize);
}
2020-07-08 18:12:17 +08:00
/**
* SpatialHash中移除所有碰撞器
*/
public static clear(){
this._spatialHash.clear();
}
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
public static boxcastBroadphase(rect: Rectangle, layerMask: number = this.allLayers){
2020-07-07 18:54:19 +08:00
let boxcastResult = this._spatialHash.aabbBroadphase(rect, null, layerMask);
return {colliders: boxcastResult.tempHashSet, rect: boxcastResult.bounds};
2020-06-15 08:46:38 +08:00
}
2020-06-15 10:42:06 +08:00
public static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask = this.allLayers){
return this._spatialHash.aabbBroadphase(rect, collider, layerMask);
}
2020-07-08 18:12:17 +08:00
/**
*
* @param collider
*/
public static addCollider(collider: Collider){
Physics._spatialHash.register(collider);
}
2020-07-08 18:12:17 +08:00
/**
*
* @param collider
*/
public static removeCollider(collider: Collider){
Physics._spatialHash.remove(collider);
}
2020-07-08 18:12:17 +08:00
/**
*
* @param collider
*/
2020-06-15 10:42:06 +08:00
public static updateCollider(collider: Collider){
this._spatialHash.remove(collider);
this._spatialHash.register(collider);
}
}