完善shapeCollision 支持多边形

This commit is contained in:
YHH
2020-06-16 00:04:28 +08:00
parent 5186bc0187
commit dba43b9773
22 changed files with 872 additions and 94 deletions

View File

@@ -3,26 +3,26 @@ class Circle extends Shape {
public radius: number;
private _originalRadius: number;
constructor(radius: number){
constructor(radius: number) {
super();
this.radius = radius;
this._originalRadius = radius;
}
public pointCollidesWithShape(point: Vector2): CollisionResult {
return ShapeCollisions.pointToCicle(point, this);
return ShapeCollisions.pointToCircle(point, this);
}
public collidesWithShape(other: Shape): CollisionResult{
if (other instanceof Box && (other as Box).isUnrotated){
return ShapeCollisions.circleToRect(this, other as Box);
public collidesWithShape(other: Shape): CollisionResult {
if (other instanceof Box && (other as Box).isUnrotated) {
return ShapeCollisions.circleToBox(this, other);
}
if (other instanceof Circle){
// TODO CIRCLETOCIRCLE
if (other instanceof Circle) {
return ShapeCollisions.circleToCircle(this, other);
}
if (other instanceof Polygon){
if (other instanceof Polygon) {
return ShapeCollisions.circleToPolygon(this, other);
}
@@ -31,5 +31,34 @@ class Circle extends Shape {
public recalculateBounds(collider: Collider) {
this.center = collider.localOffset;
if (collider.shouldColliderScaleAndRotationWithTransform) {
let scale = collider.entity.transform.scale;
let hasUnitScale = scale.x == 1 && scale.y == 1;
let maxScale = Math.max(scale.x, scale.y);
this.radius = this._originalRadius * maxScale;
if (collider.entity.transform.rotation != 0) {
let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
let offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.transform.scale)).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, collider.entity.transform.rotationDegrees + offsetAngle);
}
}
this.position = Vector2.add(collider.entity.transform.position, this.center);
this.bounds = new Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2);
}
public overlaps(other: Shape){
if (other instanceof Box && (other as Box).isUnrotated)
return Collisions.isRectToCircle(other.bounds, this.position, this.radius);
if (other instanceof Circle)
return Collisions.isCircleToCircle(this.position, this.radius, other.position, (other as Circle).radius);
if (other instanceof Polygon)
return ShapeCollisions.circleToPolygon(this, other);
throw new Error(`overlaps of circle to ${other} are not supported`);
}
}