新增 circleCollider与 polygonCollider

This commit is contained in:
yhh
2020-07-09 15:11:30 +08:00
parent 6e3eb1189a
commit 817b703d4f
13 changed files with 253 additions and 18 deletions

View File

@@ -1,7 +1,7 @@
///<reference path="./Shape.ts" />
class Circle extends Shape {
public radius: number;
private _originalRadius: number;
public _originalRadius: number;
public center = new Vector2();
constructor(radius: number) {

View File

@@ -93,6 +93,10 @@ class Polygon extends Shape {
throw new Error(`overlaps of Pologon to ${other} are not supported`);
}
/**
* 找到多边形的中心。注意,这对于正则多边形是准确的。不规则多边形没有中心。
* @param points
*/
public static findPolygonCenter(points: Vector2[]) {
let x = 0, y = 0;
@@ -104,6 +108,16 @@ class Polygon extends Shape {
return new Vector2(x / points.length, y / points.length);
}
/**
* 重定位多边形的点
* @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);
}
/**
* 迭代多边形的所有边,并得到任意边上离点最近的点。
* 通过最近点的平方距离和它所在的边的法线返回。