优化collision

This commit is contained in:
YHH
2020-06-15 08:46:38 +08:00
parent 246e9a9511
commit 7f8f1cf0d0
8 changed files with 132 additions and 7 deletions

View File

@@ -4,13 +4,40 @@ class Polygon extends Shape {
public isUnrotated: boolean = true;
private _polygonCenter: Vector2;
private _areEdgeNormalsDirty = true;
private _originalPoint: Vector2[]
private _originalPoint: Vector2[];
public _edgeNormals: Vector2[];
public get edgeNormals(){
if (this._areEdgeNormalsDirty)
this.buildEdgeNormals();
return this._edgeNormals;
}
public isBox: boolean;
constructor(vertCount: number, radius: number) {
super();
this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
}
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;
}
}
public setPoints(points: Vector2[]) {
this.points = points;
this.recalculateCenterAndEdgeNormals();
@@ -19,6 +46,11 @@ class Polygon extends Shape {
this._originalPoint = points;
}
public collidesWithShape(other: Shape){
if (other instanceof Polygon)
return ShapeCollisions.polygonToPolygon(this, other);
}
public recalculateCenterAndEdgeNormals() {
this._polygonCenter = Polygon.findPolygonCenter(this.points);
this._areEdgeNormalsDirty = true;

View File

@@ -1,4 +1,37 @@
class ShapeCollisions {
public static polygonToPolygon(first: Polygon, second: Polygon){
let result = new CollisionResult();
let isIntersecting = true;
// let firstEdges = first.ed
}
public static circleToPolygon(circle: Circle, polygon: Polygon){
let result = new CollisionResult();
let poly2Circle = Vector2.subtract(circle.position, polygon.position);
let gpp = Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle);
let closestPoint = gpp.closestPoint;
let distanceSquared: number = gpp.distanceSquared;
result.normal = gpp.edgeNormal;
let circleCenterInsidePoly = polygon.containsPoint(circle.position);
if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
return result;
let mtv: Vector2;
if (circleCenterInsidePoly){
mtv = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared) - circle.radius, Math.sqrt(distanceSquared) - circle.radius));
}else{
if (distanceSquared == 0){
mtv = Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius));
}else{
let distance = Math.sqrt(distanceSquared);
// mtv = Vector2.multiply( -Vector2.subtract(poly2Circle, closestPoint), new Vector2((circle.radius - distanceSquared) / distance))
}
}
}
public static circleToRect(circle: Circle, box: Rect): CollisionResult{
let result = new CollisionResult();
let closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;