2020-06-11 20:36:36 +08:00
|
|
|
|
///<reference path="./Shape.ts" />
|
2020-07-22 23:30:31 +08:00
|
|
|
|
module es {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 多边形
|
2020-07-22 20:07:14 +08:00
|
|
|
|
*/
|
2020-07-22 23:30:31 +08:00
|
|
|
|
export class Polygon extends Shape {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 组成多边形的点
|
|
|
|
|
|
* 保持顺时针与凸边形
|
|
|
|
|
|
*/
|
|
|
|
|
|
public points: Vector2[];
|
|
|
|
|
|
public _areEdgeNormalsDirty = true;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 多边形的原始数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
public _originalPoints: Vector2[];
|
|
|
|
|
|
public _polygonCenter: Vector2;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 用于优化未旋转box碰撞
|
|
|
|
|
|
*/
|
|
|
|
|
|
public isBox: boolean;
|
|
|
|
|
|
public isUnrotated: boolean = true;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 从点构造一个多边形
|
|
|
|
|
|
* 多边形应该以顺时针方式指定 不能重复第一个/最后一个点,它们以0 0为中心
|
|
|
|
|
|
* @param points
|
|
|
|
|
|
* @param isBox
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
constructor(points: Vector2[], isBox?: boolean) {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
|
|
this.setPoints(points);
|
|
|
|
|
|
this.isBox = isBox;
|
2020-06-15 08:46:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-02 18:25:30 +08:00
|
|
|
|
public create(vertCount: number, radius: number) {
|
|
|
|
|
|
Polygon.buildSymmetricalPolygon(vertCount, radius);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public _edgeNormals: Vector2[];
|
2020-06-11 20:36:36 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
2020-07-28 16:25:20 +08:00
|
|
|
|
* 边缘法线用于SAT碰撞检测。缓存它们用于避免squareRoots
|
|
|
|
|
|
* box只有两个边缘 因为其他两边是平行的
|
2020-07-22 23:30:31 +08:00
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public get edgeNormals() {
|
|
|
|
|
|
if (this._areEdgeNormalsDirty)
|
|
|
|
|
|
this.buildEdgeNormals();
|
|
|
|
|
|
return this._edgeNormals;
|
2020-06-16 11:22:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-31 17:17:44 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 重置点并重新计算中心和边缘法线
|
|
|
|
|
|
* @param points
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setPoints(points: Vector2[]) {
|
|
|
|
|
|
this.points = points;
|
|
|
|
|
|
this.recalculateCenterAndEdgeNormals();
|
|
|
|
|
|
|
2021-06-29 18:40:34 +08:00
|
|
|
|
this._originalPoints = [];
|
|
|
|
|
|
this.points.forEach(p => {
|
|
|
|
|
|
this._originalPoints.push(p.clone());
|
|
|
|
|
|
});
|
2020-07-31 17:17:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 重新计算多边形中心
|
|
|
|
|
|
* 如果点数改变必须调用该方法
|
|
|
|
|
|
*/
|
|
|
|
|
|
public recalculateCenterAndEdgeNormals() {
|
|
|
|
|
|
this._polygonCenter = Polygon.findPolygonCenter(this.points);
|
|
|
|
|
|
this._areEdgeNormalsDirty = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 建立多边形边缘法线
|
|
|
|
|
|
* 它们仅由edgeNormals getter惰性创建和更新
|
|
|
|
|
|
*/
|
|
|
|
|
|
public buildEdgeNormals() {
|
|
|
|
|
|
// 对于box 我们只需要两条边,因为另外两条边是平行的
|
|
|
|
|
|
let totalEdges = this.isBox ? 2 : this.points.length;
|
2020-08-27 18:48:20 +08:00
|
|
|
|
if (this._edgeNormals == undefined || this._edgeNormals.length != totalEdges)
|
2020-07-31 17:17:44 +08:00
|
|
|
|
this._edgeNormals = new Array(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);
|
2020-08-24 09:06:25 +08:00
|
|
|
|
Vector2Ext.normalize(perp);
|
2020-07-31 17:17:44 +08:00
|
|
|
|
this._edgeNormals[i] = perp;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 建立一个对称的多边形(六边形,八角形,n角形)并返回点
|
|
|
|
|
|
* @param vertCount
|
|
|
|
|
|
* @param radius
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static buildSymmetricalPolygon(vertCount: number, radius: number) {
|
2021-07-02 18:25:30 +08:00
|
|
|
|
const verts = new Array(vertCount);
|
2020-07-22 23:30:31 +08:00
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < vertCount; i++) {
|
2021-07-02 18:25:30 +08:00
|
|
|
|
const a = 2 * Math.PI * (i / vertCount);
|
2020-08-24 09:06:25 +08:00
|
|
|
|
verts[i] = new Vector2(Math.cos(a) * radius, Math.sin(a) * radius);
|
2020-06-16 11:22:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
return verts;
|
2020-06-16 11:22:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 重定位多边形的点
|
|
|
|
|
|
* @param points
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public static recenterPolygonVerts(points: Vector2[]) {
|
2021-07-02 10:11:09 +08:00
|
|
|
|
const center = this.findPolygonCenter(points);
|
2020-07-28 16:25:20 +08:00
|
|
|
|
for (let i = 0; i < points.length; i++)
|
2021-07-02 10:11:09 +08:00
|
|
|
|
points[i] = points[i].sub(center);
|
2020-07-22 23:30:31 +08:00
|
|
|
|
}
|
2020-06-11 20:36:36 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 找到多边形的中心。注意,这对于正则多边形是准确的。不规则多边形没有中心。
|
|
|
|
|
|
* @param points
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static findPolygonCenter(points: Vector2[]) {
|
|
|
|
|
|
let x = 0, y = 0;
|
2020-06-16 00:04:28 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
for (let i = 0; i < points.length; i++) {
|
|
|
|
|
|
x += points[i].x;
|
|
|
|
|
|
y += points[i].y;
|
2020-06-16 00:04:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
return new Vector2(x / points.length, y / points.length);
|
2020-06-16 00:04:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-31 17:17:44 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 不知道辅助顶点,所以取每个顶点,如果你知道辅助顶点,执行climbing算法
|
|
|
|
|
|
* @param points
|
|
|
|
|
|
* @param direction
|
|
|
|
|
|
*/
|
2021-07-02 10:11:09 +08:00
|
|
|
|
public static getFarthestPointInDirection(points: Vector2[], direction: Vector2): Vector2 {
|
2020-07-31 17:17:44 +08:00
|
|
|
|
let index = 0;
|
2021-07-02 10:11:09 +08:00
|
|
|
|
let maxDot = points[index].dot(direction);
|
2020-07-31 17:17:44 +08:00
|
|
|
|
|
2021-07-02 10:11:09 +08:00
|
|
|
|
for (let i = 1; i < points.length; i++) {
|
|
|
|
|
|
let dot = points[i].dot(direction);
|
|
|
|
|
|
if (dot > maxDot) {
|
2020-07-31 17:17:44 +08:00
|
|
|
|
maxDot = dot;
|
|
|
|
|
|
index = i;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return points[index];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 迭代多边形的所有边,并得到任意边上离点最近的点。
|
|
|
|
|
|
* 通过最近点的平方距离和它所在的边的法线返回。
|
|
|
|
|
|
* 点应该在多边形的空间中(点-多边形.位置)
|
|
|
|
|
|
* @param points
|
|
|
|
|
|
* @param point
|
2020-07-27 16:10:36 +08:00
|
|
|
|
* @param distanceSquared
|
|
|
|
|
|
* @param edgeNormal
|
2020-07-22 23:30:31 +08:00
|
|
|
|
*/
|
2021-07-02 10:11:09 +08:00
|
|
|
|
public static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): { distanceSquared: number; edgeNormal: Vector2; closestPoint: Vector2 } {
|
|
|
|
|
|
const res = {
|
|
|
|
|
|
distanceSquared: Number.MAX_VALUE,
|
|
|
|
|
|
edgeNormal: Vector2.zero,
|
|
|
|
|
|
closestPoint: Vector2.zero,
|
|
|
|
|
|
};
|
2020-07-22 23:30:31 +08:00
|
|
|
|
|
2020-08-27 18:48:20 +08:00
|
|
|
|
let tempDistanceSquared = 0;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
for (let i = 0; i < points.length; i++) {
|
|
|
|
|
|
let j = i + 1;
|
2021-07-02 10:11:09 +08:00
|
|
|
|
if (j === points.length)
|
2020-07-22 23:30:31 +08:00
|
|
|
|
j = 0;
|
|
|
|
|
|
|
2021-07-02 10:11:09 +08:00
|
|
|
|
const closest = ShapeCollisionsCircle.closestPointOnLine(points[i], points[j], point);
|
|
|
|
|
|
tempDistanceSquared = Vector2.sqrDistance(point, closest);
|
2020-07-22 23:30:31 +08:00
|
|
|
|
|
2021-07-02 10:11:09 +08:00
|
|
|
|
if (tempDistanceSquared < res.distanceSquared) {
|
|
|
|
|
|
res.distanceSquared = tempDistanceSquared;
|
|
|
|
|
|
res.closestPoint = closest;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
|
|
|
|
|
|
// 求直线的法线
|
2021-07-02 10:11:09 +08:00
|
|
|
|
const line = points[j].sub(points[i]);
|
|
|
|
|
|
res.edgeNormal.x = line.y;
|
|
|
|
|
|
res.edgeNormal.y = -line.x;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-06-16 00:04:28 +08:00
|
|
|
|
|
2021-07-02 10:11:09 +08:00
|
|
|
|
res.edgeNormal = res.edgeNormal.normalize();
|
|
|
|
|
|
return res;
|
2020-06-11 20:36:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
/**
|
2020-07-31 17:17:44 +08:00
|
|
|
|
* 旋转原始点并复制旋转的值到旋转的点
|
|
|
|
|
|
* @param radians
|
|
|
|
|
|
* @param originalPoints
|
|
|
|
|
|
* @param rotatedPoints
|
2020-07-28 16:25:20 +08:00
|
|
|
|
*/
|
2021-07-02 10:11:09 +08:00
|
|
|
|
public static rotatePolygonVerts(radians: number, originalPoints: Vector2[], rotatedPoints: Vector2[]) {
|
2020-07-31 17:17:44 +08:00
|
|
|
|
let cos = Math.cos(radians);
|
2020-08-27 18:48:20 +08:00
|
|
|
|
let sin = Math.sin(radians);
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
2021-07-02 10:11:09 +08:00
|
|
|
|
for (let i = 0; i < originalPoints.length; i++) {
|
2020-07-31 17:17:44 +08:00
|
|
|
|
let position = originalPoints[i];
|
|
|
|
|
|
rotatedPoints[i] = new Vector2(position.x * cos + position.y * -sin, position.x * sin + position.y * cos);
|
2020-07-28 16:25:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public recalculateBounds(collider: Collider) {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心,我们会从那开始
|
2021-07-02 10:11:09 +08:00
|
|
|
|
this.center = collider.localOffset;
|
2020-06-11 20:36:36 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (collider.shouldColliderScaleAndRotateWithTransform) {
|
2020-07-23 13:25:10 +08:00
|
|
|
|
let hasUnitScale = true;
|
2021-07-02 10:11:09 +08:00
|
|
|
|
const tempMat: Matrix2D = new Matrix2D();
|
|
|
|
|
|
const combinedMatrix: Matrix2D = new Matrix2D();
|
|
|
|
|
|
Matrix2D.createTranslation(
|
|
|
|
|
|
this._polygonCenter.x * -1,
|
|
|
|
|
|
this._polygonCenter.y * -1,
|
|
|
|
|
|
combinedMatrix
|
|
|
|
|
|
);
|
2020-07-23 13:25:10 +08:00
|
|
|
|
|
2020-08-28 09:13:53 +08:00
|
|
|
|
if (!collider.entity.transform.scale.equals(Vector2.one)) {
|
2021-07-02 10:11:09 +08:00
|
|
|
|
Matrix2D.createScale(
|
|
|
|
|
|
collider.entity.scale.x,
|
|
|
|
|
|
collider.entity.scale.y,
|
|
|
|
|
|
tempMat
|
|
|
|
|
|
);
|
|
|
|
|
|
Matrix2D.multiply(combinedMatrix, tempMat, combinedMatrix);
|
2020-07-23 13:25:10 +08:00
|
|
|
|
hasUnitScale = false;
|
|
|
|
|
|
|
|
|
|
|
|
// 缩放偏移量并将其设置为中心。如果我们有旋转,它会在下面重置
|
2021-07-02 10:11:09 +08:00
|
|
|
|
const scaledOffset = new Vector2(
|
|
|
|
|
|
collider.localOffset.x * collider.entity.scale.x,
|
|
|
|
|
|
collider.localOffset.y * collider.entity.scale.y
|
|
|
|
|
|
);
|
|
|
|
|
|
this.center = scaledOffset;
|
2020-07-23 13:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (collider.entity.transform.rotation != 0) {
|
2021-07-02 10:11:09 +08:00
|
|
|
|
Matrix2D.createRotation(
|
|
|
|
|
|
MathHelper.Deg2Rad * collider.entity.rotation,
|
|
|
|
|
|
tempMat
|
|
|
|
|
|
);
|
|
|
|
|
|
Matrix2D.multiply(combinedMatrix, tempMat, combinedMatrix);
|
2020-07-23 13:25:10 +08:00
|
|
|
|
|
|
|
|
|
|
// 为了处理偏移原点的旋转我们只需要将圆心在(0,0)附近移动
|
|
|
|
|
|
// 我们的偏移使角度为0我们还需要处理这里的比例所以我们先对偏移进行缩放以得到合适的长度。
|
2021-07-02 10:11:09 +08:00
|
|
|
|
const offsetAngle = Math.atan2(collider.localOffset.y * collider.entity.transform.scale.y, collider.localOffset.x * collider.entity.transform.scale.x) * MathHelper.Rad2Deg;
|
|
|
|
|
|
const offsetLength = hasUnitScale ? collider._localOffsetLength :
|
|
|
|
|
|
collider.localOffset.multiply(collider.entity.transform.scale).magnitude();
|
2023-03-13 14:51:38 +08:00
|
|
|
|
this.center = MathHelper.pointOnCircle(Vector2.zero, offsetLength,
|
2020-08-26 19:56:48 +08:00
|
|
|
|
collider.entity.transform.rotationDegrees + offsetAngle);
|
2020-07-23 13:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-02 10:11:09 +08:00
|
|
|
|
Matrix2D.createTranslation(
|
|
|
|
|
|
this._polygonCenter.x,
|
|
|
|
|
|
this._polygonCenter.y,
|
|
|
|
|
|
tempMat
|
|
|
|
|
|
);
|
|
|
|
|
|
Matrix2D.multiply(combinedMatrix, tempMat, combinedMatrix);
|
2020-07-23 13:25:10 +08:00
|
|
|
|
|
|
|
|
|
|
// 最后变换原始点
|
2021-07-02 10:11:09 +08:00
|
|
|
|
this.points = [];
|
|
|
|
|
|
this._originalPoints.forEach(p => {
|
|
|
|
|
|
this.points.push(p.transform(combinedMatrix));
|
|
|
|
|
|
});
|
2020-07-23 13:25:10 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this.isUnrotated = collider.entity.transform.rotation == 0;
|
2020-07-23 13:25:10 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果旋转的话,我们只需要重建边的法线
|
|
|
|
|
|
if (collider._isRotationDirty)
|
|
|
|
|
|
this._areEdgeNormalsDirty = true;
|
2020-06-12 20:24:51 +08:00
|
|
|
|
}
|
2020-07-22 23:30:31 +08:00
|
|
|
|
|
2021-07-02 10:11:09 +08:00
|
|
|
|
this.position = collider.transform.position.add(this.center);
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this.bounds = Rectangle.rectEncompassingPoints(this.points);
|
2021-07-02 10:11:09 +08:00
|
|
|
|
this.bounds.location = this.bounds.location.add(this.position);
|
2020-06-12 20:24:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public overlaps(other: Shape) {
|
2022-03-12 10:23:33 +08:00
|
|
|
|
let result = new Out<CollisionResult>();
|
2020-07-22 23:30:31 +08:00
|
|
|
|
if (other instanceof Polygon)
|
2021-04-28 14:43:48 +08:00
|
|
|
|
return ShapeCollisionsPolygon.polygonToPolygon(this, other, result);
|
2020-06-12 20:24:51 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (other instanceof Circle) {
|
2021-04-28 14:43:48 +08:00
|
|
|
|
if (ShapeCollisionsCircle.circleToPolygon(other, this, result)) {
|
2022-03-12 10:23:33 +08:00
|
|
|
|
result.value.invertResult();
|
2020-07-22 23:30:31 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2020-06-12 20:24:51 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
throw new Error(`overlaps of Pologon to ${other} are not supported`);
|
|
|
|
|
|
}
|
2020-06-12 20:24:51 +08:00
|
|
|
|
|
2022-03-12 10:23:33 +08:00
|
|
|
|
public collidesWithShape(other: Shape, result: Out<CollisionResult>): boolean {
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (other instanceof Polygon) {
|
2021-04-28 14:43:48 +08:00
|
|
|
|
return ShapeCollisionsPolygon.polygonToPolygon(this, other, result);
|
2020-06-12 20:24:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (other instanceof Circle) {
|
2021-04-28 14:43:48 +08:00
|
|
|
|
if (ShapeCollisionsCircle.circleToPolygon(other, this, result)) {
|
2022-03-12 10:23:33 +08:00
|
|
|
|
result.value.invertResult();
|
2020-07-27 16:10:36 +08:00
|
|
|
|
return true;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
}
|
2020-06-12 20:24:51 +08:00
|
|
|
|
|
2020-07-27 16:10:36 +08:00
|
|
|
|
return false;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw new Error(`overlaps of Polygon to ${other} are not supported`);
|
|
|
|
|
|
}
|
2020-06-11 20:36:36 +08:00
|
|
|
|
|
2022-03-12 10:23:33 +08:00
|
|
|
|
public collidesWithLine(start: Vector2, end: Vector2, hit: Out<RaycastHit>): boolean {
|
2021-04-28 14:43:48 +08:00
|
|
|
|
return ShapeCollisionsLine.lineToPoly(start, end, this, hit);
|
2020-08-03 14:45:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 本质上,这个算法所做的就是从一个点发射一条射线。
|
|
|
|
|
|
* 如果它与奇数条多边形边相交,我们就知道它在多边形内部。
|
|
|
|
|
|
* @param point
|
|
|
|
|
|
*/
|
|
|
|
|
|
public containsPoint(point: Vector2) {
|
|
|
|
|
|
// 将点归一化到多边形坐标空间中
|
2021-07-02 10:11:09 +08:00
|
|
|
|
point = point.sub(this.position);
|
2020-07-22 23:30:31 +08:00
|
|
|
|
|
|
|
|
|
|
let isInside = false;
|
|
|
|
|
|
for (let i = 0, j = this.points.length - 1; i < this.points.length; j = i++) {
|
2021-07-02 10:11:09 +08:00
|
|
|
|
if (((this.points[i].y > point.y) !== (this.points[j].y > point.y)) &&
|
2020-07-22 23:30:31 +08:00
|
|
|
|
(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-11 20:36:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-12 10:23:33 +08:00
|
|
|
|
public pointCollidesWithShape(point: Vector2, result: Out<CollisionResult>): boolean {
|
2021-04-28 14:43:48 +08:00
|
|
|
|
return ShapeCollisionsPoint.pointToPoly(point, this, result);
|
2020-07-22 23:30:31 +08:00
|
|
|
|
}
|
2020-06-11 20:36:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|