2020-06-11 20:36:36 +08:00
|
|
|
|
///<reference path="./Shape.ts" />
|
2020-07-22 20:07:14 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 多边形
|
|
|
|
|
|
*/
|
2020-06-11 20:36:36 +08:00
|
|
|
|
class Polygon extends Shape {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
/** 组成多边形的点。它们应该是CW和凸的。 */
|
2020-06-11 20:36:36 +08:00
|
|
|
|
public points: Vector2[];
|
2020-06-12 20:24:51 +08:00
|
|
|
|
private _polygonCenter: Vector2;
|
|
|
|
|
|
private _areEdgeNormalsDirty = true;
|
2020-06-15 10:42:06 +08:00
|
|
|
|
protected _originalPoints: Vector2[];
|
2020-07-09 14:16:10 +08:00
|
|
|
|
public center = new Vector2();
|
2020-07-22 20:07:14 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 多边形坐标
|
|
|
|
|
|
* 此为内部字段 可访问
|
|
|
|
|
|
*/
|
|
|
|
|
|
public position: Vector2 = Vector2.zero;
|
2020-06-15 08:46:38 +08:00
|
|
|
|
|
2020-07-20 16:38:49 +08:00
|
|
|
|
public get bounds(){
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return new Rectangle(this.position.x, this.position.y, 0, 0);
|
2020-07-20 16:38:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-15 08:46:38 +08:00
|
|
|
|
public _edgeNormals: Vector2[];
|
|
|
|
|
|
public get edgeNormals(){
|
|
|
|
|
|
if (this._areEdgeNormalsDirty)
|
|
|
|
|
|
this.buildEdgeNormals();
|
|
|
|
|
|
return this._edgeNormals;
|
|
|
|
|
|
}
|
|
|
|
|
|
public isBox: boolean;
|
2020-06-11 20:36:36 +08:00
|
|
|
|
|
2020-06-16 16:35:17 +08:00
|
|
|
|
constructor(points: Vector2[], isBox?: boolean){
|
2020-06-11 20:36:36 +08:00
|
|
|
|
super();
|
2020-06-16 16:35:17 +08:00
|
|
|
|
|
|
|
|
|
|
this.setPoints(points);
|
|
|
|
|
|
this.isBox = isBox;
|
2020-06-11 20:36:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-15 08:46:38 +08:00
|
|
|
|
private buildEdgeNormals(){
|
|
|
|
|
|
let totalEdges = this.isBox ? 2 : this.points.length;
|
|
|
|
|
|
if (this._edgeNormals == null || this._edgeNormals.length != totalEdges)
|
2020-06-15 20:08:21 +08:00
|
|
|
|
this._edgeNormals = new Array(totalEdges);
|
2020-06-15 08:46:38 +08:00
|
|
|
|
|
|
|
|
|
|
let p2: Vector2;
|
|
|
|
|
|
for (let i = 0; i < totalEdges; i ++){
|
|
|
|
|
|
let p1 = this.points[i];
|
|
|
|
|
|
if (i + 1 >= this.points.length)
|
|
|
|
|
|
p2 = this.points[0];
|
|
|
|
|
|
else
|
|
|
|
|
|
p2 = this.points[i + 1];
|
|
|
|
|
|
|
|
|
|
|
|
let perp = Vector2Ext.perpendicular(p1, p2);
|
|
|
|
|
|
perp = Vector2.normalize(perp);
|
|
|
|
|
|
this._edgeNormals[i] = perp;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-12 20:24:51 +08:00
|
|
|
|
public setPoints(points: Vector2[]) {
|
2020-06-11 20:36:36 +08:00
|
|
|
|
this.points = points;
|
|
|
|
|
|
this.recalculateCenterAndEdgeNormals();
|
|
|
|
|
|
|
2020-06-16 16:35:17 +08:00
|
|
|
|
this._originalPoints = [];
|
|
|
|
|
|
for (let i = 0; i < this.points.length; i ++){
|
|
|
|
|
|
this._originalPoints.push(this.points[i]);
|
|
|
|
|
|
}
|
2020-06-12 20:24:51 +08:00
|
|
|
|
}
|
2020-06-11 20:36:36 +08:00
|
|
|
|
|
2020-06-15 08:46:38 +08:00
|
|
|
|
public collidesWithShape(other: Shape){
|
2020-06-16 11:22:37 +08:00
|
|
|
|
let result = new CollisionResult();
|
|
|
|
|
|
if (other instanceof Polygon){
|
2020-06-21 10:27:15 +08:00
|
|
|
|
return ShapeCollisions.polygonToPolygon(this, other);
|
2020-06-16 11:22:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (other instanceof Circle){
|
|
|
|
|
|
result = ShapeCollisions.circleToPolygon(other, this);
|
|
|
|
|
|
if (result){
|
|
|
|
|
|
result.invertResult();
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw new Error(`overlaps of Polygon to ${other} are not supported`);
|
2020-06-15 08:46:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-12 20:24:51 +08:00
|
|
|
|
public recalculateCenterAndEdgeNormals() {
|
|
|
|
|
|
this._polygonCenter = Polygon.findPolygonCenter(this.points);
|
|
|
|
|
|
this._areEdgeNormalsDirty = true;
|
2020-06-11 20:36:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 00:04:28 +08:00
|
|
|
|
public overlaps(other: Shape){
|
|
|
|
|
|
let result: CollisionResult;
|
|
|
|
|
|
if (other instanceof Polygon)
|
|
|
|
|
|
return ShapeCollisions.polygonToPolygon(this, other);
|
|
|
|
|
|
|
|
|
|
|
|
if (other instanceof Circle){
|
|
|
|
|
|
result = ShapeCollisions.circleToPolygon(other, this);
|
|
|
|
|
|
if (result){
|
|
|
|
|
|
result.invertResult();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw new Error(`overlaps of Pologon to ${other} are not supported`);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-09 15:11:30 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 找到多边形的中心。注意,这对于正则多边形是准确的。不规则多边形没有中心。
|
|
|
|
|
|
* @param points
|
|
|
|
|
|
*/
|
2020-06-12 20:24:51 +08:00
|
|
|
|
public static findPolygonCenter(points: Vector2[]) {
|
2020-06-11 20:36:36 +08:00
|
|
|
|
let x = 0, y = 0;
|
|
|
|
|
|
|
2020-06-12 20:24:51 +08:00
|
|
|
|
for (let i = 0; i < points.length; i++) {
|
2020-06-11 20:36:36 +08:00
|
|
|
|
x += points[i].x;
|
|
|
|
|
|
y += points[i].y;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Vector2(x / points.length, y / points.length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-09 15:11:30 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 重定位多边形的点
|
|
|
|
|
|
* @param points
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static recenterPolygonVerts(points: Vector2[]){
|
|
|
|
|
|
let center = this.findPolygonCenter(points);
|
|
|
|
|
|
for (let i = 0; i < points.length; i ++)
|
|
|
|
|
|
points[i] = Vector2.subtract(points[i], center);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-08 18:12:17 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 迭代多边形的所有边,并得到任意边上离点最近的点。
|
|
|
|
|
|
* 通过最近点的平方距离和它所在的边的法线返回。
|
|
|
|
|
|
* 点应该在多边形的空间中(点-多边形.位置)
|
|
|
|
|
|
* @param points
|
|
|
|
|
|
* @param point
|
|
|
|
|
|
*/
|
2020-06-12 20:24:51 +08:00
|
|
|
|
public static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): { closestPoint, distanceSquared, edgeNormal } {
|
|
|
|
|
|
let distanceSquared = Number.MAX_VALUE;
|
|
|
|
|
|
let edgeNormal = new Vector2(0, 0);
|
|
|
|
|
|
let closestPoint = new Vector2(0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
let tempDistanceSquared;
|
|
|
|
|
|
for (let i = 0; i < points.length; i++) {
|
|
|
|
|
|
let j = i + 1;
|
|
|
|
|
|
if (j == points.length)
|
|
|
|
|
|
j = 0;
|
|
|
|
|
|
|
|
|
|
|
|
let closest = ShapeCollisions.closestPointOnLine(points[i], points[j], point);
|
|
|
|
|
|
tempDistanceSquared = Vector2.distanceSquared(point, closest);
|
|
|
|
|
|
|
|
|
|
|
|
if (tempDistanceSquared < distanceSquared) {
|
|
|
|
|
|
distanceSquared = tempDistanceSquared;
|
|
|
|
|
|
closestPoint = closest;
|
|
|
|
|
|
|
2020-07-08 18:12:17 +08:00
|
|
|
|
// 求直线的法线
|
2020-06-12 20:24:51 +08:00
|
|
|
|
let line = Vector2.subtract(points[j], points[i]);
|
|
|
|
|
|
edgeNormal.x = -line.y;
|
|
|
|
|
|
edgeNormal.y = line.x;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
edgeNormal = Vector2.normalize(edgeNormal);
|
|
|
|
|
|
|
|
|
|
|
|
return { closestPoint: closestPoint, distanceSquared: distanceSquared, edgeNormal: edgeNormal };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
public recalculateBounds(collider: Collider){
|
|
|
|
|
|
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心,我们会从那开始
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-12 20:24:51 +08:00
|
|
|
|
public pointCollidesWithShape(point: Vector2): CollisionResult {
|
|
|
|
|
|
return ShapeCollisions.pointToPoly(point, this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-08 18:12:17 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 本质上,这个算法所做的就是从一个点发射一条射线。
|
|
|
|
|
|
* 如果它与奇数条多边形边相交,我们就知道它在多边形内部。
|
|
|
|
|
|
* @param point
|
|
|
|
|
|
*/
|
2020-06-12 20:24:51 +08:00
|
|
|
|
public containsPoint(point: Vector2) {
|
2020-07-08 18:12:17 +08:00
|
|
|
|
// 将点归一化到多边形坐标空间中
|
2020-06-12 20:24:51 +08:00
|
|
|
|
point = Vector2.subtract(point, this.position);
|
|
|
|
|
|
|
|
|
|
|
|
let isInside = false;
|
|
|
|
|
|
for (let i = 0, j = this.points.length - 1; i < this.points.length; j = i++) {
|
|
|
|
|
|
if (((this.points[i].y > point.y) != (this.points[j].y > point.y)) &&
|
|
|
|
|
|
(point.x < (this.points[j].x - this.points[i].x) * (point.y - this.points[i].y) / (this.points[j].y - this.points[i].y) +
|
|
|
|
|
|
this.points[i].x)) {
|
|
|
|
|
|
isInside = !isInside;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return isInside;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 11:22:37 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 建立一个对称的多边形(六边形,八角形,n角形)并返回点
|
|
|
|
|
|
* @param vertCount
|
|
|
|
|
|
* @param radius
|
|
|
|
|
|
*/
|
2020-06-12 20:24:51 +08:00
|
|
|
|
public static buildSymmertricalPolygon(vertCount: number, radius: number) {
|
2020-06-15 20:08:21 +08:00
|
|
|
|
let verts = new Array(vertCount);
|
2020-06-11 20:36:36 +08:00
|
|
|
|
|
2020-06-12 20:24:51 +08:00
|
|
|
|
for (let i = 0; i < vertCount; i++) {
|
2020-06-11 20:36:36 +08:00
|
|
|
|
let a = 2 * Math.PI * (i / vertCount);
|
2020-06-15 12:16:23 +08:00
|
|
|
|
verts[i] = new Vector2(Math.cos(a), Math.sin(a) * radius);
|
2020-06-11 20:36:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return verts;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|