2020-06-11 20:36:36 +08:00
|
|
|
///<reference path="./Shape.ts" />
|
|
|
|
|
class Polygon extends Shape {
|
|
|
|
|
public points: Vector2[];
|
2020-06-12 20:24:51 +08:00
|
|
|
public isUnrotated: boolean = true;
|
|
|
|
|
private _polygonCenter: Vector2;
|
|
|
|
|
private _areEdgeNormalsDirty = true;
|
2020-06-15 10:42:06 +08:00
|
|
|
protected _originalPoints: Vector2[];
|
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-12 20:24:51 +08:00
|
|
|
constructor(vertCount: number, radius: number) {
|
2020-06-11 20:36:36 +08:00
|
|
|
super();
|
|
|
|
|
this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
this._edgeNormals = new Vector2[totalEdges];
|
|
|
|
|
|
|
|
|
|
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-15 10:42:06 +08:00
|
|
|
this._originalPoints = new Vector2[points.length];
|
|
|
|
|
this._originalPoints = points;
|
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){
|
|
|
|
|
if (other instanceof Polygon)
|
|
|
|
|
return ShapeCollisions.polygonToPolygon(this, other);
|
|
|
|
|
}
|
|
|
|
|
|
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-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-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;
|
|
|
|
|
|
|
|
|
|
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 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public pointCollidesWithShape(point: Vector2): CollisionResult {
|
|
|
|
|
return ShapeCollisions.pointToPoly(point, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public containsPoint(point: Vector2) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static buildSymmertricalPolygon(vertCount: number, radius: number) {
|
2020-06-11 20:36:36 +08:00
|
|
|
let verts = new Vector2[vertCount];
|
|
|
|
|
|
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);
|
|
|
|
|
verts[i] = new Vector2(Math.cos(a), Math.sign(a) * radius);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return verts;
|
|
|
|
|
}
|
|
|
|
|
}
|