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

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