box重载overlaps

This commit is contained in:
yhh
2020-07-07 18:54:19 +08:00
parent ace8fb685d
commit b14fee1685
13 changed files with 127 additions and 34 deletions

View File

@@ -1,3 +1,4 @@
/** 移动器使用的帮助器类用于管理触发器碰撞器交互并调用itriggerlistener。 */
class ColliderTriggerHelper {
private _entity: Entity;
/** 存储当前帧中发生的所有活动交集对 */
@@ -18,7 +19,9 @@ class ColliderTriggerHelper {
for (let i = 0; i < colliders.length; i++) {
let collider = colliders[i];
let neighbors = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
let boxcastResult = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
collider.bounds = boxcastResult.rect;
let neighbors = boxcastResult.colliders;
for (let j = 0; j < neighbors.length; j++) {
let neighbor = neighbors[j];
if (!collider.isTrigger && !neighbor.isTrigger)

View File

@@ -18,7 +18,8 @@ class Physics {
}
public static boxcastBroadphase(rect: Rectangle, layerMask: number = this.allLayers){
return this._spatialHash.aabbBroadphase(rect, null, layerMask);
let boxcastResult = this._spatialHash.aabbBroadphase(rect, null, layerMask);
return {colliders: boxcastResult.tempHashSet, rect: boxcastResult.bounds};
}
public static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask = this.allLayers){

View File

@@ -21,6 +21,22 @@ class Box extends Polygon {
return verts;
}
/**
*
* @param other
*/
public overlaps(other: Shape){
if (this.isUnrotated){
if (other instanceof Box && other.isUnrotated)
return this.bounds.intersects(other.bounds);
if (other instanceof Circle)
return Collisions.isRectToCircle(this.bounds, other.position, other.radius);
}
return super.overlaps(other);
}
/**
*
* @param other

View File

@@ -270,9 +270,11 @@ class ShapeCollisions {
result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y);
result.normal.normalize();
return result;
}
return result;
return null;
}
private static minkowskiDifference(first: Box, second: Box){

View File

@@ -63,7 +63,9 @@ class SpatialHash {
this._overlapTestCircle.position = circleCenter;
let resultCounter = 0;
let potentials = this.aabbBroadphase(bounds, null, layerMask);
let aabbBroadphaseResult = this.aabbBroadphase(bounds, null, layerMask);
bounds = aabbBroadphaseResult.bounds;
let potentials = aabbBroadphaseResult.tempHashSet;
for (let i = 0; i < potentials.length; i++) {
let collider = potentials[i];
if (collider instanceof BoxCollider) {
@@ -106,7 +108,7 @@ class SpatialHash {
}
}
return this._tempHashSet;
return {tempHashSet: this._tempHashSet, bounds: bounds};
}
private cellAtPosition(x: number, y: number, createCellIfEmpty: boolean = false) {