新增 collidesWithAnyNonMotion

This commit is contained in:
yhh
2021-04-14 10:51:18 +08:00
parent 5e15543bab
commit a28fb72e36
4 changed files with 136 additions and 54 deletions

View File

@@ -1213,11 +1213,18 @@ declare module es {
* @param result * @param result
*/ */
collidesWithNonMotion(collider: Collider, result?: CollisionResult): boolean; collidesWithNonMotion(collider: Collider, result?: CollisionResult): boolean;
/**
* 检查此碰撞器是否已应用运动(增量运动矢量)与任何碰撞器发生碰撞。
* 如果是这样则将返回true并且将使用碰撞数据填充结果。 运动将设置为碰撞器在碰撞之前可以行进的最大距离。
* @param motion
* @param result
*/
collidesWithAny(motion: Vector2, result: CollisionResult): boolean;
/** /**
* 检查此碰撞器是否与场景中的其他碰撞器碰撞。它相交的第一个碰撞器将在碰撞结果中返回碰撞数据。 * 检查此碰撞器是否与场景中的其他碰撞器碰撞。它相交的第一个碰撞器将在碰撞结果中返回碰撞数据。
* @param result * @param result
*/ */
collidesWithAny(result?: CollisionResult): boolean; collidesWithAnyNonMotion(result?: CollisionResult): boolean;
} }
} }
declare module es { declare module es {

View File

@@ -2730,21 +2730,32 @@ var es;
return false; return false;
}; };
/** /**
* 检查此碰撞器是否与场景中的其他碰撞器碰撞。它相交的第一个碰撞器将在碰撞结果中返回碰撞数据 * 检查此碰撞器是否已应用运动(增量运动矢量)与任何碰撞器发生碰撞
* 如果是这样则将返回true并且将使用碰撞数据填充结果。 运动将设置为碰撞器在碰撞之前可以行进的最大距离。
* @param motion
* @param result * @param result
*/ */
Collider.prototype.collidesWithAny = function (result) { Collider.prototype.collidesWithAny = function (motion, result) {
if (result === void 0) { result = new es.CollisionResult(); }
var e_5, _a; var e_5, _a;
// 在我们的新位置上获取我们可能会碰到的任何东西 // 在我们的新位置上获取我们可能会碰到的任何东西
var neighbors = es.Physics.boxcastBroadphaseExcludingSelfNonRect(this, this.collidesWithLayers.value); var colliderBounds = this.bounds.clone();
colliderBounds.x += motion.x;
colliderBounds.y += motion.y;
var neighbors = es.Physics.boxcastBroadphaseExcludingSelf(this, colliderBounds, this.collidesWithLayers.value);
// 更改形状位置,使其处于移动后的位置,以便我们检查是否有重叠
var oldPosition = this.shape.position.clone();
this.shape.position = es.Vector2.add(this.shape.position, motion);
var didCollide = false;
try { try {
for (var neighbors_3 = __values(neighbors), neighbors_3_1 = neighbors_3.next(); !neighbors_3_1.done; neighbors_3_1 = neighbors_3.next()) { for (var neighbors_3 = __values(neighbors), neighbors_3_1 = neighbors_3.next(); !neighbors_3_1.done; neighbors_3_1 = neighbors_3.next()) {
var neighbor = neighbors_3_1.value; var neighbor = neighbors_3_1.value;
if (neighbor.isTrigger) if (neighbor.isTrigger)
continue; continue;
if (this.collidesWithNonMotion(neighbor, result)) if (this.collidesWithNonMotion(neighbor, result)) {
return true; motion = es.Vector2.subtract(motion, result.minimumTranslationVector);
this.shape.position = es.Vector2.subtract(this.shape.position, result.minimumTranslationVector);
didCollide = true;
}
} }
} }
catch (e_5_1) { e_5 = { error: e_5_1 }; } catch (e_5_1) { e_5 = { error: e_5_1 }; }
@@ -2754,6 +2765,35 @@ var es;
} }
finally { if (e_5) throw e_5.error; } finally { if (e_5) throw e_5.error; }
} }
// 将形状位置返回到检查之前的位置
this.shape.position = oldPosition;
return didCollide;
};
/**
* 检查此碰撞器是否与场景中的其他碰撞器碰撞。它相交的第一个碰撞器将在碰撞结果中返回碰撞数据。
* @param result
*/
Collider.prototype.collidesWithAnyNonMotion = function (result) {
if (result === void 0) { result = new es.CollisionResult(); }
var e_6, _a;
// 在我们的新位置上获取我们可能会碰到的任何东西
var neighbors = es.Physics.boxcastBroadphaseExcludingSelfNonRect(this, this.collidesWithLayers.value);
try {
for (var neighbors_4 = __values(neighbors), neighbors_4_1 = neighbors_4.next(); !neighbors_4_1.done; neighbors_4_1 = neighbors_4.next()) {
var neighbor = neighbors_4_1.value;
if (neighbor.isTrigger)
continue;
if (this.collidesWithNonMotion(neighbor, result))
return true;
}
}
catch (e_6_1) { e_6 = { error: e_6_1 }; }
finally {
try {
if (neighbors_4_1 && !neighbors_4_1.done && (_a = neighbors_4.return)) _a.call(neighbors_4);
}
finally { if (e_6) throw e_6.error; }
}
return false; return false;
}; };
return Collider; return Collider;
@@ -3917,7 +3957,7 @@ var es;
this._componentsToRemove.length = 0; this._componentsToRemove.length = 0;
}; };
ComponentList.prototype.deregisterAllComponents = function () { ComponentList.prototype.deregisterAllComponents = function () {
var e_6, _a; var e_7, _a;
try { try {
for (var _b = __values(this._components), _c = _b.next(); !_c.done; _c = _b.next()) { for (var _b = __values(this._components), _c = _b.next(); !_c.done; _c = _b.next()) {
var component = _c.value; var component = _c.value;
@@ -3930,16 +3970,16 @@ var es;
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
} }
} }
catch (e_6_1) { e_6 = { error: e_6_1 }; } catch (e_7_1) { e_7 = { error: e_7_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_6) throw e_6.error; } finally { if (e_7) throw e_7.error; }
} }
}; };
ComponentList.prototype.registerAllComponents = function () { ComponentList.prototype.registerAllComponents = function () {
var e_7, _a; var e_8, _a;
try { try {
for (var _b = __values(this._components), _c = _b.next(); !_c.done; _c = _b.next()) { for (var _b = __values(this._components), _c = _b.next(); !_c.done; _c = _b.next()) {
var component = _c.value; var component = _c.value;
@@ -3949,12 +3989,12 @@ var es;
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
} }
} }
catch (e_7_1) { e_7 = { error: e_7_1 }; } catch (e_8_1) { e_8 = { error: e_8_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_7) throw e_7.error; } finally { if (e_8) throw e_8.error; }
} }
}; };
/** /**
@@ -4013,7 +4053,7 @@ var es;
* @param onlyReturnInitializedComponents * @param onlyReturnInitializedComponents
*/ */
ComponentList.prototype.getComponent = function (type, onlyReturnInitializedComponents) { ComponentList.prototype.getComponent = function (type, onlyReturnInitializedComponents) {
var e_8, _a, e_9, _b; var e_9, _a, e_10, _b;
try { try {
for (var _c = __values(this._components), _d = _c.next(); !_d.done; _d = _c.next()) { for (var _c = __values(this._components), _d = _c.next(); !_d.done; _d = _c.next()) {
var component = _d.value; var component = _d.value;
@@ -4021,12 +4061,12 @@ var es;
return component; return component;
} }
} }
catch (e_8_1) { e_8 = { error: e_8_1 }; } catch (e_9_1) { e_9 = { error: e_9_1 }; }
finally { finally {
try { try {
if (_d && !_d.done && (_a = _c.return)) _a.call(_c); if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
} }
finally { if (e_8) throw e_8.error; } finally { if (e_9) throw e_9.error; }
} }
// 我们可以选择检查挂起的组件以防addComponent和getComponent在同一个框架中被调用 // 我们可以选择检查挂起的组件以防addComponent和getComponent在同一个框架中被调用
if (!onlyReturnInitializedComponents) { if (!onlyReturnInitializedComponents) {
@@ -4037,12 +4077,12 @@ var es;
return component; return component;
} }
} }
catch (e_9_1) { e_9 = { error: e_9_1 }; } catch (e_10_1) { e_10 = { error: e_10_1 }; }
finally { finally {
try { try {
if (_f && !_f.done && (_b = _e.return)) _b.call(_e); if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
} }
finally { if (e_9) throw e_9.error; } finally { if (e_10) throw e_10.error; }
} }
} }
return null; return null;
@@ -4053,7 +4093,7 @@ var es;
* @param components * @param components
*/ */
ComponentList.prototype.getComponents = function (typeName, components) { ComponentList.prototype.getComponents = function (typeName, components) {
var e_10, _a, e_11, _b; var e_11, _a, e_12, _b;
if (!components) if (!components)
components = []; components = [];
try { try {
@@ -4064,12 +4104,12 @@ var es;
} }
} }
} }
catch (e_10_1) { e_10 = { error: e_10_1 }; } catch (e_11_1) { e_11 = { error: e_11_1 }; }
finally { finally {
try { try {
if (_d && !_d.done && (_a = _c.return)) _a.call(_c); if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
} }
finally { if (e_10) throw e_10.error; } finally { if (e_11) throw e_11.error; }
} }
try { try {
// 我们还检查了待处理的组件以防在同一帧中调用addComponent和getComponent // 我们还检查了待处理的组件以防在同一帧中调用addComponent和getComponent
@@ -4080,12 +4120,12 @@ var es;
} }
} }
} }
catch (e_11_1) { e_11 = { error: e_11_1 }; } catch (e_12_1) { e_12 = { error: e_12_1 }; }
finally { finally {
try { try {
if (_f && !_f.done && (_b = _e.return)) _b.call(_e); if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
} }
finally { if (e_11) throw e_11.error; } finally { if (e_12) throw e_12.error; }
} }
return components; return components;
}; };
@@ -4254,7 +4294,7 @@ var es;
list.delete(entity); list.delete(entity);
}; };
EntityList.prototype.update = function () { EntityList.prototype.update = function () {
var e_12, _a; var e_13, _a;
try { try {
for (var _b = __values(this._entities), _c = _b.next(); !_c.done; _c = _b.next()) { for (var _b = __values(this._entities), _c = _b.next(); !_c.done; _c = _b.next()) {
var entity = _c.value; var entity = _c.value;
@@ -4262,12 +4302,12 @@ var es;
entity.update(); entity.update();
} }
} }
catch (e_12_1) { e_12 = { error: e_12_1 }; } catch (e_13_1) { e_13 = { error: e_13_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_12) throw e_12.error; } finally { if (e_13) throw e_13.error; }
} }
}; };
EntityList.prototype.updateLists = function () { EntityList.prototype.updateLists = function () {
@@ -4324,7 +4364,7 @@ var es;
* @param tag * @param tag
*/ */
EntityList.prototype.entitiesWithTag = function (tag) { EntityList.prototype.entitiesWithTag = function (tag) {
var e_13, _a; var e_14, _a;
var list = this.getTagList(tag); var list = this.getTagList(tag);
var returnList = es.ListPool.obtain(); var returnList = es.ListPool.obtain();
try { try {
@@ -4333,12 +4373,12 @@ var es;
returnList.push(entity); returnList.push(entity);
} }
} }
catch (e_13_1) { e_13 = { error: e_13_1 }; } catch (e_14_1) { e_14 = { error: e_14_1 }; }
finally { finally {
try { try {
if (list_1_1 && !list_1_1.done && (_a = list_1.return)) _a.call(list_1); if (list_1_1 && !list_1_1.done && (_a = list_1.return)) _a.call(list_1);
} }
finally { if (e_13) throw e_13.error; } finally { if (e_14) throw e_14.error; }
} }
return returnList; return returnList;
}; };
@@ -4348,7 +4388,7 @@ var es;
* @returns * @returns
*/ */
EntityList.prototype.entityWithTag = function (tag) { EntityList.prototype.entityWithTag = function (tag) {
var e_14, _a; var e_15, _a;
var list = this.getTagList(tag); var list = this.getTagList(tag);
try { try {
for (var list_2 = __values(list), list_2_1 = list_2.next(); !list_2_1.done; list_2_1 = list_2.next()) { for (var list_2 = __values(list), list_2_1 = list_2.next(); !list_2_1.done; list_2_1 = list_2.next()) {
@@ -4356,12 +4396,12 @@ var es;
return entity; return entity;
} }
} }
catch (e_14_1) { e_14 = { error: e_14_1 }; } catch (e_15_1) { e_15 = { error: e_15_1 }; }
finally { finally {
try { try {
if (list_2_1 && !list_2_1.done && (_a = list_2.return)) _a.call(list_2); if (list_2_1 && !list_2_1.done && (_a = list_2.return)) _a.call(list_2);
} }
finally { if (e_14) throw e_14.error; } finally { if (e_15) throw e_15.error; }
} }
return null; return null;
}; };
@@ -4434,7 +4474,7 @@ var es;
for (var _i = 0; _i < arguments.length; _i++) { for (var _i = 0; _i < arguments.length; _i++) {
types[_i] = arguments[_i]; types[_i] = arguments[_i];
} }
var e_15, _a, e_16, _b; var e_16, _a, e_17, _b;
var entities = []; var entities = [];
for (var i = 0; i < this._entities.length; i++) { for (var i = 0; i < this._entities.length; i++) {
if (this._entities[i].enabled) { if (this._entities[i].enabled) {
@@ -4449,12 +4489,12 @@ var es;
} }
} }
} }
catch (e_15_1) { e_15 = { error: e_15_1 }; } catch (e_16_1) { e_16 = { error: e_16_1 }; }
finally { finally {
try { try {
if (types_1_1 && !types_1_1.done && (_a = types_1.return)) _a.call(types_1); if (types_1_1 && !types_1_1.done && (_a = types_1.return)) _a.call(types_1);
} }
finally { if (e_15) throw e_15.error; } finally { if (e_16) throw e_16.error; }
} }
if (meet) { if (meet) {
entities.push(this._entities[i]); entities.push(this._entities[i]);
@@ -4475,12 +4515,12 @@ var es;
} }
} }
} }
catch (e_16_1) { e_16 = { error: e_16_1 }; } catch (e_17_1) { e_17 = { error: e_17_1 }; }
finally { finally {
try { try {
if (types_2_1 && !types_2_1.done && (_b = types_2.return)) _b.call(types_2); if (types_2_1 && !types_2_1.done && (_b = types_2.return)) _b.call(types_2);
} }
finally { if (e_16) throw e_16.error; } finally { if (e_17) throw e_17.error; }
} }
if (meet) { if (meet) {
entities.push(entity); entities.push(entity);
@@ -7398,7 +7438,7 @@ var es;
* @param layerMask * @param layerMask
*/ */
SpatialHash.prototype.overlapRectangle = function (rect, results, layerMask) { SpatialHash.prototype.overlapRectangle = function (rect, results, layerMask) {
var e_17, _a; var e_18, _a;
this._overlapTestBox.updateBox(rect.width, rect.height); this._overlapTestBox.updateBox(rect.width, rect.height);
this._overlapTestBox.position = rect.location; this._overlapTestBox.position = rect.location;
var resultCounter = 0; var resultCounter = 0;
@@ -7429,12 +7469,12 @@ var es;
return resultCounter; return resultCounter;
} }
} }
catch (e_17_1) { e_17 = { error: e_17_1 }; } catch (e_18_1) { e_18 = { error: e_18_1 }; }
finally { finally {
try { try {
if (potentials_1_1 && !potentials_1_1.done && (_a = potentials_1.return)) _a.call(potentials_1); if (potentials_1_1 && !potentials_1_1.done && (_a = potentials_1.return)) _a.call(potentials_1);
} }
finally { if (e_17) throw e_17.error; } finally { if (e_18) throw e_18.error; }
} }
return resultCounter; return resultCounter;
}; };
@@ -7446,7 +7486,7 @@ var es;
* @param layerMask * @param layerMask
*/ */
SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) { SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
var e_18, _a; var e_19, _a;
var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2); var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius; this._overlapTestCircle.radius = radius;
this._overlapTestCircle.position = circleCenter; this._overlapTestCircle.position = circleCenter;
@@ -7479,12 +7519,12 @@ var es;
return resultCounter; return resultCounter;
} }
} }
catch (e_18_1) { e_18 = { error: e_18_1 }; } catch (e_19_1) { e_19 = { error: e_19_1 }; }
finally { finally {
try { try {
if (potentials_2_1 && !potentials_2_1.done && (_a = potentials_2.return)) _a.call(potentials_2); if (potentials_2_1 && !potentials_2_1.done && (_a = potentials_2.return)) _a.call(potentials_2);
} }
finally { if (e_18) throw e_18.error; } finally { if (e_19) throw e_19.error; }
} }
return resultCounter; return resultCounter;
}; };
@@ -11631,7 +11671,7 @@ var es;
* 创建一个Set从一个Enumerable.List< T>。 * 创建一个Set从一个Enumerable.List< T>。
*/ */
List.prototype.toSet = function () { List.prototype.toSet = function () {
var e_19, _a; var e_20, _a;
var result = new Set(); var result = new Set();
try { try {
for (var _b = __values(this._elements), _c = _b.next(); !_c.done; _c = _b.next()) { for (var _b = __values(this._elements), _c = _b.next(); !_c.done; _c = _b.next()) {
@@ -11639,12 +11679,12 @@ var es;
result.add(x); result.add(x);
} }
} }
catch (e_19_1) { e_19 = { error: e_19_1 }; } catch (e_20_1) { e_20 = { error: e_20_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_19) throw e_19.error; } finally { if (e_20) throw e_20.error; }
} }
return result; return result;
}; };
@@ -11893,7 +11933,7 @@ var es;
* 计算可见性多边形并返回三角形扇形的顶点减去中心顶点。返回的数组来自ListPool * 计算可见性多边形并返回三角形扇形的顶点减去中心顶点。返回的数组来自ListPool
*/ */
VisibilityComputer.prototype.end = function () { VisibilityComputer.prototype.end = function () {
var e_20, _a; var e_21, _a;
var output = es.ListPool.obtain(); var output = es.ListPool.obtain();
this.updateSegments(); this.updateSegments();
this._endPoints.sort(this._radialComparer.compare); this._endPoints.sort(this._radialComparer.compare);
@@ -11932,12 +11972,12 @@ var es;
} }
} }
} }
catch (e_20_1) { e_20 = { error: e_20_1 }; } catch (e_21_1) { e_21 = { error: e_21_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_20) throw e_20.error; } finally { if (e_21) throw e_21.error; }
} }
} }
VisibilityComputer._openSegments.clear(); VisibilityComputer._openSegments.clear();
@@ -12053,7 +12093,7 @@ var es;
* 处理片段,以便我们稍后对它们进行分类 * 处理片段,以便我们稍后对它们进行分类
*/ */
VisibilityComputer.prototype.updateSegments = function () { VisibilityComputer.prototype.updateSegments = function () {
var e_21, _a; var e_22, _a;
try { try {
for (var _b = __values(this._segments), _c = _b.next(); !_c.done; _c = _b.next()) { for (var _b = __values(this._segments), _c = _b.next(); !_c.done; _c = _b.next()) {
var segment = _c.value; var segment = _c.value;
@@ -12071,12 +12111,12 @@ var es;
segment.p2.begin = !segment.p1.begin; segment.p2.begin = !segment.p1.begin;
} }
} }
catch (e_21_1) { e_21 = { error: e_21_1 }; } catch (e_22_1) { e_22 = { error: e_22_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_21) throw e_21.error; } finally { if (e_22) throw e_22.error; }
} }
// 如果我们有一个聚光灯,我们需要存储前两个段的角度。 // 如果我们有一个聚光灯,我们需要存储前两个段的角度。
// 这些是光斑的边界,我们将用它们来过滤它们之外的任何顶点。 // 这些是光斑的边界,我们将用它们来过滤它们之外的任何顶点。

