2020-06-15 10:42:06 +08:00
|
|
|
|
///<reference path="./Polygon.ts" />
|
2020-07-22 23:30:31 +08:00
|
|
|
|
module es {
|
2020-07-08 18:12:17 +08:00
|
|
|
|
/**
|
2020-07-22 23:30:31 +08:00
|
|
|
|
* 多边形的特殊情况。在进行SAT碰撞检查时,我们只需要检查2个轴而不是8个轴
|
2020-07-08 18:12:17 +08:00
|
|
|
|
*/
|
2020-07-22 23:30:31 +08:00
|
|
|
|
export class Box extends Polygon {
|
|
|
|
|
|
public width: number;
|
|
|
|
|
|
public height: number;
|
2020-06-16 16:35:17 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
constructor(width: number, height: number) {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
super(Box.buildBox(width, height), true);
|
|
|
|
|
|
this.width = width;
|
|
|
|
|
|
this.height = height;
|
|
|
|
|
|
}
|
2020-07-07 18:54:19 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 在一个盒子的形状中建立多边形需要的点的帮助方法
|
|
|
|
|
|
* @param width
|
|
|
|
|
|
* @param height
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
private static buildBox(width: number, height: number): Vector2[] {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
// 我们在(0,0)的中心周围创建点
|
|
|
|
|
|
let halfWidth = width / 2;
|
|
|
|
|
|
let halfHeight = height / 2;
|
|
|
|
|
|
let verts = new Array(4);
|
|
|
|
|
|
verts[0] = new Vector2(-halfWidth, -halfHeight);
|
|
|
|
|
|
verts[1] = new Vector2(halfWidth, -halfHeight);
|
|
|
|
|
|
verts[2] = new Vector2(halfWidth, halfHeight);
|
|
|
|
|
|
verts[3] = new Vector2(-halfWidth, halfHeight);
|
2020-07-07 18:54:19 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
return verts;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更新框点,重新计算中心,设置宽度/高度
|
|
|
|
|
|
* @param width
|
|
|
|
|
|
* @param height
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public updateBox(width: number, height: number) {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this.width = width;
|
|
|
|
|
|
this.height = height;
|
|
|
|
|
|
|
|
|
|
|
|
// 我们在(0,0)的中心周围创建点
|
|
|
|
|
|
let halfWidth = width / 2;
|
|
|
|
|
|
let halfHeight = height / 2;
|
2020-07-07 18:54:19 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this.points[0] = new Vector2(-halfWidth, -halfHeight);
|
|
|
|
|
|
this.points[1] = new Vector2(halfWidth, -halfHeight);
|
|
|
|
|
|
this.points[2] = new Vector2(halfWidth, halfHeight);
|
|
|
|
|
|
this.points[3] = new Vector2(-halfWidth, halfHeight);
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
for (let i = 0; i < this.points.length; i++)
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this._originalPoints[i] = this.points[i];
|
2020-07-07 12:18:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public overlaps(other: Shape) {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
// 特殊情况,这一个高性能方式实现,其他情况则使用polygon方法检测
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (this.isUnrotated) {
|
2020-07-31 17:17:44 +08:00
|
|
|
|
if (other instanceof Box && other.isUnrotated)
|
2020-07-23 09:10:27 +08:00
|
|
|
|
return this.bounds.intersects(other.bounds);
|
2020-07-07 12:18:51 +08:00
|
|
|
|
|
2020-07-23 09:10:27 +08:00
|
|
|
|
if (other instanceof Circle)
|
2020-08-27 18:48:20 +08:00
|
|
|
|
return Collisions.rectToCircle(this.bounds, other.position, other.radius);
|
2020-07-23 09:10:27 +08:00
|
|
|
|
}
|
2020-07-07 12:18:51 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
return super.overlaps(other);
|
|
|
|
|
|
}
|
2020-06-15 10:42:06 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public collidesWithShape(other: Shape, result: CollisionResult): boolean {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
// 特殊情况,这一个高性能方式实现,其他情况则使用polygon方法检测
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (other instanceof Box && (other as Box).isUnrotated) {
|
2021-04-28 14:43:48 +08:00
|
|
|
|
return ShapeCollisionsBox.boxToBox(this, other, result);
|
2020-07-22 23:30:31 +08:00
|
|
|
|
}
|
2020-06-15 10:42:06 +08:00
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
// TODO: 让 minkowski 运行于 cricleToBox
|
2020-06-15 10:42:06 +08:00
|
|
|
|
|
2020-07-27 16:10:36 +08:00
|
|
|
|
return super.collidesWithShape(other, result);
|
2020-07-22 23:30:31 +08:00
|
|
|
|
}
|
2020-06-15 10:42:06 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public containsPoint(point: Vector2) {
|
2020-07-23 09:10:27 +08:00
|
|
|
|
if (this.isUnrotated)
|
|
|
|
|
|
return this.bounds.contains(point.x, point.y);
|
|
|
|
|
|
|
|
|
|
|
|
return super.containsPoint(point);
|
2020-07-22 23:30:31 +08:00
|
|
|
|
}
|
2020-07-31 17:17:44 +08:00
|
|
|
|
|
|
|
|
|
|
public pointCollidesWithShape(point: es.Vector2, result: es.CollisionResult): boolean {
|
|
|
|
|
|
if (this.isUnrotated)
|
2021-04-28 14:43:48 +08:00
|
|
|
|
return ShapeCollisionsPoint.pointToBox(point, this, result);
|
2020-07-31 17:17:44 +08:00
|
|
|
|
|
|
|
|
|
|
return super.pointCollidesWithShape(point, result);
|
|
|
|
|
|
}
|
2020-06-15 10:42:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|