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

@@ -43,6 +43,7 @@ class BoxCollider extends Collider {
constructor(){
super();
// 我们在这里插入一个1x1框作为占位符直到碰撞器在下一阵被添加到实体并可以获得更精确的自动调整大小数据
this.shape = new Box(1, 1);
this._colliderRequiresAutoSizing = true;
}

View File

@@ -22,7 +22,9 @@ class Mover extends Component {
let bounds = collider.bounds;
bounds.x += motion.x;
bounds.y += motion.y;
let neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
let boxcastResult = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
bounds = boxcastResult.bounds;
let neighbors = boxcastResult.tempHashSet;
for (let j = 0; j < neighbors.length; j ++){
let neighbor = neighbors[j];
@@ -42,7 +44,7 @@ class Mover extends Component {
ListPool.free(colliders);
return collisionResult;
return {collisionResult: collisionResult, motion: motion};
}
public applyMovement(motion: Vector2){
@@ -53,7 +55,9 @@ class Mover extends Component {
}
public move(motion: Vector2){
let collisionResult = this.calculateMovement(motion);
let movementResult = this.calculateMovement(motion);
let collisionResult = movementResult.collisionResult;
motion = movementResult.motion;
this.applyMovement(motion);

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) {