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

87 lines
3.1 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;
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
*/
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
*/
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);
for (let i = 0; i < this.points.length; i ++)
this._originalPoints[i] = this.points[i];
2020-07-07 12:18:51 +08:00
}
public overlaps(other: Shape){
// 特殊情况这一个高性能方式实现其他情况则使用polygon方法检测
2020-07-23 09:10:27 +08:00
if (this.isUnrotated){
if (other instanceof Box)
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
public collidesWithShape(other: Shape){
// 特殊情况这一个高性能方式实现其他情况则使用polygon方法检测
2020-07-23 09:10:27 +08:00
if (other instanceof Box && (other as Box).isUnrotated){
return ShapeCollisions.boxToBox(this, other);
}
2020-06-15 10:42:06 +08:00
// TODO: 让 minkowski 运行于 cricleToBox
2020-06-15 10:42:06 +08:00
return super.collidesWithShape(other);
}
2020-06-15 10:42:06 +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-06-15 10:42:06 +08:00
}
}