新增shape形状
This commit is contained in:
40
source/src/Physics/Shapes/Polygon.ts
Normal file
40
source/src/Physics/Shapes/Polygon.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
///<reference path="./Shape.ts" />
|
||||
class Polygon extends Shape {
|
||||
public points: Vector2[];
|
||||
|
||||
constructor(vertCount: number, radius: number){
|
||||
super();
|
||||
this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
|
||||
}
|
||||
|
||||
public setPoints(points: Vector2[]){
|
||||
this.points = points;
|
||||
this.recalculateCenterAndEdgeNormals();
|
||||
}
|
||||
|
||||
public recalculateCenterAndEdgeNormals(){
|
||||
|
||||
}
|
||||
|
||||
public static findPolygonCenter(points: Vector2[]){
|
||||
let x = 0, y = 0;
|
||||
|
||||
for (let i = 0; i < points.length; i++){
|
||||
x += points[i].x;
|
||||
y += points[i].y;
|
||||
}
|
||||
|
||||
return new Vector2(x / points.length, y / points.length);
|
||||
}
|
||||
|
||||
public static buildSymmertricalPolygon(vertCount: number, radius: number){
|
||||
let verts = new Vector2[vertCount];
|
||||
|
||||
for (let i = 0; i < vertCount; i ++){
|
||||
let a = 2 * Math.PI * (i / vertCount);
|
||||
verts[i] = new Vector2(Math.cos(a), Math.sign(a) * radius);
|
||||
}
|
||||
|
||||
return verts;
|
||||
}
|
||||
}
|
||||
3
source/src/Physics/Shapes/Shape.ts
Normal file
3
source/src/Physics/Shapes/Shape.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
abstract class Shape {
|
||||
public bounds: Rectangle;
|
||||
}
|
||||
Reference in New Issue
Block a user