修复intervalDistance计算错误问题

This commit is contained in:
yhh
2020-12-04 14:42:29 +08:00
parent 9654a4d8c9
commit c2cd3b9c44
5 changed files with 17 additions and 21 deletions

View File

@@ -2083,9 +2083,10 @@ var es;
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
// 第一点和最后一点决不能相同。我们想要一个开放的多边形 // 第一点和最后一点决不能相同。我们想要一个开放的多边形
var isPolygonClosed = points[0] == points[points.length - 1]; var isPolygonClosed = points[0] == points[points.length - 1];
var linqPoints = new linq.List(points);
// 最后一个移除 // 最后一个移除
if (isPolygonClosed) if (isPolygonClosed)
points.splice(points.length - 1, 1); linqPoints.remove(linqPoints.last());
var center = es.Polygon.findPolygonCenter(points); var center = es.Polygon.findPolygonCenter(points);
_this.setLocalOffset(center); _this.setLocalOffset(center);
es.Polygon.recenterPolygonVerts(points); es.Polygon.recenterPolygonVerts(points);
@@ -6234,10 +6235,7 @@ var es;
Polygon.prototype.setPoints = function (points) { Polygon.prototype.setPoints = function (points) {
this.points = points; this.points = points;
this.recalculateCenterAndEdgeNormals(); this.recalculateCenterAndEdgeNormals();
this._originalPoints = []; this._originalPoints = this.points.slice();
for (var i = 0; i < this.points.length; i++) {
this._originalPoints.push(this.points[i]);
}
}; };
/** /**
* 重新计算多边形中心 * 重新计算多边形中心
@@ -6715,8 +6713,8 @@ var es;
*/ */
ShapeCollisions.polygonToPolygon = function (first, second, result) { ShapeCollisions.polygonToPolygon = function (first, second, result) {
var isIntersecting = true; var isIntersecting = true;
var firstEdges = first.edgeNormals; var firstEdges = first.edgeNormals.slice();
var secondEdges = second.edgeNormals; var secondEdges = second.edgeNormals.slice();
var minIntervalDistance = Number.POSITIVE_INFINITY; var minIntervalDistance = Number.POSITIVE_INFINITY;
var translationAxis = new es.Vector2(); var translationAxis = new es.Vector2();
var polygonOffset = es.Vector2.subtract(first.position, second.position); var polygonOffset = es.Vector2.subtract(first.position, second.position);
@@ -6776,7 +6774,7 @@ var es;
ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) { ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) {
if (minA < minB) if (minA < minB)
return minB - maxA; return minB - maxA;
return minA - minB; return minA - maxB;
}; };
/** /**
* 计算一个多边形在一个轴上的投影,并返回一个[minmax]区间 * 计算一个多边形在一个轴上的投影,并返回一个[minmax]区间
@@ -6918,7 +6916,7 @@ var es;
if (poly.containsPoint(point)) { if (poly.containsPoint(point)) {
var distanceSquared = new es.Ref(0); var distanceSquared = new es.Ref(0);
var closestPoint = es.Polygon.getClosestPointOnPolygonToPoint(poly.points, es.Vector2.subtract(point, poly.position), distanceSquared, result.normal); var closestPoint = es.Polygon.getClosestPointOnPolygonToPoint(poly.points, es.Vector2.subtract(point, poly.position), distanceSquared, result.normal);
result.minimumTranslationVector = es.Vector2.multiply(result.normal, new es.Vector2(Math.sqrt(distanceSquared.value), Math.sqrt(distanceSquared.value))); result.minimumTranslationVector = new es.Vector2(result.normal.x * Math.sqrt(distanceSquared.value), result.normal.y * Math.sqrt(distanceSquared.value));
result.point = es.Vector2.add(closestPoint, poly.position); result.point = es.Vector2.add(closestPoint, poly.position);
return true; return true;
} }
@@ -6965,7 +6963,7 @@ var es;
ShapeCollisions.minkowskiDifference = function (first, second) { ShapeCollisions.minkowskiDifference = function (first, second) {
// 我们需要第一个框的左上角 // 我们需要第一个框的左上角
// 碰撞器只会修改运动的位置所以我们需要用位置来计算出运动是什么。 // 碰撞器只会修改运动的位置所以我们需要用位置来计算出运动是什么。
var positionOffset = es.Vector2.subtract(first.position, es.Vector2.add(first.bounds.location, es.Vector2.divide(first.bounds.size, new es.Vector2(2)))); var positionOffset = es.Vector2.subtract(first.position, es.Vector2.add(first.bounds.location, new es.Vector2(first.bounds.size.x / 2, first.bounds.size.y / 2)));
var topLeft = es.Vector2.subtract(es.Vector2.add(first.bounds.location, positionOffset), second.bounds.max); var topLeft = es.Vector2.subtract(es.Vector2.add(first.bounds.location, positionOffset), second.bounds.max);
var fullSize = es.Vector2.add(first.bounds.size, second.bounds.size); var fullSize = es.Vector2.add(first.bounds.size, second.bounds.size);
return new es.Rectangle(topLeft.x, topLeft.y, fullSize.x, fullSize.y); return new es.Rectangle(topLeft.x, topLeft.y, fullSize.x, fullSize.y);

File diff suppressed because one or more lines are too long

View File

@@ -13,9 +13,10 @@ module es {
// 第一点和最后一点决不能相同。我们想要一个开放的多边形 // 第一点和最后一点决不能相同。我们想要一个开放的多边形
let isPolygonClosed = points[0] == points[points.length - 1]; let isPolygonClosed = points[0] == points[points.length - 1];
let linqPoints = new linq.List(points);
// 最后一个移除 // 最后一个移除
if (isPolygonClosed) if (isPolygonClosed)
points.splice(points.length - 1, 1); linqPoints.remove(linqPoints.last());
let center = Polygon.findPolygonCenter(points); let center = Polygon.findPolygonCenter(points);
this.setLocalOffset(center); this.setLocalOffset(center);

View File

@@ -54,10 +54,7 @@ module es {
this.points = points; this.points = points;
this.recalculateCenterAndEdgeNormals(); this.recalculateCenterAndEdgeNormals();
this._originalPoints = []; this._originalPoints = this.points.slice();
for (let i = 0; i < this.points.length; i++) {
this._originalPoints.push(this.points[i]);
}
} }
/** /**

View File

@@ -14,8 +14,8 @@ module es {
public static polygonToPolygon(first: Polygon, second: Polygon, result: CollisionResult): boolean { public static polygonToPolygon(first: Polygon, second: Polygon, result: CollisionResult): boolean {
let isIntersecting = true; let isIntersecting = true;
let firstEdges = first.edgeNormals; let firstEdges = first.edgeNormals.slice();
let secondEdges = second.edgeNormals; let secondEdges = second.edgeNormals.slice();
let minIntervalDistance = Number.POSITIVE_INFINITY; let minIntervalDistance = Number.POSITIVE_INFINITY;
let translationAxis = new Vector2(); let translationAxis = new Vector2();
let polygonOffset = Vector2.subtract(first.position, second.position); let polygonOffset = Vector2.subtract(first.position, second.position);
@@ -86,7 +86,7 @@ module es {
if (minA < minB) if (minA < minB)
return minB - maxA; return minB - maxA;
return minA - minB; return minA - maxB;
} }
/** /**
@@ -250,7 +250,7 @@ module es {
let distanceSquared = new Ref(0); let distanceSquared = new Ref(0);
let closestPoint = Polygon.getClosestPointOnPolygonToPoint(poly.points, Vector2.subtract(point, poly.position), distanceSquared, result.normal); let closestPoint = Polygon.getClosestPointOnPolygonToPoint(poly.points, Vector2.subtract(point, poly.position), distanceSquared, result.normal);
result.minimumTranslationVector = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared.value), Math.sqrt(distanceSquared.value))); result.minimumTranslationVector = new Vector2(result.normal.x * Math.sqrt(distanceSquared.value), result.normal.y * Math.sqrt(distanceSquared.value));
result.point = Vector2.add(closestPoint, poly.position); result.point = Vector2.add(closestPoint, poly.position);
return true; return true;
@@ -308,7 +308,7 @@ module es {
private static minkowskiDifference(first: Box, second: Box): Rectangle { private static minkowskiDifference(first: Box, second: Box): Rectangle {
// 我们需要第一个框的左上角 // 我们需要第一个框的左上角
// 碰撞器只会修改运动的位置所以我们需要用位置来计算出运动是什么。 // 碰撞器只会修改运动的位置所以我们需要用位置来计算出运动是什么。
let positionOffset = Vector2.subtract(first.position, Vector2.add(first.bounds.location, Vector2.divide(first.bounds.size, new Vector2(2)))); let positionOffset = Vector2.subtract(first.position, Vector2.add(first.bounds.location, new Vector2(first.bounds.size.x / 2, first.bounds.size.y / 2)));
let topLeft = Vector2.subtract(Vector2.add(first.bounds.location, positionOffset), second.bounds.max); let topLeft = Vector2.subtract(Vector2.add(first.bounds.location, positionOffset), second.bounds.max);
let fullSize = Vector2.add(first.bounds.size, second.bounds.size); let fullSize = Vector2.add(first.bounds.size, second.bounds.size);