2020-07-23 11:00:46 +08:00
|
|
|
|
module es {
|
|
|
|
|
|
export class Rectangle extends egret.Rectangle {
|
2020-07-28 16:11:58 +08:00
|
|
|
|
public _tempMat: Matrix2D;
|
|
|
|
|
|
public _transformMat: Matrix2D;
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取矩形的最大点,即右下角
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get max() {
|
|
|
|
|
|
return new Vector2(this.right, this.bottom);
|
|
|
|
|
|
}
|
2020-07-07 12:18:51 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/** 中心点坐标 */
|
|
|
|
|
|
public get center() {
|
|
|
|
|
|
return new Vector2(this.x + (this.width / 2), this.y + (this.height / 2));
|
|
|
|
|
|
}
|
2020-06-16 11:22:37 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/** 左上角的坐标 */
|
|
|
|
|
|
public get location() {
|
|
|
|
|
|
return new Vector2(this.x, this.y);
|
|
|
|
|
|
}
|
2020-07-28 16:25:20 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/** 左上角的坐标 */
|
|
|
|
|
|
public set location(value: Vector2) {
|
|
|
|
|
|
this.x = value.x;
|
|
|
|
|
|
this.y = value.y;
|
|
|
|
|
|
}
|
2020-06-10 17:41:53 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
public get size() {
|
|
|
|
|
|
return new Vector2(this.width, this.height);
|
|
|
|
|
|
}
|
2020-06-19 09:16:49 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
public set size(value: Vector2) {
|
|
|
|
|
|
this.width = value.x;
|
|
|
|
|
|
this.height = value.y;
|
|
|
|
|
|
}
|
2020-06-19 09:16:49 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 创建一个矩形的最小/最大点(左上角,右下角的点)
|
|
|
|
|
|
* @param minX
|
|
|
|
|
|
* @param minY
|
|
|
|
|
|
* @param maxX
|
|
|
|
|
|
* @param maxY
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static fromMinMax(minX: number, minY: number, maxX: number, maxY: number) {
|
|
|
|
|
|
return new Rectangle(minX, minY, maxX - minX, maxY - minY);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 给定多边形的点,计算边界
|
|
|
|
|
|
* @param points
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static rectEncompassingPoints(points: Vector2[]) {
|
|
|
|
|
|
// 我们需要求出x/y的最小值/最大值
|
|
|
|
|
|
let minX = Number.POSITIVE_INFINITY;
|
|
|
|
|
|
let minY = Number.POSITIVE_INFINITY;
|
|
|
|
|
|
let maxX = Number.NEGATIVE_INFINITY;
|
|
|
|
|
|
let maxY = Number.NEGATIVE_INFINITY;
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < points.length; i++) {
|
|
|
|
|
|
let pt = points[i];
|
|
|
|
|
|
|
|
|
|
|
|
if (pt.x < minX) minX = pt.x;
|
|
|
|
|
|
if (pt.x > maxX) maxX = pt.x;
|
|
|
|
|
|
|
|
|
|
|
|
if (pt.y < minY) minY = pt.y;
|
|
|
|
|
|
if (pt.y > maxY) maxY = pt.y;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this.fromMinMax(minX, minY, maxX, maxY);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/**
|
2020-07-31 17:17:44 +08:00
|
|
|
|
* 如果其他相交矩形返回true
|
2020-07-23 11:00:46 +08:00
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
|
|
|
|
|
public intersects(value: egret.Rectangle) {
|
|
|
|
|
|
return value.left < this.right &&
|
|
|
|
|
|
this.left < value.right &&
|
|
|
|
|
|
value.top < this.bottom &&
|
|
|
|
|
|
this.top < value.bottom;
|
|
|
|
|
|
}
|
2020-06-10 17:41:53 +08:00
|
|
|
|
|
2020-08-25 14:21:37 +08:00
|
|
|
|
public rayIntersects(ray: Ray2D, distance: Ref<number>): boolean{
|
|
|
|
|
|
distance.value = 0;
|
2020-07-31 19:33:04 +08:00
|
|
|
|
let maxValue = Number.MAX_VALUE;
|
|
|
|
|
|
|
|
|
|
|
|
if (Math.abs(ray.direction.x) < 1E-06){
|
|
|
|
|
|
if ((ray.start.x < this.x) || (ray.start.x > this.x + this.width))
|
2020-08-25 14:21:37 +08:00
|
|
|
|
return false;
|
2020-07-31 19:33:04 +08:00
|
|
|
|
}else{
|
|
|
|
|
|
let num11 = 1 / ray.direction.x;
|
|
|
|
|
|
let num8 = (this.x - ray.start.x) * num11;
|
|
|
|
|
|
let num7 = (this.x + this.width - ray.start.x) * num11;
|
|
|
|
|
|
if (num8 > num7){
|
|
|
|
|
|
let num14 = num8;
|
|
|
|
|
|
num8 = num7;
|
|
|
|
|
|
num7 = num14;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-25 14:21:37 +08:00
|
|
|
|
distance.value = Math.max(num8, distance.value);
|
2020-07-31 19:33:04 +08:00
|
|
|
|
maxValue = Math.min(num7, maxValue);
|
2020-08-25 14:21:37 +08:00
|
|
|
|
if (distance.value > maxValue)
|
|
|
|
|
|
return false;
|
2020-07-31 19:33:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Math.abs(ray.direction.y) < 1E-06){
|
|
|
|
|
|
if ((ray.start.y < this.y) || (ray.start.y > this.y + this.height))
|
2020-08-25 14:21:37 +08:00
|
|
|
|
return false;
|
2020-07-31 19:33:04 +08:00
|
|
|
|
}else{
|
|
|
|
|
|
let num10 = 1 / ray.direction.y;
|
|
|
|
|
|
let num6 = (this.y - ray.start.y) * num10;
|
|
|
|
|
|
let num5 = (this.y + this.height - ray.start.y) * num10;
|
|
|
|
|
|
if (num6 > num5){
|
|
|
|
|
|
let num13 = num6;
|
|
|
|
|
|
num6 = num5;
|
|
|
|
|
|
num5 = num13;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-25 14:21:37 +08:00
|
|
|
|
distance.value = Math.max(num6, distance.value);
|
2020-07-31 19:33:04 +08:00
|
|
|
|
maxValue = Math.max(num5, maxValue);
|
2020-08-25 14:21:37 +08:00
|
|
|
|
if (distance.value > maxValue)
|
|
|
|
|
|
return false;
|
2020-07-31 19:33:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-25 14:21:37 +08:00
|
|
|
|
return true;
|
2020-07-31 19:33:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取所提供的矩形是否在此矩形的边界内
|
|
|
|
|
|
* @param value
|
|
|
|
|
|
*/
|
|
|
|
|
|
public containsRect(value: Rectangle) {
|
|
|
|
|
|
return ((((this.x <= value.x) && (value.x < (this.x + this.width))) &&
|
|
|
|
|
|
(this.y <= value.y)) &&
|
|
|
|
|
|
(value.y < (this.y + this.height)));
|
|
|
|
|
|
}
|
2020-06-19 09:16:49 +08:00
|
|
|
|
|
2020-07-31 17:17:44 +08:00
|
|
|
|
public contains(x: number, y: number): boolean{
|
|
|
|
|
|
return ((((this.x <= x) && (x < (this.x + this.width))) && (this.y <= y)) && (y < (this.y + this.height)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
public getHalfSize() {
|
|
|
|
|
|
return new Vector2(this.width * 0.5, this.height * 0.5);
|
|
|
|
|
|
}
|
2020-06-19 18:16:42 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取矩形边界上与给定点最近的点
|
|
|
|
|
|
* @param point
|
2020-07-27 16:10:36 +08:00
|
|
|
|
* @param edgeNormal
|
2020-07-23 11:00:46 +08:00
|
|
|
|
*/
|
2020-07-27 16:10:36 +08:00
|
|
|
|
public getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2 {
|
|
|
|
|
|
edgeNormal = Vector2.zero;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
|
|
|
|
|
|
// 对于每个轴,如果点在盒子外面
|
|
|
|
|
|
let res = new Vector2();
|
|
|
|
|
|
res.x = MathHelper.clamp(point.x, this.left, this.right);
|
|
|
|
|
|
res.y = MathHelper.clamp(point.y, this.top, this.bottom);
|
|
|
|
|
|
|
|
|
|
|
|
// 如果点在矩形内,我们需要推res到边界,因为它将在矩形内
|
|
|
|
|
|
if (this.contains(res.x, res.y)) {
|
|
|
|
|
|
let dl = res.x - this.left;
|
|
|
|
|
|
let dr = this.right - res.x;
|
|
|
|
|
|
let dt = res.y - this.top;
|
|
|
|
|
|
let db = this.bottom - res.y;
|
|
|
|
|
|
|
|
|
|
|
|
let min = Math.min(dl, dr, dt, db);
|
|
|
|
|
|
if (min == dt) {
|
|
|
|
|
|
res.y = this.top;
|
|
|
|
|
|
edgeNormal.y = -1;
|
|
|
|
|
|
} else if (min == db) {
|
|
|
|
|
|
res.y = this.bottom;
|
|
|
|
|
|
edgeNormal.y = 1;
|
|
|
|
|
|
} else if (min == dl) {
|
|
|
|
|
|
res.x = this.left;
|
|
|
|
|
|
edgeNormal.x = -1;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
res.x = this.right;
|
|
|
|
|
|
edgeNormal.x = 1;
|
|
|
|
|
|
}
|
2020-06-19 09:16:49 +08:00
|
|
|
|
} else {
|
2020-07-23 11:00:46 +08:00
|
|
|
|
if (res.x == this.left) edgeNormal.x = -1;
|
|
|
|
|
|
if (res.x == this.right) edgeNormal.x = 1;
|
|
|
|
|
|
if (res.y == this.top) edgeNormal.y = -1;
|
|
|
|
|
|
if (res.y == this.bottom) edgeNormal.y = 1;
|
2020-06-12 20:24:51 +08:00
|
|
|
|
}
|
2020-07-23 11:00:46 +08:00
|
|
|
|
|
2020-07-27 16:10:36 +08:00
|
|
|
|
return res;
|
2020-06-12 20:24:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
public getClosestPointOnBoundsToOrigin() {
|
|
|
|
|
|
let max = this.max;
|
|
|
|
|
|
let minDist = Math.abs(this.location.x);
|
|
|
|
|
|
let boundsPoint = new Vector2(this.location.x, 0);
|
|
|
|
|
|
|
|
|
|
|
|
if (Math.abs(max.x) < minDist) {
|
|
|
|
|
|
minDist = Math.abs(max.x);
|
|
|
|
|
|
boundsPoint.x = max.x;
|
|
|
|
|
|
boundsPoint.y = 0;
|
|
|
|
|
|
}
|
2020-06-11 20:36:36 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
if (Math.abs(max.y) < minDist) {
|
|
|
|
|
|
minDist = Math.abs(max.y);
|
|
|
|
|
|
boundsPoint.x = 0;
|
|
|
|
|
|
boundsPoint.y = max.y;
|
|
|
|
|
|
}
|
2020-07-08 18:12:17 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
if (Math.abs(this.location.y) < minDist) {
|
|
|
|
|
|
minDist = Math.abs(this.location.y);
|
|
|
|
|
|
boundsPoint.x = 0;
|
|
|
|
|
|
boundsPoint.y = this.location.y;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return boundsPoint;
|
2020-07-07 12:18:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number) {
|
|
|
|
|
|
if (rotation == 0) {
|
2020-07-28 16:11:58 +08:00
|
|
|
|
this.x = parentPosition.x + position.x - origin.x * scale.x;
|
|
|
|
|
|
this.y = parentPosition.y + position.y - origin.y * scale.y;
|
|
|
|
|
|
this.width = width * scale.x;
|
|
|
|
|
|
this.height = height * scale.y;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 特别注意旋转的边界。我们需要找到绝对的最小/最大值并从中创建边界
|
|
|
|
|
|
let worldPosX = parentPosition.x + position.x;
|
|
|
|
|
|
let worldPosY = parentPosition.y + position.y;
|
|
|
|
|
|
|
|
|
|
|
|
// 将参考点设置为世界参考
|
|
|
|
|
|
this._transformMat = Matrix2D.create().translate(-worldPosX - origin.x, -worldPosY - origin.y);
|
|
|
|
|
|
this._tempMat = Matrix2D.create().scale(scale.x, scale.y);
|
|
|
|
|
|
this._transformMat = this._transformMat.multiply(this._tempMat);
|
|
|
|
|
|
this._tempMat = Matrix2D.create().rotate(rotation);
|
|
|
|
|
|
this._transformMat = this._transformMat.multiply(this._tempMat);
|
|
|
|
|
|
this._tempMat = Matrix2D.create().translate(worldPosX, worldPosY);
|
|
|
|
|
|
this._transformMat = this._transformMat.multiply(this._tempMat);
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: 这有点傻。我们可以把世界变换留在矩阵中,避免在世界空间中得到所有的四个角
|
|
|
|
|
|
let topLeft = new Vector2(worldPosX, worldPosY);
|
|
|
|
|
|
let topRight = new Vector2(worldPosX + width, worldPosY);
|
|
|
|
|
|
let bottomLeft = new Vector2(worldPosX, worldPosY + height);
|
|
|
|
|
|
let bottomRight = new Vector2(worldPosX + width, worldPosY + height);
|
|
|
|
|
|
|
2020-08-28 18:04:50 +08:00
|
|
|
|
Vector2Ext.transformR(topLeft, this._transformMat, topLeft);
|
|
|
|
|
|
Vector2Ext.transformR(topRight, this._transformMat, topRight);
|
|
|
|
|
|
Vector2Ext.transformR(bottomLeft, this._transformMat, bottomLeft);
|
|
|
|
|
|
Vector2Ext.transformR(bottomRight, this._transformMat, bottomRight);
|
2020-07-28 16:11:58 +08:00
|
|
|
|
|
|
|
|
|
|
let minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
|
|
|
|
|
let maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
|
|
|
|
|
let minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
|
|
|
|
|
let maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
|
|
|
|
|
|
|
|
|
|
|
this.location = new Vector2(minX, minY);
|
|
|
|
|
|
this.width = maxX - minX;
|
|
|
|
|
|
this.height = maxY - minY;
|
|
|
|
|
|
}
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-16 11:22:37 +08:00
|
|
|
|
}
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|