2020-08-22 12:21:40 +08:00
|
|
|
|
///<reference path="./RaycastHit.ts" />
|
2020-07-23 11:00:46 +08:00
|
|
|
|
module es {
|
|
|
|
|
|
export class Physics {
|
2020-11-26 17:26:49 +08:00
|
|
|
|
public static _spatialHash: SpatialHash;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/** 调用reset并创建一个新的SpatialHash时使用的单元格大小 */
|
|
|
|
|
|
public static spatialHashCellSize = 100;
|
|
|
|
|
|
/** 接受layerMask的所有方法的默认值 */
|
|
|
|
|
|
public static readonly allLayers: number = -1;
|
2020-07-31 19:33:04 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* raycast是否检测配置为触发器的碰撞器
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static raycastsHitTriggers: boolean = false;
|
2020-08-03 14:45:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 在碰撞器中开始的射线/直线是否强制转换检测到那些碰撞器
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static raycastsStartInColliders = false;
|
2020-08-22 12:21:40 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 我们保留它以避免在每次raycast发生时分配它
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static _hitArray: RaycastHit[] = [
|
|
|
|
|
|
new RaycastHit()
|
|
|
|
|
|
];
|
2020-06-12 20:24:51 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public static reset() {
|
2020-07-23 11:00:46 +08:00
|
|
|
|
this._spatialHash = new SpatialHash(this.spatialHashCellSize);
|
2020-11-26 10:39:32 +08:00
|
|
|
|
this._hitArray[0].reset();
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-16 11:59:40 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 从SpatialHash中移除所有碰撞器
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public static clear() {
|
2020-07-23 11:00:46 +08:00
|
|
|
|
this._spatialHash.clear();
|
|
|
|
|
|
}
|
2020-06-21 10:27:15 +08:00
|
|
|
|
|
2020-07-27 16:10:36 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取位于指定圆内的所有碰撞器
|
|
|
|
|
|
* @param center
|
|
|
|
|
|
* @param randius
|
|
|
|
|
|
* @param results
|
|
|
|
|
|
* @param layerMask
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1) {
|
|
|
|
|
|
if (results.length == 0) {
|
2020-11-30 12:57:53 +08:00
|
|
|
|
console.error("传入了一个空的结果数组。不会返回任何结果");
|
2020-07-27 16:10:36 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
|
|
|
|
|
|
}
|
2020-06-15 08:46:38 +08:00
|
|
|
|
|
2020-07-27 16:10:36 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 返回所有碰撞器与边界相交的碰撞器。bounds。请注意,这是一个broadphase检查,所以它只检查边界,不做单个碰撞到碰撞器的检查!
|
|
|
|
|
|
* @param rect
|
|
|
|
|
|
* @param layerMask
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public static boxcastBroadphase(rect: Rectangle, layerMask: number = this.allLayers) {
|
2020-07-27 16:10:36 +08:00
|
|
|
|
return this._spatialHash.aabbBroadphase(rect, null, layerMask);
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-15 10:42:06 +08:00
|
|
|
|
|
2020-07-27 16:10:36 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 返回所有与边界相交的碰撞器,不包括传入的碰撞器(self)。如果您希望为其他查询自行创建扫过的边界,则此方法非常有用
|
|
|
|
|
|
* @param collider
|
|
|
|
|
|
* @param rect
|
|
|
|
|
|
* @param layerMask
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask = this.allLayers) {
|
2020-07-23 11:00:46 +08:00
|
|
|
|
return this._spatialHash.aabbBroadphase(rect, collider, layerMask);
|
|
|
|
|
|
}
|
2020-06-16 11:22:37 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 将对撞机添加到物理系统中
|
|
|
|
|
|
* @param collider
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public static addCollider(collider: Collider) {
|
2020-07-23 11:00:46 +08:00
|
|
|
|
Physics._spatialHash.register(collider);
|
|
|
|
|
|
}
|
2020-06-15 20:08:21 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 从物理系统中移除对撞机
|
|
|
|
|
|
* @param collider
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public static removeCollider(collider: Collider) {
|
2020-07-23 11:00:46 +08:00
|
|
|
|
Physics._spatialHash.remove(collider);
|
|
|
|
|
|
}
|
2020-06-15 20:08:21 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 更新物理系统中对撞机的位置。这实际上只是移除然后重新添加带有新边界的碰撞器
|
|
|
|
|
|
* @param collider
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public static updateCollider(collider: Collider) {
|
2020-07-23 11:00:46 +08:00
|
|
|
|
this._spatialHash.remove(collider);
|
|
|
|
|
|
this._spatialHash.register(collider);
|
|
|
|
|
|
}
|
2020-07-09 16:16:04 +08:00
|
|
|
|
|
2020-08-22 12:21:40 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 返回与layerMask匹配的碰撞器的第一次命中
|
|
|
|
|
|
* @param start
|
|
|
|
|
|
* @param end
|
|
|
|
|
|
* @param layerMask
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static linecast(start: Vector2, end: Vector2, layerMask: number = Physics.allLayers): RaycastHit{
|
|
|
|
|
|
this._hitArray[0].reset();
|
|
|
|
|
|
this.linecastAll(start, end, this._hitArray, layerMask);
|
|
|
|
|
|
return this._hitArray[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 通过空间散列强制执行一行,并用该行命中的任何碰撞器填充hits数组
|
|
|
|
|
|
* @param start
|
|
|
|
|
|
* @param end
|
|
|
|
|
|
* @param hits
|
|
|
|
|
|
* @param layerMask
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static linecastAll(start: Vector2, end: Vector2, hits: RaycastHit[], layerMask: number = Physics.allLayers){
|
|
|
|
|
|
if (hits.length == 0){
|
|
|
|
|
|
console.warn("传入了一个空的hits数组。没有点击会被返回");
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this._spatialHash.linecast(start, end, hits, layerMask);
|
|
|
|
|
|
}
|
2020-07-09 16:16:04 +08:00
|
|
|
|
}
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|