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

201 lines
7.4 KiB
TypeScript
Raw Normal View History

///<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;
/** 用于在全局范围内存储重力值的方便字段 */
2021-05-27 18:32:38 +08:00
public static gravity = new Vector2(0, -300);
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;
2021-06-11 16:20:01 +08:00
public static debugRender: boolean = false;
/**
* raycast发生时分配它
*/
public static _hitArray: RaycastHit[] = [
new RaycastHit()
];
/**
*
*/
public static _colliderArray: Collider[] = [
null
];
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);
this._hitArray[0].reset();
this._colliderArray[0] = null;
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();
}
2021-05-27 18:32:38 +08:00
public static debugDraw(secondsToDisplay) {
2021-06-11 16:20:01 +08:00
if (this.debugRender)
this._spatialHash.debugDraw(secondsToDisplay);
2021-05-27 18:32:38 +08:00
}
/**
*
* @param center
* @param radius
* @param layerMask
*/
public static overlapCircle(center: Vector2, radius: number, layerMask: number = Physics.allLayers) {
this._colliderArray[0] = null;
this._spatialHash.overlapCircle(center, radius, this._colliderArray, layerMask);
return this._colliderArray[0];
}
/**
*
* @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) {
console.warn("传入了一个空的结果数组。不会返回任何结果");
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
/**
* boundsbroadphase检查!
* @param rect
* @param layerMask
*/
2020-07-28 16:25:20 +08:00
public static boxcastBroadphase(rect: Rectangle, layerMask: number = this.allLayers) {
return this._spatialHash.aabbBroadphase(rect, null, layerMask);
2020-07-23 11:00:46 +08:00
}
2020-06-15 10:42:06 +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);
}
/**
* collider.bounds (self)
* @param collider
* @param layerMask
*/
public static boxcastBroadphaseExcludingSelfNonRect(collider: Collider, layerMask = this.allLayers) {
let bounds = collider.bounds;
return this._spatialHash.aabbBroadphase(bounds, collider, layerMask);
}
/**
* collider.bounds deltaX/deltaY self
* @param collider
* @param deltaX
* @param deltaY
* @param layerMask
*/
public static boxcastBroadphaseExcludingSelfDelta(collider: Collider, deltaX: number, deltaY: number, layerMask: number = Physics.allLayers) {
let colliderBounds = collider.bounds;
let sweptBounds = colliderBounds.getSweptBroadphaseBounds(deltaX, deltaY);
return this._spatialHash.aabbBroadphase(sweptBounds, collider, layerMask);
}
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-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-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
/**
* 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);
}
/**
*
* @param rect
* @param layerMask
*/
public static overlapRectangle(rect: Rectangle, layerMask: number = Physics.allLayers) {
this._colliderArray[0] = null;
this._spatialHash.overlapRectangle(rect, this._colliderArray, layerMask);
return this._colliderArray[0];
}
/**
*
* @param rect
* @param results
* @param layerMask
*/
public static overlapRectangleAll(rect: Rectangle, results: Collider[], layerMask: number = Physics.allLayers) {
if (results.length == 0){
console.warn("传入了一个空的结果数组。不会返回任何结果");
return 0;
}
return this._spatialHash.overlapRectangle(rect, results, layerMask);
}
2020-07-09 16:16:04 +08:00
}
2020-07-23 11:00:46 +08:00
}