File diff suppressed because one or more lines are too long

View File

@@ -213,11 +213,46 @@ module es {
return false; return false;
} }
/**
* 检查此碰撞器是否已应用运动(增量运动矢量)与任何碰撞器发生碰撞。
* 如果是这样则将返回true并且将使用碰撞数据填充结果。 运动将设置为碰撞器在碰撞之前可以行进的最大距离。
* @param motion
* @param result
*/
public collidesWithAny(motion: Vector2, result: CollisionResult) {
// 在我们的新位置上获取我们可能会碰到的任何东西
let colliderBounds = this.bounds.clone();
colliderBounds.x += motion.x;
colliderBounds.y += motion.y;
let neighbors = Physics.boxcastBroadphaseExcludingSelf(this, colliderBounds, this.collidesWithLayers.value);
// 更改形状位置,使其处于移动后的位置,以便我们检查是否有重叠
let oldPosition = this.shape.position.clone();
this.shape.position = Vector2.add(this.shape.position, motion);
let didCollide = false;
for (let neighbor of neighbors) {
if (neighbor.isTrigger)
continue;
if (this.collidesWithNonMotion(neighbor, result)) {
motion = Vector2.subtract(motion, result.minimumTranslationVector);
this.shape.position = Vector2.subtract(this.shape.position, result.minimumTranslationVector);
didCollide = true;
}
}
// 将形状位置返回到检查之前的位置
this.shape.position = oldPosition;
return didCollide;
}
/** /**
* 检查此碰撞器是否与场景中的其他碰撞器碰撞。它相交的第一个碰撞器将在碰撞结果中返回碰撞数据。 * 检查此碰撞器是否与场景中的其他碰撞器碰撞。它相交的第一个碰撞器将在碰撞结果中返回碰撞数据。
* @param result * @param result
*/ */
public collidesWithAny(result: CollisionResult = new CollisionResult()) { public collidesWithAnyNonMotion(result: CollisionResult = new CollisionResult()) {
// 在我们的新位置上获取我们可能会碰到的任何东西 // 在我们的新位置上获取我们可能会碰到的任何东西
let neighbors = Physics.boxcastBroadphaseExcludingSelfNonRect(this, this.collidesWithLayers.value); let neighbors = Physics.boxcastBroadphaseExcludingSelfNonRect(this, this.collidesWithLayers.value);