2020-06-12 20:24:51 +08:00
|
|
|
|
class ShapeCollisions {
|
2020-06-16 09:10:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 检查两个多边形之间的碰撞
|
|
|
|
|
|
* @param first
|
|
|
|
|
|
* @param second
|
|
|
|
|
|
*/
|
2020-06-16 00:04:28 +08:00
|
|
|
|
public static polygonToPolygon(first: Polygon, second: Polygon) {
|
2020-06-15 08:46:38 +08:00
|
|
|
|
let result = new CollisionResult();
|
|
|
|
|
|
let isIntersecting = true;
|
2020-06-15 12:16:23 +08:00
|
|
|
|
|
|
|
|
|
|
let firstEdges = first.edgeNormals;
|
|
|
|
|
|
let secondEdges = second.edgeNormals;
|
|
|
|
|
|
let minIntervalDistance = Number.POSITIVE_INFINITY;
|
|
|
|
|
|
let translationAxis = new Vector2();
|
|
|
|
|
|
let polygonOffset = Vector2.subtract(first.position, second.position);
|
|
|
|
|
|
let axis: Vector2;
|
2020-06-16 00:04:28 +08:00
|
|
|
|
|
|
|
|
|
|
for (let edgeIndex = 0; edgeIndex < firstEdges.length + secondEdges.length; edgeIndex++) {
|
|
|
|
|
|
if (edgeIndex < firstEdges.length) {
|
2020-06-15 20:08:21 +08:00
|
|
|
|
axis = firstEdges[edgeIndex];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
axis = secondEdges[edgeIndex - firstEdges.length];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let minA = 0;
|
|
|
|
|
|
let minB = 0;
|
|
|
|
|
|
let maxA = 0;
|
|
|
|
|
|
let maxB = 0;
|
|
|
|
|
|
let intervalDist = 0;
|
2020-06-16 00:04:28 +08:00
|
|
|
|
let ta = this.getInterval(axis, first, minA, maxA);
|
|
|
|
|
|
minA = ta.min;
|
|
|
|
|
|
minB = ta.max;
|
|
|
|
|
|
let tb = this.getInterval(axis, second, minB, maxB);
|
|
|
|
|
|
minB = tb.min;
|
|
|
|
|
|
maxB = tb.max;
|
|
|
|
|
|
|
|
|
|
|
|
let relativeIntervalOffset = Vector2.dot(polygonOffset, axis);
|
|
|
|
|
|
minA += relativeIntervalOffset;
|
|
|
|
|
|
maxA += relativeIntervalOffset;
|
|
|
|
|
|
|
|
|
|
|
|
intervalDist = this.intervalDistance(minA, maxA, minB, maxB);
|
|
|
|
|
|
if (intervalDist > 0)
|
|
|
|
|
|
isIntersecting = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (!isIntersecting)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
intervalDist = Math.abs(intervalDist);
|
|
|
|
|
|
if (intervalDist < minIntervalDistance) {
|
|
|
|
|
|
minIntervalDistance = intervalDist;
|
|
|
|
|
|
translationAxis = axis;
|
|
|
|
|
|
|
|
|
|
|
|
if (Vector2.dot(translationAxis, polygonOffset) < 0)
|
|
|
|
|
|
translationAxis = new Vector2(-translationAxis);
|
|
|
|
|
|
}
|
2020-06-15 20:08:21 +08:00
|
|
|
|
}
|
2020-06-16 00:04:28 +08:00
|
|
|
|
|
|
|
|
|
|
result.normal = translationAxis;
|
|
|
|
|
|
result.minimumTranslationVector = Vector2.multiply(new Vector2(-translationAxis), new Vector2(minIntervalDistance));
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
2020-06-15 20:08:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 09:10:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 计算[minA, maxA]和[minB, maxB]之间的距离。如果间隔重叠,距离是负的
|
|
|
|
|
|
* @param minA
|
|
|
|
|
|
* @param maxA
|
|
|
|
|
|
* @param minB
|
|
|
|
|
|
* @param maxB
|
|
|
|
|
|
*/
|
2020-06-16 00:04:28 +08:00
|
|
|
|
public static intervalDistance(minA: number, maxA: number, minB: number, maxB) {
|
|
|
|
|
|
if (minA < minB)
|
|
|
|
|
|
return minB - maxA;
|
|
|
|
|
|
|
|
|
|
|
|
return minA - minB;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 09:10:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 计算一个多边形在一个轴上的投影,并返回一个[min,max]区间
|
|
|
|
|
|
* @param axis
|
|
|
|
|
|
* @param polygon
|
|
|
|
|
|
* @param min
|
|
|
|
|
|
* @param max
|
|
|
|
|
|
*/
|
2020-06-16 00:04:28 +08:00
|
|
|
|
public static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number) {
|
2020-06-15 20:08:21 +08:00
|
|
|
|
let dot = Vector2.dot(polygon.points[0], axis);
|
|
|
|
|
|
min = max = dot;
|
|
|
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
|
for (let i = 1; i < polygon.points.length; i++) {
|
2020-06-15 20:08:21 +08:00
|
|
|
|
dot = Vector2.dot(polygon.points[i], axis);
|
2020-06-16 00:04:28 +08:00
|
|
|
|
if (dot < min) {
|
2020-06-15 20:08:21 +08:00
|
|
|
|
min = dot;
|
2020-06-16 00:04:28 +08:00
|
|
|
|
} else if (dot > max) {
|
2020-06-15 20:08:21 +08:00
|
|
|
|
max = dot;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-06-16 00:04:28 +08:00
|
|
|
|
|
|
|
|
|
|
return { min: min, max: max };
|
2020-06-15 08:46:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 09:10:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param circle
|
|
|
|
|
|
* @param polygon
|
|
|
|
|
|
*/
|
2020-06-16 00:04:28 +08:00
|
|
|
|
public static circleToPolygon(circle: Circle, polygon: Polygon) {
|
2020-06-15 08:46:38 +08:00
|
|
|
|
let result = new CollisionResult();
|
|
|
|
|
|
|
|
|
|
|
|
let poly2Circle = Vector2.subtract(circle.position, polygon.position);
|
|
|
|
|
|
|
|
|
|
|
|
let gpp = Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle);
|
2020-06-16 00:04:28 +08:00
|
|
|
|
let closestPoint: Vector2 = gpp.closestPoint;
|
2020-06-15 08:46:38 +08:00
|
|
|
|
let distanceSquared: number = gpp.distanceSquared;
|
|
|
|
|
|
result.normal = gpp.edgeNormal;
|
|
|
|
|
|
|
|
|
|
|
|
let circleCenterInsidePoly = polygon.containsPoint(circle.position);
|
|
|
|
|
|
if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
|
2020-06-16 00:04:28 +08:00
|
|
|
|
return null;
|
2020-06-15 08:46:38 +08:00
|
|
|
|
|
|
|
|
|
|
let mtv: Vector2;
|
2020-06-16 00:04:28 +08:00
|
|
|
|
if (circleCenterInsidePoly) {
|
|
|
|
|
|
mtv = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared) - circle.radius));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (distanceSquared == 0) {
|
|
|
|
|
|
mtv = Vector2.multiply(result.normal, new Vector2(circle.radius));
|
|
|
|
|
|
} else {
|
2020-06-15 08:46:38 +08:00
|
|
|
|
let distance = Math.sqrt(distanceSquared);
|
2020-06-16 00:04:28 +08:00
|
|
|
|
mtv = Vector2.multiply(new Vector2(-Vector2.subtract(poly2Circle, closestPoint)), new Vector2((circle.radius - distanceSquared) / distance));
|
2020-06-15 08:46:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-06-16 00:04:28 +08:00
|
|
|
|
|
|
|
|
|
|
result.minimumTranslationVector = mtv;
|
|
|
|
|
|
result.point = Vector2.add(closestPoint, polygon.position);
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
2020-06-15 08:46:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 09:10:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 适用于圆心在方框内以及只与方框外圆心重叠的圆。
|
|
|
|
|
|
* @param circle
|
|
|
|
|
|
* @param box
|
|
|
|
|
|
*/
|
2020-06-16 00:04:28 +08:00
|
|
|
|
public static circleToBox(circle: Circle, box: Box): CollisionResult {
|
2020-06-12 20:24:51 +08:00
|
|
|
|
let result = new CollisionResult();
|
|
|
|
|
|
let closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
|
|
|
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
|
if (box.containsPoint(circle.position)) {
|
2020-06-12 20:24:51 +08:00
|
|
|
|
result.point = closestPointOnBounds;
|
|
|
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
|
let safePlace = Vector2.add(closestPointOnBounds, Vector2.subtract(result.normal, new Vector2(circle.radius)));
|
|
|
|
|
|
result.minimumTranslationVector = Vector2.subtract(circle.position, safePlace);
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
2020-06-12 20:24:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
|
let sqrDistance = Vector2.distanceSquared(closestPointOnBounds, circle.position);
|
|
|
|
|
|
if (sqrDistance == 0) {
|
|
|
|
|
|
result.minimumTranslationVector = Vector2.multiply(result.normal, new Vector2(circle.radius));
|
|
|
|
|
|
} else if (sqrDistance <= circle.radius * circle.radius) {
|
|
|
|
|
|
result.normal = Vector2.subtract(circle.position, closestPointOnBounds);
|
|
|
|
|
|
let depth = result.normal.length() - circle.radius;
|
|
|
|
|
|
result.normal = Vector2Ext.normalize(result.normal);
|
|
|
|
|
|
result.minimumTranslationVector = Vector2.multiply(new Vector2(depth), result.normal);
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
2020-06-12 20:24:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 09:10:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param point
|
|
|
|
|
|
* @param circle
|
|
|
|
|
|
*/
|
2020-06-16 00:04:28 +08:00
|
|
|
|
public static pointToCircle(point: Vector2, circle: Circle) {
|
2020-06-12 20:24:51 +08:00
|
|
|
|
let result = new CollisionResult();
|
|
|
|
|
|
|
|
|
|
|
|
let distanceSquared = Vector2.distanceSquared(point, circle.position);
|
|
|
|
|
|
let sumOfRadii = 1 + circle.radius;
|
|
|
|
|
|
let collided = distanceSquared < sumOfRadii * sumOfRadii;
|
2020-06-16 00:04:28 +08:00
|
|
|
|
if (collided) {
|
2020-06-12 20:24:51 +08:00
|
|
|
|
result.normal = Vector2.normalize(Vector2.subtract(point, circle.position));
|
|
|
|
|
|
let depth = sumOfRadii - Math.sqrt(distanceSquared);
|
|
|
|
|
|
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth, -depth), result.normal);
|
|
|
|
|
|
result.point = Vector2.add(circle.position, Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius)));
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
|
return null;
|
2020-06-12 20:24:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 09:10:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param lineA
|
|
|
|
|
|
* @param lineB
|
|
|
|
|
|
* @param closestTo
|
|
|
|
|
|
*/
|
2020-06-16 00:04:28 +08:00
|
|
|
|
public static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2) {
|
2020-06-12 20:24:51 +08:00
|
|
|
|
let v = Vector2.subtract(lineB, lineA);
|
|
|
|
|
|
let w = Vector2.subtract(closestTo, lineA);
|
|
|
|
|
|
let t = Vector2.dot(w, v) / Vector2.dot(v, v);
|
|
|
|
|
|
t = MathHelper.clamp(t, 0, 1);
|
|
|
|
|
|
|
|
|
|
|
|
return Vector2.add(lineA, Vector2.multiply(v, new Vector2(t, t)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 09:10:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param point
|
|
|
|
|
|
* @param poly
|
|
|
|
|
|
*/
|
2020-06-16 00:04:28 +08:00
|
|
|
|
public static pointToPoly(point: Vector2, poly: Polygon) {
|
2020-06-12 20:24:51 +08:00
|
|
|
|
let result = new CollisionResult();
|
|
|
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
|
if (poly.containsPoint(point)) {
|
2020-06-12 20:24:51 +08:00
|
|
|
|
let distanceSquared: number;
|
|
|
|
|
|
let gpp = Polygon.getClosestPointOnPolygonToPoint(poly.points, Vector2.subtract(point, poly.position));
|
|
|
|
|
|
let closestPoint = gpp.closestPoint;
|
|
|
|
|
|
distanceSquared = gpp.distanceSquared;
|
|
|
|
|
|
result.normal = gpp.edgeNormal;
|
|
|
|
|
|
|
|
|
|
|
|
result.minimumTranslationVector = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared), Math.sqrt(distanceSquared)));
|
|
|
|
|
|
result.point = Vector2.add(closestPoint, poly.position);
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 09:10:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param first
|
|
|
|
|
|
* @param second
|
|
|
|
|
|
*/
|
2020-06-16 00:04:28 +08:00
|
|
|
|
public static circleToCircle(first: Circle, second: Circle){
|
|
|
|
|
|
let result = new CollisionResult();
|
|
|
|
|
|
|
|
|
|
|
|
let distanceSquared = Vector2.distanceSquared(first.position, second.position);
|
|
|
|
|
|
let sumOfRadii = first.radius + second.radius;
|
|
|
|
|
|
let collided = distanceSquared < sumOfRadii * sumOfRadii;
|
|
|
|
|
|
if (collided){
|
|
|
|
|
|
result.normal = Vector2.normalize(Vector2.subtract(first.position, second.position));
|
|
|
|
|
|
let depth = sumOfRadii - Math.sqrt(distanceSquared);
|
|
|
|
|
|
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth), result.normal);
|
|
|
|
|
|
result.point = Vector2.add(second.position, Vector2.multiply(result.normal, new Vector2(second.radius)));
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
2020-06-12 20:24:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|