修复polygon数组错误 修复emit空注册报错

This commit is contained in:
yhh
2020-06-15 20:08:21 +08:00
parent c3120d791f
commit 5186bc0187
16 changed files with 398 additions and 32 deletions

View File

@@ -11,6 +11,14 @@ class Physics {
return this._spatialHash.aabbBroadphase(rect, null, layerMask);
}
public static addCollider(collider: Collider){
Physics._spatialHash.register(collider);
}
public static removeCollider(collider: Collider){
Physics._spatialHash.remove(collider);
}
public static updateCollider(collider: Collider){
this._spatialHash.remove(collider);
this._spatialHash.register(collider);

View File

@@ -18,6 +18,18 @@ class Circle extends Shape {
return ShapeCollisions.circleToRect(this, other as Box);
}
if (other instanceof Circle){
// TODO CIRCLETOCIRCLE
}
if (other instanceof Polygon){
return ShapeCollisions.circleToPolygon(this, other);
}
throw new Error(`Collisions of Circle to ${other} are not supported`);
}
public recalculateBounds(collider: Collider) {
this.center = collider.localOffset;
}
}

View File

@@ -22,7 +22,7 @@ class Polygon extends Shape {
private buildEdgeNormals(){
let totalEdges = this.isBox ? 2 : this.points.length;
if (this._edgeNormals == null || this._edgeNormals.length != totalEdges)
this._edgeNormals = new Vector2[totalEdges];
this._edgeNormals = new Array(totalEdges);
let p2: Vector2;
for (let i = 0; i < totalEdges; i ++){
@@ -42,7 +42,7 @@ class Polygon extends Shape {
this.points = points;
this.recalculateCenterAndEdgeNormals();
this._originalPoints = new Vector2[points.length];
this._originalPoints = new Array(points.length);
this._originalPoints = points;
}
@@ -116,7 +116,7 @@ class Polygon extends Shape {
}
public static buildSymmertricalPolygon(vertCount: number, radius: number) {
let verts = new Vector2[vertCount];
let verts = new Array(vertCount);
for (let i = 0; i < vertCount; i++) {
let a = 2 * Math.PI * (i / vertCount);
@@ -125,4 +125,8 @@ class Polygon extends Shape {
return verts;
}
public recalculateBounds(collider: Collider) {
this.center = collider.localOffset;
}
}

View File

@@ -1,6 +1,8 @@
abstract class Shape {
public bounds: Rectangle;
public position: Vector2;
public center: Vector2;
public abstract recalculateBounds(collider: Collider);
public abstract pointCollidesWithShape(point: Vector2): CollisionResult;
}

View File

@@ -10,6 +10,35 @@ class ShapeCollisions {
let polygonOffset = Vector2.subtract(first.position, second.position);
let axis: Vector2;
for (let edgeIndex = 0; edgeIndex < firstEdges.length + secondEdges.length; edgeIndex ++){
if (edgeIndex < firstEdges.length){
axis = firstEdges[edgeIndex];
} else {
axis = secondEdges[edgeIndex - firstEdges.length];
}
let minA = 0;
let minB = 0;
let maxA = 0;
let maxB = 0;
let intervalDist = 0;
this.getInterval(axis, first, minA, maxA);
this.getInterval(axis, second, minB, maxB);
}
}
public static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number){
let dot = Vector2.dot(polygon.points[0], axis);
min = max = dot;
for (let i = 1; i < polygon.points.length; i++){
dot = Vector2.dot(polygon.points[i], axis);
if (dot < min){
min = dot;
}else if(dot > max){
max = dot;
}
}
}
public static circleToPolygon(circle: Circle, polygon: Polygon){