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

@@ -385,7 +385,10 @@ interface ITriggerListener {
declare class Mover extends Component {
private _triggerHelper;
onAddedToEntity(): void;
calculateMovement(motion: Vector2): CollisionResult;
calculateMovement(motion: Vector2): {
collisionResult: CollisionResult;
motion: Vector2;
};
applyMovement(motion: Vector2): void;
move(motion: Vector2): CollisionResult;
}
@@ -839,8 +842,14 @@ declare class Physics {
static reset(): void;
static clear(): void;
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
static boxcastBroadphase(rect: Rectangle, layerMask?: number): Collider[];
static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): Collider[];
static boxcastBroadphase(rect: Rectangle, layerMask?: number): {
colliders: Collider[];
rect: Rectangle;
};
static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): {
tempHashSet: Collider[];
bounds: Rectangle;
};
static addCollider(collider: Collider): void;
static removeCollider(collider: Collider): void;
static updateCollider(collider: Collider): void;
@@ -885,6 +894,7 @@ declare class Box extends Polygon {
height: number;
constructor(width: number, height: number);
private static buildBox;
overlaps(other: Shape): any;
collidesWithShape(other: Shape): any;
updateBox(width: number, height: number): void;
containsPoint(point: Vector2): boolean;
@@ -934,7 +944,10 @@ declare class SpatialHash {
register(collider: Collider): void;
clear(): void;
overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask: any): number;
aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): Collider[];
aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): {
tempHashSet: Collider[];
bounds: Rectangle;
};
private cellAtPosition;
private cellCoords;
}

View File

@@ -1799,7 +1799,9 @@ var Mover = (function (_super) {
var bounds = collider.bounds;
bounds.x += motion.x;
bounds.y += motion.y;
var neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
var boxcastResult = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
bounds = boxcastResult.bounds;
var neighbors = boxcastResult.tempHashSet;
for (var j = 0; j < neighbors.length; j++) {
var neighbor = neighbors[j];
if (neighbor.isTrigger)
@@ -1814,7 +1816,7 @@ var Mover = (function (_super) {
}
}
ListPool.free(colliders);
return collisionResult;
return { collisionResult: collisionResult, motion: motion };
};
Mover.prototype.applyMovement = function (motion) {
this.entity.position = Vector2.add(this.entity.position, motion);
@@ -1822,7 +1824,9 @@ var Mover = (function (_super) {
this._triggerHelper.update();
};
Mover.prototype.move = function (motion) {
var collisionResult = this.calculateMovement(motion);
var movementResult = this.calculateMovement(motion);
var collisionResult = movementResult.collisionResult;
motion = movementResult.motion;
this.applyMovement(motion);
return collisionResult;
};
@@ -3679,7 +3683,9 @@ var ColliderTriggerHelper = (function () {
var colliders = this._entity.getComponents(Collider);
for (var i = 0; i < colliders.length; i++) {
var collider = colliders[i];
var neighbors = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
var boxcastResult = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
collider.bounds = boxcastResult.rect;
var neighbors = boxcastResult.colliders;
var _loop_5 = function (j) {
var neighbor = neighbors[j];
if (!collider.isTrigger && !neighbor.isTrigger)
@@ -3899,7 +3905,8 @@ var Physics = (function () {
};
Physics.boxcastBroadphase = function (rect, layerMask) {
if (layerMask === void 0) { layerMask = this.allLayers; }
return this._spatialHash.aabbBroadphase(rect, null, layerMask);
var boxcastResult = this._spatialHash.aabbBroadphase(rect, null, layerMask);
return { colliders: boxcastResult.tempHashSet, rect: boxcastResult.bounds };
};
Physics.boxcastBroadphaseExcludingSelf = function (collider, rect, layerMask) {
if (layerMask === void 0) { layerMask = this.allLayers; }
@@ -4104,6 +4111,15 @@ var Box = (function (_super) {
verts[3] = new Vector2(-halfWidth, halfHeight);
return verts;
};
Box.prototype.overlaps = function (other) {
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.prototype.overlaps.call(this, other);
};
Box.prototype.collidesWithShape = function (other) {
if (this.isUnrotated && other instanceof Box && other.isUnrotated) {
return ShapeCollisions.boxToBox(this, other);
@@ -4367,8 +4383,9 @@ var ShapeCollisions = (function () {
return false;
result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y);
result.normal.normalize();
return result;
}
return result;
return null;
};
ShapeCollisions.minkowskiDifference = function (first, second) {
var positionOffset = Vector2.subtract(first.position, Vector2.add(first.bounds.location, Vector2.divide(first.bounds.size, new Vector2(2))));
@@ -4428,7 +4445,9 @@ var SpatialHash = (function () {
this._overlapTestCircle.radius = radius;
this._overlapTestCircle.position = circleCenter;
var resultCounter = 0;
var potentials = this.aabbBroadphase(bounds, null, layerMask);
var aabbBroadphaseResult = this.aabbBroadphase(bounds, null, layerMask);
bounds = aabbBroadphaseResult.bounds;
var potentials = aabbBroadphaseResult.tempHashSet;
for (var i = 0; i < potentials.length; i++) {
var collider = potentials[i];
if (collider instanceof BoxCollider) {
@@ -4463,7 +4482,7 @@ var SpatialHash = (function () {
}
}
}
return this._tempHashSet;
return { tempHashSet: this._tempHashSet, bounds: bounds };
};
SpatialHash.prototype.cellAtPosition = function (x, y, createCellIfEmpty) {
if (createCellIfEmpty === void 0) { createCellIfEmpty = false; }

File diff suppressed because one or more lines are too long