Files
esengine/source/src/Physics/Shapes/Box.ts

94 lines
3.4 KiB
TypeScript
Raw Normal View History

2020-06-15 10:42:06 +08:00
///<reference path="./Polygon.ts" />
module es {
2020-07-08 18:12:17 +08:00
/**
* SAT碰撞检查时28
2020-07-08 18:12:17 +08:00
*/
export class Box extends Polygon {
public width: number;
public height: number;
2020-07-28 16:25:20 +08:00
constructor(width: number, height: number) {
super(Box.buildBox(width, height), true);
this.width = width;
this.height = height;
}
2020-07-07 18:54:19 +08:00
/**
*
* @param width
* @param height
*/
2020-07-28 16:25:20 +08:00
private static buildBox(width: number, height: number): Vector2[] {
// 我们在(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
return verts;
}
/**
* /
* @param width
* @param height
*/
2020-07-28 16:25:20 +08:00
public updateBox(width: number, height: number) {
this.width = width;
this.height = height;
// 我们在(0,0)的中心周围创建点
let halfWidth = width / 2;
let halfHeight = height / 2;
2020-07-07 18:54:19 +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++)
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) {
// 特殊情况这一个高性能方式实现其他情况则使用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)
return Collisions.isRectToCircle(this.bounds, other.position, other.radius);
}
2020-07-07 12:18:51 +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 {
// 特殊情况这一个高性能方式实现其他情况则使用polygon方法检测
2020-07-28 16:25:20 +08:00
if (other instanceof Box && (other as Box).isUnrotated) {
return ShapeCollisions.boxToBox(this, other, result);
}
2020-06-15 10:42:06 +08:00
// TODO: 让 minkowski 运行于 cricleToBox
2020-06-15 10:42:06 +08:00
return super.collidesWithShape(other, result);
}
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-31 17:17:44 +08:00
public pointCollidesWithShape(point: es.Vector2, result: es.CollisionResult): boolean {
if (this.isUnrotated)
return ShapeCollisions.pointToBox(point, this, result);
return super.pointCollidesWithShape(point, result);
}
2020-06-15 10:42:06 +08:00
}
}